Loops are the repeated execution of some segment of code for as long as a specific Boolean condition (often called the test-expression,) remains true. Remember that in C any non-zero value is considered true, 0 is considered false.
The simplest form of loop is the "while" loop, which executes it's statement for as long as its expression remains true:
while (expression) statement
while (
)
This might look familiar to the if-statement, which executes it's statement if the expression is true. A while loop is ultimately like an if-statement that just keeps executing the statement over and over for as long as the expression remains true.
// This loop will never stop, since the expression is always true. while (1) printf("Print forever\n");
/** * Prints 0 through 9. Remember that 'i++' gives it's value before incrementing. * When i becomes 10, the expression is no longer true, so the execution moves * on to the statements that follow the while loop. */ int i=0; while (i < 10) printf("%d\n", i++);
// The same as the previous but using a compound statement and moving the // increment of i outside of the printf() function. int i = 0; while (i < 10) { printf("%d\n", i); i = i + 1; }
Many loops have the form:
initilize some index variable(s) while ( test-expression ) { statements increment index variable(s) }
while
(
) {
}
so a specific type of loop was created to handle this form specifically, it is the "for" loop:
for( init-expression ; test-expression ; increment-expression) statement
for(
;
Note:
// Will loop forever: for(;;) statement
// Will loop forever:
for(;;)
init-expression is only performed once. You may declare variables inside this expression, however these variables are not visible outside of the for loop.
test-expression is performed at the beginning of each loop, performing the statement only if this is true. If omitted it is always true.
increment-expression is performed after the statement (or compound statement) at the end of each loop. This is always executed even if a continue is raised.
// print 0 to 9. Note we're using a compound statement: for(int i=0; i < 10; i++) { printf("%d\n",i); }
// Print from 10 down to 0. for(i=10; i >= 0; i--) printf("%d\n", i);
// Print from 'start' to 'end', skipping 'skip' steps after each output: // start, end and skip are integer variables that contain some value. for(i=start; i < end; i = i + skip) printf("%d\n", i);
Sometimes we want to exit a loop or do the next loop iteration early, to do so we use the break; and continue; statements respectively.
The break statement exits a loop (or a switch statement, discussed at a later time,) immediately. Execution continues at the point following the loop.
Grammar:
break;
/** * Prints 0 through 6. Stops printing when i becomes 7 because the if statement * becomes true and thus executes the break, which causes the for loop to * terminate. */ for(int i=0; i < 10; i++) { if (i > 6) break; printf("%d\n", i); }
In a while loop it immediately moves execution to the test-expression of the while loop.
In a for loop it immediately moves execution to the increment-expression part of the loop, which then moves to the test-expression.
continue;
int sum = 0, n; /** * The following will read integers from the input and accumulate a sum of all * positive non-negative values. The `sum = sum + n` statement will be skipped * when n is <= 0 because the continue will move execution back to the * test-expression of the while loop immediately. */ while (scanf("%d", &n) == 1) { if (n <= 0) continue; sum = sum + n; } printf("sum = %d\n", sum);
int sum = 0, n; // Another way of writing the above example: while (1) { if (scanf("%d", &n) != 1) break; // Note how the test condition is reversed (!= vs ==) since we only want to exit the loop // when the scanf fails to read one integer. if (n <= 0) continue; sum = sum + n; } printf("sum = %d\n", sum);
// This will only print the numbers 3 through 6: for(i=0; i < 10; i++) { if (i < 3) continue; // If i < 3 go immediately to the increment-expression if (i > 6) break; // When i reaches 7 (i.e. > 6) exit the loop. // Only when i passes the above two checks does this statement run: printf("%d\n", i); } // i will be == 7 here, since the for loop exited early.