Names specified here
Name Description Notes Source Availability
break Exit from loop L Keyword C89 C90 C95 C99 C11
continue Jump to end of loop L Keyword C89 C90 C95 C99 C11
do Loop syntax L Keyword C89 C90 C95 C99 C11
for Loop syntax L Keyword C89 C90 C95 C99 C11
while Loop syntax L Keyword C89 C90 C95 C99 C11

C permits the repeated execution of a statement with an iteration statement or loop, a statement that contains another statement to be executed multiple times, plus the means to determine whether another execution is warranted. C provides three types of loop. The first to be shown is a while loop:

while (i++ < 10)
  printf("Hello!\n");
iteration-statement
while ( expression ) statement

This loop will keep on incrementing i until it reaches 10 or more. At that point, it terminates, and execution falls through to whatever follows the loop.

The expression i++ < 10 is the test, and it is interpreted as a condition. The test is evaluated before each iteration of the loop, It also has the side-effect of incrementing i. If the value of the expression at that moment is non-zero, the body of the loop, the printf statement, is executed, and then execution returns to the beginning of the loop, and a new test.

The number of complete iterations depends on the initial value of i. If it started at zero, the message will be printed 10 times. Importantly, if it started at 10 or higher, i will be incremented, but the loop will terminate immediately, printing no message.

Another type of loop is dowhile:

do
  printf("Hello!\n");
while (i++ < 10);
iteration-statement
do statement while ( expression ) ;

This behaves almost identically to the while loop, except that the test is evaluated only at the end of each iteration. Consequently, this kind of loop will always iterate at least once.

The third type of loop is the for loop:

for (i = 0; i < 10; i++)
  printf("Hello!\n");
iteration-statement
for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement
since C99

This combines essential components of most loops, and places them syntactically at the start. The expression i = 0 serves as initialization, and is evaluated exactly once at the start of the loop's execution. i < 10 is the test, and is evaluated once before each iteration; if it is zero, the loop terminates. i++ is the expression that modifies the controlling variable, ultimately leading to the test becoming zero, and the loop's termination; it is executed at the end of each iteration.

From C99, the initialization part can be a declaration:

for (int i = 0; i < 10; i++)
  printf("%d squared is %d\n", i, i * i);

This is a more compact equivalent of declaring an outer block for the control variable:

{
  int i;
  for (i = 0; i < 10; i++)
    printf("%d squared is %d\n", i, i * i);
}

In all loops, the body is a single statement. If you need more than one statement in a loop body, use a block statement as a single statement containing many statements within it:

for (int i = 0; i < 10; i++) {
  printf("%d squared is %d\n", i, i * i);
  printf("Root of %d is %g\n", i, sqrt(i * i));
}

On some rare occasions, you might need no statement. For that, you could use an empty block:

while (do_more_work()) { }

…or an empty statement:

while (do_more_work())
  ;

Within the body of a loop, it is possible to use the continue statement. It causes execution to jump to the end of the current iteration, and the next test to be evaluated. In a for loop, it first causes the evaluation of the next modifying expression, then the test:

for (int i = 0; i < 10; i++) {
  if (is_magic(i))
    continue;
  printf("%d squared is %d\n", i, i * i);
}

This might be preferable to inverting the test, and making a potentially large portion of code conditional:

for (int i = 0; i < 10; i++) {
  if (!is_magic(i)) {
    printf("%d squared is %d\n", i, i * i);
  }
}

A break statement within a loop causes it to terminate immediately. Its main purpose is to allow a test to be performed within the loop body, rather than at the start or end.

int i = 0;
while (true) {
  // Do stuff.
  i++;
  if (i == 10)
    break;
  // Do more stuff.
}

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000