Advanced for Loops - C++ Statement

C++ examples for Statement:for

Introduction

When the initialization and action sections contain more than one statement, they are separated by commas.

Here's an example:

Demo Code

#include <iostream> 

int main() //www.jav a2s  .  c  om
{ 

    for (int x = 0, y = 0; x < 10; x++, y++) 
    { 
        std::cout << x * y << "\n"; 
    } 
}

Result

This loop has an initialization section that sets up two integer variables: x and y.

Each section of a for loop also can be empty. The semicolons are still there to separate sections, but some of them contain no code.

Here's an example:

Demo Code

#include <iostream> 

int main() /*from w  w w  .j a  va 2  s. c  o  m*/
{ 
    int x = 0; 
    int y = 0; 
    for ( ; x < 10; x++, y++) 
    { 
        std::cout << x * y << "\n"; 
    } 
}

Result


Related Tutorials