C++ Program Flow Control: Selection Domingos Begalli Saddleback College, Computer Science CS1B, Spring 2018 1 / Domingos Begalli CS1B Spring 2018 C++ Introduction 1/19 19
Control program flow control structures selection if, if-else, switch -case repetition for, while, do-while 2 / Domingos Begalli CS1B Spring 2018 C++ Introduction 2/19 19
Control logical expressions logical expressions are also called boolean expressions expressions that evaluate to or conditional statements evaluate them using relational operators 3 / Domingos Begalli CS1B Spring 2018 C++ Introduction 3/19 19
Control relational operators operator description == equal to!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to 4 / Domingos Begalli CS1B Spring 2018 C++ Introduction 4/19 19
Relational Operators comparing integers and real numbers usage result 8 == 8 8!= 8 8 < 8 8 > 8.0 8.0 <= 8 8.0 >= 8.0 5 / Domingos Begalli CS1B Spring 2018 C++ Introduction 5/19 19
Relational Operators comparing characters - CLI is your friend: man ascii [partial] ascii character set in decimal 32 sp 33! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44, 45-46. 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 125 } 126 ~ 127 del 6 / Domingos Begalli CS1B Spring 2018 C++ Introduction 6/19 19
Selection Flow one-way selection if (expression) process enter if (expression) process exit 7 / Domingos Begalli CS1B Spring 2018 C++ Introduction 7/19 19
Selection Flow two-way selection if (expression) process else process enter if (expression) process process exit 8 / Domingos Begalli CS1B Spring 2018 C++ Introduction 8/19 19
Logical Expressions bool type and boolean expressions old c++ compilers relied on 1 for & 0 for int age = 15; int legalage; legalage = ( age >= 21) // legalage i s now 0 new c++ compilers support boolean values & int age = 15; bool legalage; legalage = ( age >= 21) // legalage i s now f a l s e ( a l s o 0) 9 / Domingos Begalli CS1B Spring 2018 C++ Introduction 9/19 19
Logical Expressions boolean operators operator description! not && and or 10 / Domingos Begalli CS1B Spring 2018 C++ Introduction 10/19 19
Logical Expressions boolean operators - the! (not) operator expression value 11 / Domingos Begalli CS1B Spring 2018 C++ Introduction 11/19 19
Logical Expressions boolean operators - the & & (and) operator expr 1 expr 2 value 12 / Domingos Begalli CS1B Spring 2018 C++ Introduction 12/19 19
Logical Expressions boolean operators - the (or) operator expr 1 expr 2 value 13 / Domingos Begalli CS1B Spring 2018 C++ Introduction 13/19 19
Operators order of precedence value expression first unary! + - second * / % third + - fourth < <= >= > fifth ==!= sixth && seventh eighth = 14 / Domingos Begalli CS1B Spring 2018 C++ Introduction 14/19 19
Conditional Operator if can be replaced by the ternary operator?: expr_1? expr_2 : expr_3; for example, rather than: if ( a > b ) { max = a; } else { max = b; } the conditional version can be used: max = (a >= b)? a : b; // or : a > b? max = a : max = b; 15 / Domingos Begalli CS1B Spring 2018 C++ Introduction 15/19 19
Boolean Semantics short-circuit evaluation - for sake of optimization... compilers ignore second expression if x is greater than 5: (x > 5) (y == 3) similarly if x is less than 5: (x > 5) && (y == 3) however... x = 6; if (0 <= x <= 5) evaluates to since 0 < 6 evaluates to 1, which is less than 5 16 / Domingos Begalli CS1B Spring 2018 C++ Introduction 16/19 19
Floating-point Numbers Comparison other problems double x = 1.0; double y = 2.0/7.0 + 3.0/7.0 + 2.0/7.0; if (x == y) { cout << " x and y are the same\n"; } else { cout << "NOT the same: "; cout << "x = " << x << ", and y = " << y << endl; } // p r i n t s NOT the same : x = 1, and y = 1 17 / Domingos Begalli CS1B Spring 2018 C++ Introduction 17/19 19
Multiple Selections nested ifs enter if (expr_1) { block_1 } else if (expr_2) { block_2.... if (expr_1) elseif (expr_2) elseif (expr_n) process block_1 process block_2 process block_n } else if (expr_n) { block_n } else { default_block } else default_block exit 18 / Domingos Begalli CS1B Spring 2018 C++ Introduction 18/19 19
Multiple Selections switch case instead of nested ifs switch (selector) switch (selector) { case const_1: block_1 break; case const_2: block_2 break;.... case const_n: block_n break; default: default_block } case (const_1) block_1 break case (const_2) block_2 break case (const_n) block_n break default default block exit 19 / Domingos Begalli CS1B Spring 2018 C++ Introduction 19/19 19