For loop

Estimated reading: 5 minutes 166 Views

The for loop in C++ is a control statement that allows you to execute a block of code repeatedly a certain number of times. The general syntax of a for loop is as follows:

for (initialization; condition; increment) {
  // code to execute
}

The initialization statement is executed once, before the for loop starts. The condition statement is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. The increment statement is executed after each iteration of the loop.

Here is an example of a for loop:

#include <iostream>

using namespace std;

int main()
{
    
    for (int c = 0; c < 10; c++)
    {
        cout << c << endl;
    }
    
    return 0;
}

The for loop works as follows:

  1. The initialization statement initializes the c variable to 0.
  2. The condition statement checks if the c variable is less than 10. If it is, the code block inside the loop is executed.
  3. The code block inside the loop prints the value of the c variable to the console.
  4. The increment statement increments the c variable by 1.
  5. Steps 2-4 are repeated until the condition statement evaluates to false.

When the condition statement evaluates to false, the for loop terminates.

Here is the output of this program:

0
1
2
3
4
5
6
7
8
9

Here is another example that prompts the user to enter 10 marks and then prints the average of those marks to the console:

#include <iostream>

using namespace std;

int main()
{
    int m = 0, sum = 0;
    for (int i = 1; i <= 10; i++)
    {
        cout << "Enter your marks:";
        cin >> m;
        sum = sum + m;
    }
    cout << "the avg = " << ((double)sum / 10) << endl;
    
    return 0;
}

The for loop works as follows:

  1. The initialization statement initializes the i variable to 1.
  2. The condition statement checks if the i variable is less than or equal to 10. If it is, the code block inside the loop is executed.
  3. The code block inside the loop prompts the user to enter their marks and stores the value in the m variable. The code block also adds the value of the m variable to the sum variable.
  4. The increment statement increments the i variable by 1.
  5. Steps 2-4 are repeated until the condition statement evaluates to false.

When the condition statement evaluates to false, the for loop terminates.

After the for loop has terminated, the program prints the average of the marks to the console. The average is calculated by dividing the sum of the marks by the number of marks.

You used the (double) cast in the expression ((double)sum / 10) to ensure that the result of the division operation is a floating-point number. This is important because the sum variable is an integer variable, and the division of two integers results in an integer. If you did not use the (double) cast, the program would print the integer result of the division operation to the console, which may not be accurate.

Here is an example of the output of this program:

Enter your marks: 10
Enter your marks: 20
Enter your marks: 30
Enter your marks: 40
Enter your marks: 50
Enter your marks: 60
Enter your marks: 70
Enter your marks: 80
Enter your marks: 90
Enter your marks: 100
the avg = 55.000000

This code prints the numbers from 10 to 50 to the console, separated by spaces:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 10; i <= 50; i++)
    {
        cout << i << " ";
    }

    return 0;
}

The for loop works as follows:

  1. The initialization statement initializes the i variable to 10.
  2. The condition statement checks if the i variable is less than or equal to 50. If it is, the code block inside the loop is executed.
  3. The code block inside the loop prints the value of the i variable to the console, followed by a space.
  4. The increment statement increments the i variable by 1.
  5. Steps 2-4 are repeated until the condition statement evaluates to false.

When the condition statement evaluates to false, the for loop terminates.

Here is the output of this program:

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Why is size_t used as the loop counter?

The size_t type is used as the loop counter because it is the type that is returned by the sizeof operator. This ensures that the loop counter can be used to iterate over any type of data, including arrays and objects.

This code prints the numbers from 10 to 1 to the console, in reverse order, separated by spaces:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 10; i >= 1; i--)
    {
        cout << i << " ";
    }

    return 0;
}

The for loop works as follows:

  1. The initialization statement initializes the i variable to 10.
  2. The condition statement checks if the i variable is greater than or equal to 1. If it is, the code block inside the loop is executed.
  3. The code block inside the loop prints the value of the i variable to the console, followed by a space.
  4. The decrement statement decrements the i variable by 1.
  5. Steps 2-4 are repeated until the condition statement evaluates to false.

When the condition statement evaluates to false, the for loop terminates.

Here is the output of this program:

10 9 8 7 6 5 4 3 2 1

Share this

For loop

Or copy link

CONTENTS
English