Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"— Presentation transcript:

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

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

3 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 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 5 Introduction CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

6 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 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 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 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 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 11 Selection Criteria with Boolean Expression CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

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

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

14 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 15 THE if STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Example :

16 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 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 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 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 20 THE if..else if STATEMENT pair

21 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 < 18.5 18.5 – 24.9 25 – 29.9 > 30 Underweight Normal weight Overweight Obesity

22 Get Yearly Income Income >= 30000 Tax Income = Income – 30000 Tax = 4500 + (Tax Income * 0.28) Tax = Income * 0.15 Display Tax TRUEFALSE


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

Similar presentations


Ads by Google