C for loop

C for loop can be considered the most important loop in the C programming language. Usually, this loop is used when the number of repetitions is already known and it has more features than the while loop.

C for loop structure

The for-loop structure is as follows:

for(expression1; expression2; expression3){
    //statements
}
  1. Expression 1 initializes the loop-control variable. In this loop, there is a variable named loop controller. The loop controller counts the number of iterations and has an initial value. The length of the counter is determined by the number of repetitions of the loop.
    For example, if the number of loop repetitions is 200 times, you can introduce a variable of unsigned char type, but if the number of loop repetitions is 1000, you should choose a longer variable such as int type.
  2. Expression 2 is the loop-continuation condition. As long as the condition is true, the loop iterates. The movement step can be integer or decimal, positive or negative, or character.
  3. Expression 3 changes(increment, decrement, etc.) the control variable per iteration.

The flowchart of the for loop is as follows:

Consider the following example:

int a[7];

for(int i = 1; i < 8; i++)
    a[i] = i;
  1. One the first line, an empty array is defined.
  2. The loop’s counter, i, is initialized with value 1.
  3. The counter is incremented by one per iteration.
  4. As long as i is less the eight, the loop iterates.
  5. In each iteration, the value of i is assigned to the ith element of array “a.”
  6. For example, if the loop is in the second iteration, that means the value of i is equal to 2. In this case, the code inside the loop becomes a[2] = 2.
  7. The above loop does not contain { } because there is only one statement in the loop body. If the loop’s body has more than one statement, { } must be used.

Nested for loops

Nested loops are very practical, so look at the following examples:

Example1

for(int i = 1; i < 8; i++){      
    for(int j = 1; j < 256; j++)  
        printf("%d %d\n", i, j);    
}
  1. The outer loop is repeated seven times, and the inner ring is repeated 255 times. That is, every time the outer loop repeats, the inner loop repeats 255 times. Therefore, the total repetition is 255 x 7 times.
  2. In each iteration, the current value of i and j are printed out in one line.
  3. Click on the link to learn printf() function.

Example2

int a[10][20];
int i, j;
for(i = 1; i <= 10; i++)
    for(j = 1; j <= 20; j++)  
        a[i-1][j-1] = 1;
  1. In this example, a two-dimensional array is assigned. The outer loop controls the rows, while the inner loop controls ​​the columns of each row and assigns value to each array’s cell. If you need to read about arrays, check out the Arrays lesson.
  2. The outer loop has only one statement inside it, which is the inner loop, and the inner loop has only one statement.
  3. The outer loop is repeated 10 times, and each time it is repeated, the inner loop is repeated 20 times.
  4. You can select a row by repeating the first loop and fill the columns of the desired row with the repetitions of the second loop.
  5. This program inserts the value 1 into all members of the two-dimensional array a.

Loops need two statements called “break and continue” to exit the loop and jump to the next iteration, which we cover later.

Was this helpful?
[0]
Scroll to Top
Scroll to Top