Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.

Similar presentations


Presentation on theme: "Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections."— Presentation transcript:

1 Selection

2 Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections Observe use of boolean expressions for modeling logical circuits First look at class mutator methods

3 Computer Programming 3 Selection: the if Statement Single branch if (boolean_exp) statement Dual branch if (boolean_exp) statement1 else statement2 Multibranch if (boolean_exp) statement else if (boolean_exp) statement else if … else …

4 Computer Programming 4 Selection: the if Statement Note multibranch if – Appears to be a different version – Actually is an if statement with another if statement as the statement of the else if (boolean_exp) statement else if (boolean_exp) statement else if …

5 Computer Programming 5 The Multi-branch if The if ’s final form has a nested if as Statement 2 : if (Cond 1 ) Stmt 1 else if (Cond 2 ) Stmt 2... else if (Cond N ) Stmt N else Stmt N+1 Cond 1 Stmt 1 T F Stmt 2 Cond 2 TF Stmt N Cond N TF Stmt N+1...

6 Computer Programming 6 Multibranch if Note which else goes with which if

7 Computer Programming 7 The Dangling else Problem Consider: if (x > 0) if (y > 0) z = sqrt (x) + sqrt(y); else cerr << " * * Unable to Compute * *"; Which if does the else go with? In a nested if statement, an else is matched with the nearest preceding unmatched if.

8 Computer Programming 8 Conditional Expression Form: condition ? expression 1 : expression 2 Behavior – condition evaluated – If it is true, expression 1 returned as result – If it false, expression 2 returned as result Try it out … what gets printed? double a = 5, b = 10, c = -3; cout 0 ? "real root" : "imaginary root") ;

9 Computer Programming 9 Warning: Confusing = and == True and false in C++ – An integer value of 0 interprets as false – A non zero value interprets as true Assignments are expressions x = 7; – The value is assigned to the variable … and – The expression has a value (the value assigned) What happens when you write if (x = 7) …

10 Computer Programming 10 Warning: Confusing = and == When you write if (x = 7) … – The value is assigned to the variable – The expression has that value (in this case non zero) – The value of the expression is used to select the true or false branch of the if statement The program will – Compile and run without crashing – But will probably not execute as expected

11 Computer Programming 11 Selection: The switch Statement Multiple choices. – E.g. choose between different conversions You can use – The multi-branch if has non-uniform execution time: Menu option 'A' requires 1 comparison Menu option 'B' requires 2 comparisons... Menu option 'F' requires 6 comparisons Computations that are “later” in the if take longer. There are situations where the time to select one of many statements must be uniform. Solution: – Use switch statement instead

12 Computer Programming 12 The switch Statement Form: switch (expression) { case_list 1 : statement_list 1 ; case_list 2 : statement_list 2 ; … default: statement_list n+1 ; }

13 Computer Programming 13 Warning C++ switch statements exhibit drop-through behavior. 1. Expression is evaluated. 2. The control jumps to the right case statement. 3. Control continues within the switch statement until: a. The end of the switch is reached; b. A break is executed, terminating the switch; c. A return is executed, terminating the function; or d. An exit() is executed, terminating the program.

14 Computer Programming 14 Cases with No Action In some situations the expression of a switch statement may be a legal value but: – No action takes place for that value Solution: – Include that value in the case list – But include no statement, only the break ; switch (option) { case 1 : doWhatever(); break; case 2 : doSomethingElse(); break; … case 9: case 10: break; }

15 Computer Programming 15 When to Use switch Choose switch over if-else-if when: 1. The equality == comparison is being performed 2. The same expression is being compared in each condition … and … 3. The value to which the expression is being compared is int compatible

16 Computer Programming 16 Boolean Logic and Digital Design Based on work by George Boole – Note basic rules, pg 316 Use of these rules became important with invention of digital computer – Circuits

17 Computer Programming 17 Boolean Logic and Digital Design Combine to create half adder circuit – digit1, digit2, sum, and carry are all binary digits (either 0 or 1)

18 Computer Programming 18 Problem 1 Sort three real numbers x, y, and z in a non- decreasing order. (The solution will be provided in class)

19 Computer Programming 19 Problem 2 The user is presented with a menu of characters, namely A, B, C, and Q. When the user chooses A, we print “The choice is A”. When B is chosen, the output is “The choice is B”. If the user chooses C, then “The choice is C” is printed out. When Q is chosen, the program exits. (The solution will be provided in class)


Download ppt "Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections."

Similar presentations


Ads by Google