CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I Instructor: Mohammad Mojaddam
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 4: Control Structures I (Selection)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 4: Control Structures: Selection
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Branches and Program Design
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming Control Structures I (Selection).
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 4: Control Structures I (Selection)
Sequence, Selection, Iteration The IF Statement
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2 C ONTENTS CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Introduction Selection criteria with Boolean expression The if statement The if..else statement

L EARNING O UTCOME At the end of this class, student should be able to: Interpret the concept of relational and logical operators. Develop a program using if statement Differentiate between if and switch statement Produce programs using selection control

4 Introduction CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING A computer can proceed: In sequence Selectively (branch) - making a choice Repetitively (iteratively) - looping Some statements are executed only if certain conditions are met A condition is represented by a logical (Boolean) expression that can be true or false A condition is met if it evaluates to true

5 Introduction CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

6 Selection Criteria with Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Boolean expression is a sequence of operands and operators that combine to produce one of the Boolean values, true or false. 2 types of Boolean expression :  simple Boolean expression  compound Boolean expression

7 Selection Criteria with Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Simple Boolean expression : is of the form expression1 relational-operator expression2 Relational operators: Allow comparisons Require two operands Return 1 if expression is true, 0 otherwise

8 Selection Criteria with Boolean Expression Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Relational operators can be any of the following operators :

9 Selection Criteria with Boolean Expression Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Compound Boolean expression : is formed by combining simple Boolean expressions with the following logical operator :

10 Selection Criteria with Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING For example, the expression such as 5 <= x <= 10 can be written as the following compound Boolean expression: (5 <= x) && ( x <= 10)

11 Selection Criteria with Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

12 Selection Criteria with Compound Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

13 Selection Criteria with Compound Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

14 THE if STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Used in a one-way selection The syntax : if (condition) statement; statement is executed if the value of the condition is true statement is bypassed if the value of the condition is false ; program goes to the next statement

15 THE if STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Example :

16 THE if…else STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Two-way selection takes the form: if (expression/condition) statement1; else statement2; If expression is true, statement1 is executed otherwise statement2 is executed statement1 and statement2 are any C++ statements else is a reserved word

17 THE if…else STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Exercise 1 Consider the following statements : if final_score >= 60 status = pass else status = fail Write in C++ statement.

18 THE if…else STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Exercise 1 - Answer if (final_score >= 60) status = “pass”; else status = “fail”; Write in C++ statement.

19 THE if…else if…else STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Multi-way selection takes the form: if (expression) statement1; else if (expression) statement2; else if (expression) statement3; else statementn; If expression is true, statement1 is executed, else if statement2 is executed, otherwise statementn is executed.

20 THE if..else if STATEMENT pair

E XERCISE 2 Calculate BMI for a user. Enter the users weight (KG) and height (Meter). Display the following output: BMI=weight/height 2 BMICATEGORY < – – 29.9 > 30 Underweight Normal weight Overweight Obesity

Get Yearly Income Income >= Tax Income = Income – Tax = (Tax Income * 0.28) Tax = Income * 0.15 Display Tax TRUEFALSE