Lesson 6.3. For loop in C

The for loop repeats a sequence of instructions like the while and do..while loops. The for loop is particularly suitable for loops that need to run a given number of times. Typically, to count or to browse indexed data (such as arrays for example):

for ( i=0 ; i<100 ; i++) {
    //...
}

Syntax

The for loop expects 3 parameters:

for ( initialization ; test ; iterator ) {
    // ...
    Block of instructions
    // ...
}

As with the while and do..while the for loop repeats the sequence of instructions as long as the condition is true.

Here is the flowchart for the for loop:

Flowchart of the for loop in C

Note the arrangement of the blocks in the flowchart. The configuration is similar to the while loop. The body of the loop may not be executed if the test is false from the start.

There is no semicolon at the end of the for, unless the instruction block is empty.

Each parameter is optional, we can write for(;;);, which is equivalent to while(1);.

Example 1

Here is an example of a for loop that displays all integers between 0 and 42:

int i;
// Display integers between 0 and 42
for ( i=0 ; i<=42 ; i++) {
    printf ("%d ", i);    
}

Example 2

Here is an example of a for loop that displays the integers between -27 and 15 with a step of 3 :

int i;
// Displays the integers between -27 and 15 with a step of 3
for ( i=-27 ; i<=15 ; i+=3) {
  printf ("%d ", i);    
}

Exercise 1

Write a program that displays all integers between 0 and 49 inclusive:

int i;
// Display integers from 0 to 49
// COMPLETE HERE

Exercise 2

Write a program that displays all integers in descending order from 49 to 0 inclusive:

int i;
// Displays all integers in descending order from 49 to 0 inclusive:
// COMPLETE HERE

Exercise 3

Write a program that displays all integers from 0 to 256 with a step of 16 :

int i;
// Displays all integers from 0 to 256 with a step of 16
// COMPLETE HERE

Exercise 4

Write a program that displays all powers of 2 from 1 to 1024 :

int i;
// Displays all powers of 2 from 1 to 1024
// COMPLETE HERE

Here is the expected output:

1 2 4 8 16 32 64 128 256 512 1024

Exercise 5

Write the code that displays a square with n letters on each side containing the letters of the alphabet.

Here is an example for a square of size 6x6:

ABCDEF
BCDEFG
CDEFGH
DEFGHI
EFGHIJ
FGHIJK

Quiz

What does the following code display?

for ( i=0 ; i<5 ; i++) 
    printf ("%d ",i);
Check Bravo! The condition is strictly less than or equal to. When i is 5, we leave the loop before the display. Try again...

What does the following code display?

for ( i=0 ; i<=5 ; i++) 
    printf ("%d ",i);
Check Bravo! When i is 5, there is a last iteration, because the test is less than or equal.. Try again...

What does the following code display?

for ( i=5 ; i>=0 ; i--) 
    printf ("%d ",i);
Check Bravo! The test is lower or equal, zero is displayed. Try again...

What does the following code display?

for ( i=5 ; i>0 ; i--); 
    printf ("%d ",i);
Check Bravo! There is a semicolon at the end of the for, the program displays 0. Try again...

What does the following code display?

int i,j;
for ( i=0 ; i<4 ; i++)
    for ( j=0 ; j<3 ; j++)
        printf ("%d ", j);
Check Bravo! The first loop executes 4 times the second one which displays 0 1 2. Try again...

What does the following code display?

int i,j;
for ( i=0 ; i<4 ; i++) {
    for ( j=0 ; j<i ; j++)
        printf ("%d ", j);
    printf ("- ");
}
Check Bravo! The first loop displays 4 times the second which displays integers from 0 to i-1. The first time, when i is 0, we do not enter the second loop. Try again...

See also


Last update : 09/27/2023