C++ for loop


Syntax of for Loop :
 for (init; condition; increment)
 {
    // block of statement.
 }
Example :

#include <iostream>
using namespace std;
int main()
{
    for(int i = 0; i < 10 ; i++)
    {
        cout<<i<<" ";
    }
    return 0;
}

Output :
 1 2 3 4 5 6 7 8 9 10
Explanation :
init - Initializes the variable at the beginning of the loop to some value. This value is the starting point of the loop.
condition - Decides whether the loop will continue running or not. While this condition is true, the loop will continue running.


increment - The part of the loop that changes the value of the variable created in the variable declaration part of the loop. The increment statement is the part of the loop which will eventually stop the loop from running.

0 Comments