Conditionals n In many cases we want our

  • Slides: 25
Download presentation
Conditionals n In many cases we want our program to make a decision about

Conditionals n 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. n For this we use conditionals n if statements n switch statements 1

Conditionals n Example: IF score is higher than 50 THEN grade is PASS ELSE

Conditionals n 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; } 2

Conditionals Part 1 : the condition. An expression that evaluates to TRUE or FALSE

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

Conditionals if (score > 50) { grade = PASS; } else { grade =

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 4

Conditionals if (score > 50) { grade = PASS; } else { grade =

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 5

Conditionals n Sometimes, we do not need a FALSE part: if (gas_tank_state == EMPTY)

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

Conditionals n If the TRUE or the FALSE part consists of only one statement,

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

Conditionals n We often use cascading if-statements: if (score > 90) lettergrade = 'A';

Conditionals n 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'; 8

Conditionals n Cascading if-statements may sometimes be replaced with a switch statement: if (lettergrade

Conditionals n 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; } 9

Conditionals switch (expression) { case value 1: statements; break; case value 2 : statements;

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

Conditionals switch (expression) { case value 1: statements; break; case value 2 : statements;

Conditionals switch (expression) { case value 1: statements; break; case value 2 : 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 11

Conditionals This is equivalent to: switch (lettergrade) { case 'A': case 'B': case 'C':

Conditionals This is equivalent to: 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; } 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; 12

Conditionals int x = -1; int y; switch ( x ) { case -1:

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. 13

Loops n Loops repeat (iterate) a block of statements for a number of times.

Loops n Loops repeat (iterate) a block of statements for a number of times. n A terminating condition tells a loop when to stop iterating (e. g. terminate after 10 iterations or terminate when the user types NO) n Careful! If there is no terminating condition, then your program will never finish 14

Loops start n Pre-Test Loop n n n check condition if false, exit the

Loops start n Pre-Test Loop n n n check condition if false, exit the loop if true, execute statements, iterate: n check condition n if false, exit the loop n if true, execute statements, iterate: n initialize condition is(condition)true? yes { block of statements; etc. n The block of statements may not be executed at all (if condition is immediately false) n The condition must be updated no } finish 15

Loops n Pre-Test Loop n count-driven : n uses a "counter" to determine how

Loops n Pre-Test Loop n count-driven : n uses a "counter" to determine how many times it iterates n Example: n n every time the loop iterates, a counter variable is incremented by one. When the counter reaches a specific value, the condition becomes false and the loop terminates. event-driven : n uses an "event" to determine how many times it iterates n Example: n at each iteration the user is asked whether to continue. As long as the user types "yes" the loop iterates. When the user types "no" the condition becomes false and the loop terminates. 16

for loops n Pre-test, mainly count-driven n Syntax: for (init; condition; update) { statements;

for loops n Pre-test, mainly count-driven n Syntax: for (init; condition; update) { statements; } n Example: /* Frog lifetime*/ int days; for (days =155; days > 0; days--) { work_all_day(); sleep_all_night(); } die_quietly(); 17

while loops n Pre-test, mainly event-driven n Syntax: while (condition) { statements; } n

while loops n Pre-test, mainly event-driven n Syntax: while (condition) { statements; } n Example (event): /* Frog Feeding */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); clamp_mouth(); swallow_fly(); } 18

while loops n Pre-test, mainly event-driven n Syntax: while (condition) { statements; } n

while loops n Pre-test, mainly event-driven n Syntax: while (condition) { statements; } n Example (counter): /* Frog lifetime*/ int days; days = 155; /* initialize */ while ( days > 0 ) { /* condition */ work_all_day(); sleep_all_night(); days--; /* update */ } die_quietly(); 19

Loops n Post-Test Loop n execute statements n check condition n if false, exit

Loops n Post-Test Loop n execute statements n check condition n if false, exit the loop n if true, iterate: n etc. n The block of statements is always executed at least once n The condition must be updated start initialize condition { block of statements; } is(condition)true? no yes finish 20

Loops n Post-test loops n The block of statements is ALWAYS executed at least

Loops n Post-test loops n The block of statements is ALWAYS executed at least once! n Often used for data validation (e. g. if the user types a wrong selection, keep asking for a correct one) n A post-test loop may be count- or event-driven. 21

do-while loop n Post-test, mainly event driven n Syntax: do { statements; } while

do-while loop n Post-test, mainly event driven n Syntax: do { statements; } while (condition) n Example: /* Frog mating*/ do { have_mate = look_for_lady_frog(); } while ( have_mate == FALSE ) 22

Loops n In some cases, we may need to break put of a loop

Loops n In some cases, we may need to break put of a loop prematurely. To do that, we use a break statement. n Example: int days; float food, fat; . . . for( days = 155; days > 0; days--) { work_all_day(); if ( food+fat < 0. 01) break; sleep_all_night(); } die_quietly(); 23

Loops n In some cases, we may want to only execute part of the

Loops n In some cases, we may want to only execute part of the body during an iteration. To do that, we use a continue statement. n Example: /* Frog feeding v 2. 0 */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); if (!caught_fly()) continue; clamp_mouth(); swallow_fly(); } 24

break vs. continue vs. return n continue n it is used in loops n

break vs. continue vs. return n continue n it is used in loops n it means : skip the remaining statements in the loop body and iterate again n break n it is used in loops and switch statements n it means : skip the remaining statements in this block, break out of the block. When used in loops, it causes the loop to terminate n return n it may be used anywhere in a function. n it means : terminate the function. 25