|
| 1 | +# Controlling Program Flow |
| 2 | + |
| 3 | +- Sequence |
| 4 | + - Ordering statements sequentially |
| 5 | +- Selection |
| 6 | + - Making decisions |
| 7 | +- Iteration |
| 8 | + - Looping or repeating |
| 9 | + |
| 10 | +We can implement any algorithm. |
| 11 | + |
| 12 | +## Selection - Decision Making |
| 13 | + |
| 14 | +- `if` statement |
| 15 | +- `if-else` statement |
| 16 | +- Nested `if` statements |
| 17 | +- `switch` statement |
| 18 | +- Conditional operator `?:` |
| 19 | + |
| 20 | +## Iteration - Looping |
| 21 | + |
| 22 | +- `for` loop |
| 23 | +- `while` loop |
| 24 | +- `do-while` loop |
| 25 | +- Range-based `for` loop |
| 26 | +- `continue` and `break` |
| 27 | +- Infinite `loops` |
| 28 | +- Nedted `loops` |
| 29 | + |
| 30 | +### `if` statement |
| 31 | + |
| 32 | +- `if` statement followed by a boolean true/false value |
| 33 | +- If the value of the expression is true then execute the statment |
| 34 | +- If the expression is false then skip the statement |
| 35 | +- Indentation is helpful |
| 36 | + |
| 37 | +```c++ |
| 38 | +if (expr) |
| 39 | + statement; |
| 40 | +``` |
| 41 | + |
| 42 | +#### Block statement |
| 43 | + |
| 44 | +- Block statement is a sequence of statements inside a block which is made up of `{}` |
| 45 | + |
| 46 | +```c++ |
| 47 | +if (num > 10) { |
| 48 | + ++num; |
| 49 | + cout << "this too"; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +- Block statement comes in when more than 1 statement is executed if the statement is true |
| 54 | +- Create a block of code by inclding more than one statement in code block `{}` |
| 55 | +- Blocks can also contain variable declarations |
| 56 | +- These variables are visible only within the block - local scope |
| 57 | + |
| 58 | +### `if-else` statement |
| 59 | + |
| 60 | +```c++ |
| 61 | +if (expr) |
| 62 | + statement 1; |
| 63 | +else |
| 64 | + statement 2; |
| 65 | +``` |
| 66 | + |
| 67 | +- If the expression is **true** then execute **statement 1** |
| 68 | +- If the expression is **false** then execute **statement 2** |
| 69 | +- Block statements still works |
| 70 | + |
| 71 | +### `if-else-if` construct |
| 72 | + |
| 73 | +```c++ |
| 74 | +if (score > 90) |
| 75 | + cout << "A"; |
| 76 | +else if (score > 80) |
| 77 | + cout << "B"; |
| 78 | +else if (score > 70) |
| 79 | + cout << "C"; |
| 80 | +else if (score > 60) |
| 81 | + cout << "D"; |
| 82 | +else // all others must be F |
| 83 | + cout << "F"; |
| 84 | + |
| 85 | +cout << "Done"; |
| 86 | +``` |
| 87 | + |
| 88 | +### Nested `if` statement |
| 89 | + |
| 90 | +```c++ |
| 91 | +if (expr1) |
| 92 | + if (expr2) |
| 93 | + statement1; |
| 94 | + else |
| 95 | + statement2; |
| 96 | +``` |
| 97 | + |
| 98 | +- `if` statement is nested within another |
| 99 | +- Allows testing of multiple conditions |
| 100 | +- `else` belongs to closest `if` |
| 101 | +- We have a **dangaling else problem** since we have 2 `if` but only 1 `else` |
| 102 | + |
| 103 | +### `Switch` statement |
| 104 | + |
| 105 | +- The control expression must evaluate to an integer type |
| 106 | +- The case expression must be constant expressions that evaluate to integer or intergers literals |
| 107 | +- Once a match occurs all following case sections are executes UNTIL a `break` is reached the switch complete |
| 108 | +- Best practice: provide break statement for each case |
| 109 | +- Best practice: `default` is optional, but should be handled |
| 110 | + |
| 111 | +```c++ |
| 112 | +switch (integer_control_expr) { |
| 113 | + case expression_1: statement_1; break; |
| 114 | + case expression_2: statement_2; break; |
| 115 | + case expression_3: statement_3; break; |
| 116 | + ... |
| 117 | + case expression_n: statement_n; break; |
| 118 | + default: statement_default; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +- `integer_control_expr` is a control expression must evaluate to an `int` type or an `enumeration` type |
| 123 | +- then the series of `case` statements enclosed in `{}` |
| 124 | +- the value of the control expression, `integer_control_expr`, is compared with the values following the `case` keyword |
| 125 | +- then the code after the `:` under the case will be executed until it hits a `break` statement |
| 126 | +- best practice to include `break` statement after all cases |
| 127 | +- `default` case is optional and is catch-all if none case matches |
| 128 | +- `break` is not needed in the default case |
| 129 | + |
| 130 | +```c++ |
| 131 | +// fall-through example |
| 132 | +switch (selection) { |
| 133 | + case '1': cout << "1 selected"; |
| 134 | + break; |
| 135 | + case '2': cout << "2 selected"; |
| 136 | + break; |
| 137 | + case '3': |
| 138 | + case '4': cout << "3 or 4 selected"; |
| 139 | + break; |
| 140 | + default: cout << "1,2,3,4 NOT selected"; |
| 141 | +} |
| 142 | + |
| 143 | +// with an enumeration |
| 144 | +switch (screen_color) { |
| 145 | + case red: cout << "red"; break; |
| 146 | + case green: cout << "green"; break; |
| 147 | + case blue: cout << "blue"; break; |
| 148 | + default: cout << "should never execute"; |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Conditional Operator |
| 153 | + |
| 154 | +```c++ |
| 155 | +(cond_expr) ? expr1 : expr2 |
| 156 | +``` |
| 157 | + |
| 158 | +- cond_expr evaluates to a boolean expression |
| 159 | + - if cond_expr is true then the value of expr1 is returned |
| 160 | + - if cond_expr is false then the value of expe2 is returned |
| 161 | +- Similar to `if-else` statement |
| 162 | +- Ternary operator |
| 163 | +- Very useful when used inline |
| 164 | +- Very easy to abuse |
| 165 | + |
| 166 | +```c++ |
| 167 | +int a{10}, b{20}; |
| 168 | +int score{92}; |
| 169 | +int result {}; |
| 170 | + |
| 171 | + |
| 172 | +result = (a > b) ? a : b; |
| 173 | +// if a is greater than b , then the result = a |
| 174 | +// otherwise, result = b |
| 175 | + |
| 176 | +result = (a < b) ? (b - a) : (a - b); |
| 177 | +// if b is greater than a, then the result = (b - a) |
| 178 | +// otherwise, result = (a - b) |
| 179 | + |
| 180 | +result = (b != 0) ? (a/b) : 0; |
| 181 | +// if b is not equal to zero, then the result = (a/b) |
| 182 | +// otherwise, result = 0 |
| 183 | + |
| 184 | +cout << ((score > 90)) ? "Excellent" : "Good"); |
| 185 | +// if score is greater than 90, then output "Excellent" |
| 186 | +// otherwise, output "Good" |
| 187 | +``` |
| 188 | +
|
| 189 | +### Looping |
| 190 | +
|
| 191 | +- The third basic building block of programming |
| 192 | + - sequence, selection, iteration |
| 193 | +- Iteration or repetition |
| 194 | +- Allows the execution of a statement or block of statements repeatedly |
| 195 | +- Loops are made up a loop condition and the body which contains the statements to repeat |
| 196 | +
|
| 197 | +#### Use-cases |
| 198 | +
|
| 199 | +- A specific number of times |
| 200 | +- For each element in a collection |
| 201 | +- While a specific condition remains true |
| 202 | +- Until a specific condition becomes false |
| 203 | +- Until we reach the end of some input stream |
| 204 | +- Forever |
| 205 | +- Many, many more |
| 206 | +
|
| 207 | +#### C++ Looping Constructs |
| 208 | +
|
| 209 | +- `for` loop |
| 210 | + - iterate a specific number of times |
| 211 | +- `range-based for` loop |
| 212 | + - one iteration for each element in a range or collection |
| 213 | +- `while` loop |
| 214 | + - iterate while a condition remains true |
| 215 | + - stop wen the codition becomes false |
| 216 | + - check the condition at the beginning of every iteration |
| 217 | +- `do-while` loop |
| 218 | + - iterate while a condition remains true |
| 219 | + - stop when the condition becomes false |
| 220 | + - check the condition at the end of every iteration |
| 221 | +
|
| 222 | +### `for` Loop |
| 223 | +
|
| 224 | +```c++ |
| 225 | +for (initialization; condition; increment) |
| 226 | + statement; |
| 227 | +
|
| 228 | +for (initialization; condition; increment) { |
| 229 | + statement(s); |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +#### Example |
| 234 | +```c++ |
| 235 | +int i {0}; |
| 236 | + |
| 237 | +for (i = 1; i <=5; ++i) |
| 238 | + cout << i << endl; |
| 239 | +``` |
| 240 | +
|
| 241 | +#### Comma operator |
| 242 | +
|
| 243 | +```c++ |
| 244 | +for (int i{1}, j{5}; i <= 5; ++i, ++j){ |
| 245 | + cout << i << " * " << j << " : " << (i * j) << endl; |
| 246 | +} |
| 247 | +// 1 * 5 : 5 |
| 248 | +// 2 * 6 : 12 |
| 249 | +// 3 * 7 : 21 |
| 250 | +// 4 * 8 : 32 |
| 251 | +// 5 * 9 : 45 |
| 252 | +``` |
| 253 | + |
| 254 | +- The basic for loop is very clear and concise |
| 255 | +- Since the for loop's expressions are all optional, it is possible to have |
| 256 | + - no initialization |
| 257 | + - no test |
| 258 | + - no increment |
| 259 | + |
| 260 | +```c++ |
| 261 | +for (;;) |
| 262 | + cout << "Endless loop" << endl; |
| 263 | +``` |
0 commit comments