Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.

Similar presentations


Presentation on theme: "1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition."— Presentation transcript:

1 1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition. For this we use conditionals if statements switch statements

2 2 Conditionals Example: IF score is higher than 50 THEN grade is PASS ELSE grade is FAIL In C++ this corresponds to one statement with 3 parts: if (score > 50) { grade = PASS; } else { grade = FAIL; }

3 3 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 1 : the condition. An expression that evaluates to TRUE or FALSE

4 4 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 2 : the TRUE part. A block of statements that are executed if the condition evaluates to TRUE

5 5 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 3 : the FALSE part. A block of statements that are executed if the condition evaluates to FALSE

6 6 Conditionals Sometimes, we do not need a FALSE part: In that case, if the condition is FALSE, execution will continue at the statement following the if-statement. if (gas_tank_state == EMPTY) { fill_up_tank(); }

7 7 Conditionals If the TRUE or the FALSE part consists of only one statement, the curly braces may be omitted. The following statements are equivalent: if (score > 50) { grade = PASS; } else { grade = FAIL; } if (score > 50) grade = PASS; else grade = FAIL;

8 8 Conditionals We often use cascading if-statements: if (score > 90) lettergrade = 'A'; else if (score > 75) lettergrade = 'B'; else if (score > 60) lettergrade = 'C'; else if (score > 50) lettergrade = 'D'; else lettergrade = 'F';

9 9 Conditionals Cascading if-statements may sometimes be replaced with a switch statement: if (lettergrade == 'A') cout << "Very good!"; else if (lettergrade == 'B') cout << "Good!"; else if (lettergrade == 'C') cout << "Adequate"; else cout << "Work harder!"; switch (lettergrade) { case 'A': cout << "Very good!"; break; case 'B': cout << "Good!"; break; case 'C': cout << "Adequate"; break; default: cout << "Work harder!"; break; }

10 10 Conditionals switch (expression) { case value1: statements; break; case value2 : statements; break;... default : statements; break; } In English: Check the value of expression. Is it equal to value1 ? If yes, execute the statements and break out of the switch. If no, Is it equal to value2 ? etc. If it's not equal to any of the above, execute the default statements and then break out of the switch

11 11 Conditionals switch (expression) { case value1: statements; break; case value2 : statements; break;... default : statements; break; } -- expression should evaluate to either an int or a char -- NEVER omit break; (see next slide for an example of what may happen) -- ALWAYS have a default to cover the case when none of the above values match

12 12 Conditionals switch (lettergrade) { case 'A': case 'B': case 'C': case 'D': cout << "You passed!"; break; case 'F' : cout << "You failed!"; break; default: cout << "You received a " << lettergrade; } This is equivalent to: if (lettergrade == 'A' || lettergrade == 'B' || lettergrade == 'C' || lettergrade == 'D') cout << "You passed!"; else if (lettergrade == 'F') cout << "You failed!"; else cout << "You received a " << lettergrade;

13 13 Conditionals int x = -1; int y; switch ( x ) { case -1: y = 10; case 1 : y = 20; default : y = 30; } cout << y; This piece of code prints 30 on the screen x is -1, so the first case applies. y is assigned the value 10. Since there is no break statement, execution continues to the next case and eventually y becomes 30 which is not what we intended. This event is called fall-through.


Download ppt "1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition."

Similar presentations


Ads by Google