Control Constructs (Answer) Practice Questions

1. ``` int m; for (m = 0; m < 5; m++) { if(m == 3) continue; printf("Line %d\n", m); } ```
Prints ```Line 0 Line 1 Line 2 Line 4 ``` The loop generates 0, 1, 2, 3, and 4. The `if ... continue` effectively skips around the `printf` on the number 3 iteration. It skips to the number 4 iteration before the 3 is printed.

2. ``` int m; m = 0; while (m < 5) { if(m == 3) continue; printf("Line %d\n", m); m++; } ```
Prints ```Line 0 Line 1 Line 2 ``` then goes into an infinite loop. Continue in a `for` loop runs the increment, but in a `while` loop it goes straight to the test.

3. ``` int m; m = 5; do printf("Line %d\n", m); while (m++ < 5) ``` Prints ```Line 5 ``` The `do` `while` loop always executes at least once. After the first body execution, the test is false.

4. ``` int m = 7; switch(m) { case 4: printf("Mike\n"); case 6: printf("Suzie\n"); case 7: printf("Arnold\n"); case 11: printf("Ann\n"); default: printf("Smith\n"); } ``` Prints ```Arnold Ann Smith ``` Remember: `switch` cases do not end at the start of the next case. You must use a break to leave cases explicitly. This is the same as Java; Pascal programmers will think, correctly, that this behaviour is nuts.

5. ``` enum {little, some, much, gobs = 1, national_debt} frank; frank = much; printf("%d %d\n", frank, frank == national_debt); ``` Prints `2 1`. In a C enumerated type, C numbers the enumeration names starting at 0, increasing by 1. Therefore `much` is 2. You may select a value, as `gobs` is set to 1, which also resets the numbering, so `national_debt` is set to 2. Therefore `frank` equals `national_debt`.