C++ for loop - for loop in C++ with Syntax , Flowchart and Example

 

C++ for loop - for loop in C++ with Syntax , Flowchart and Example

for loop:

  • The for loop generally executes a statement or block of statements a predetermined number of times by the counter.
  • Statement can be a single statement or a compound (block) statement.
  • for is reserved word
  • You specify how a for loop operates using three expressions separated by semicolons between parentheses following the for keyword.

Syntax of for loop:

Syntax of for loop

 for loop executes as follows:

1. Initial statement executes (usually initialize variable) 
2. Loop condition evaluated. If loop condition evaluates to true. 
  •    Execute for loop statement 
  •    Execute update statement 
3. Repeat Step 2 until loop condition evaluates to false  

Flowchart of For loop:

Flowchart of For loop

Example of for loop:

  •  Printing 1 - 10 using for loop
    #include <iostream>
    using namespace std;
    int main(){
        int x;
        for( x=1 ; x<11 ; x++){
            cout<<x<<" "
        
    }

Output: 
    1 2 3 4 5 6 7 8 9 10


Post a Comment

0 Comments