Chapter 6 Section 6.4 Altering the Flow of Control CS 50 Hathairat Rattanasook
continue; The continue statement will skip the current iteration of a loop and jump to the next iteration. // continue in a while loop while ( expression ) { continue; // or with a do-while loop (can also be used in: for loop) do { continue; while ( expression ); CS 50 - Hathairat Rattanasook 1
break; The break statement will terminate the current loop and jump to the end of it. // break in a while loop while ( expression ) { break; // or with a do-while loop (can also be used in: for loop, switch) do { break; while ( expression ); CS 50 - Hathairat Rattanasook 2
continue & break The continue; and break; statements are used in loops, such as for-loop, while-loop, and do-while-loop. The break; statement can also be used in a switch statement. Both statements alter the control flow of a loop. Since both statements alter the flow of the program, they should be used with caution! CS 50 - Hathairat Rattanasook 3
continue statement The continue; statement is used inside a loop. When it is encountered, it causes the control to jump to start the next iteration instead of executing the next statement as it would normally do. while ( expression ) { if (expression2) continue; In the example, the while loop executes normally until the if-condition becomes true and the continue; statement is encountered. Then the flow ends the current iteration and jumps to the end of the loop body. The loop will continue by evaluating the expression and if true start the next iteration CS 50 - Hathairat Rattanasook 4
break statement The break; statement is used inside a loop or switch statement. When it is encountered, it jumps to the end of the current loop or switch statement. while ( expression ) { if (expression2) break; In the example, the while loop executes normally until the if-condition becomes true and the break; statement is encountered. Then the flow ends the current loop and jumps to the code after. Unlike the continue; statement, the loop does NOT continue. CS 50 - Hathairat Rattanasook 5
break with switch 1 switch ( expression ) { 2 case constant1: statement1(s); break; 3 case constant2: statement2(s); break; 4 case constant3: statement3(s); break; 5 default: statementd(s); break; 6 7... If the expression inside the switch ( and ) matches expression constant2, statement2(s) is/are executed. When the break; statement is encountered, it will cause the flow to jump to the end of the switch statement in line 7 instead of continuing with statement3(s) of case constant3 as the usual flow would be. CS 50 - Hathairat Rattanasook 6
while Example 1 int i = 10; 2 while (i > 0) { 3 printf(" "); 4 if (i-- % 3 == 0) 5 continue; // jump to while (..) 6 printf("*"); 7 Output: * * * * * * * 1. i = 10 2. 10 > 0 => true 3. 10 % 3 == 0 => false 4. 10-- => 9 5. 9 > 0 => true 6. 9 % 3 == 0 => true 7. i-- => 8 8. continue // skips printf("*"); 9. jump to while (i > 0) 10. 9 > 0 => true 11.... CS 50 - Hathairat Rattanasook 7
for Example char str[256], ch; int i; printf("enter some text: "); fgets(str, 256, stdin); printf("enter a char to search: "); scanf("%c", &ch); for (i = 0; i < strlen(str); i++) { if (str[i] == ch) { printf("*"); continue; printf("%c", str[i]); The example searches for a char in a string. It prints out a * at the position where the char is found. The the char matches, it prints out * and then continues to the next iteration. CS 50 - Hathairat Rattanasook 8
for Example int indexof(char* str, char ch) { int i, found = -1; for (i = 0; i < strlen(str); i++) { if (*(str + i) == ch) { found = i; break; return found; The function indexof() searches for a char ch in a string str. When the first occurrence of ch is found, it stores the index i in the variable found. Then, the break statement terminates the loop and jumps to to the end of it. Then the return statement returns found. CS 50 - Hathairat Rattanasook 9
Questions? 1. What is the difference between continue and break? 2. Does continue execute the loop expression before starting the next iteration? 3. What happens if break/continue is used in a nested loop? CS 50 - Hathairat Rattanasook 10
Answers 1 and 2 1. continue skips the current iteration. break terminates the loop 2. Yes, for example: while (i < 10) { statement1; continue; statement2; statementn; When continue is encountered, the current iteration is skipped and statements 2 to N are not executed. Then (i < 10) is checked and if true, the next iteration starts. CS 50 - Hathairat Rattanasook 11
Answer 3 3. What happens if break/continue is used in a nested loop? while (i < 10) { // outer loop statement1; while (k < 5) { // inner loop continue; statement2; statementn; In the example above, break will only terminate the inner loop, and will continue the outer loop at statement2. Remember, break/continue will only affect the closest loop to where they are used. CS 50 - Hathairat Rattanasook 12