C++ for loops

C++ for loop is similar to the while loop; It continues to execute a block of code until the predefined condition is met. This is an entry-controlled loop. This tutorial will teach you how to use the for loop in C++.

The basic format of the for loop statement is:

Syntax:

for ( init; condition; increment )
{
    statement(s);
}

Figure - Flowchart of the for loop:

cplusplus-for

Example C++ program to demonstrate the for loop:

Example:

#include <iostream>
using namespace std;

int main ()
{
    /* local variable Initialization */
    int n = 1, times=5;

    /* for loops execution */
    for( n = 1; n <= times; n = n + 1 )
    {
        cout << "C++ for loops: " << n <<endl;
    }
    return 0;
}

Program Output:

cplusplus-for-loops



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.