Looping Statements

 For loop

 When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

 for (statement 1; statement 2; statement 3)

 {
  // code block to be executed
}

Statement 1: (initialization) is executed (one time) before the execution of the code block.

Statement 2: defines the condition for executing the code block.

Statement 3: (increment/decrement) is executed (every time) after the code block has been executed.

 Example 01:

#include<iostream>

using namespace std;

int main()
{
    for(int i=0; i<5;i++)
    cout<<" "<<i;
}

 

while loop:

 The while loop loops through a block of code as long as a specified condition is true:

 Syntax:

Initialization;

while (condition)

 {
  // code block to be executed

Increment/decrement;
}

 

Example 02:

#include<iostream>

using namespace std;

int main()
{
    int i=0;
    while(i<5){
    cout<<" "<<i;
    i++;
    }
   
    return 0;
}


do…while loop:

 The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

Initialization;

do

{
  // code block to be executed

Increment/decrement;
}
while (condition);

Example 03:

#include<iostream>

using namespace std;

int main()
{
    int i = 5;
    do{
       
        cout<<i;
        i++;
    }while(i<5);
}


In C++ since break and continue are control flow statements used to alter the execution of loops (for, while and do-while). Here’s how each works:

break

Reason: To Break out of a Loop Extremely useful when it comes to escaping from a loop, break and pass keywords would basically allow you to control when and where respectably, the loop will end or continue.

continue

Used for: To escape from the further code execution in the same iteration of loop & go to the next iteration. It is used to skip the remaining code inside the loop if a specific condition is met.


Example 04:

#include<iostream>

using namespace std;

int main()
{
    int i;
    for(i=1; i<10; i++){
   
   
    if(i==5)
    continue;
   
    if(i==7)
     break;
    cout<<" "<<i;
    }
    return 0;
}


Goto

The goto Statement in C++, the goto statement provides an unconditional jump from the goto statement to a labeled statement within the same function. It is often viewed as a last resort for control flow since it can make the code harder to read and maintain.

label: an identifier, followed by a colon (:) This is the label to which we want our control flow to jump when the goto statement is encountered.


Example 05:

#include<iostream>

using namespace std;

int main()
{
    int input = 5, i;
    double num, avg, sum;
   
   
   
    for(int i=1; i<=input; i++)
    {
       
        cout<<" Enter a number : "<<i<<" ";
        cin>>num;
       
        sum+=num;
       
        if(num<0.0)
        {
            goto jump;
        }
       
    }
   
    jump:
        avg=sum/(i-1);
        cout<<" Sum : "<<sum;
        cout<<" Average: "<<avg;
       
        return 0;
   
}













Post a Comment

0 Comments