top of page

Loop Statement in C++

Updated: Jul 4, 2023

Loops are an essential part of programming as they allow us to repeat a block of code until a specific condition is met.


In C programming, we have three types of loop statement:

  1. for loop

  2. while loop

  3. do-while loop

Each loop has its own characteristics and is suited for different scenarios. When the number of iterations is predetermined, the for loop is generally preferred over the while or do-while loops.


Loop Statement in C++


For Loop

The for loop is a commonly used loop in C programming.

It consists of an

  1. Initialization statement

  2. test expression

  3. update expression.

The initialization statement is executed only once at the beginning of the loop. Then, the test expression is evaluated. If the test expression is false, the for loop is terminated, and the program continues with the next line of code after the loop. However, if the test expression is true, the statements inside the body of the for loop are executed. After executing the statements, the update expression is evaluated, and the loop repeats the process by evaluating the test expression again. This cycle continues until the test expression becomes false.


This process goes on until the test expression is false. When the test expression is false, the loop terminates.



Syntax:

for(initialization; condition; incr/decr){    
//code to be executed   
} 

Example:

// Print numbers from 1 to 10
#include <stdio.h>
int main() {
  int i;

  for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10


2. While Loop

The while loop is another type of loop used in C programming. It begins by evaluating the test expression inside the parentheses ().


If the test expression is true, the statements inside the body of the while loop are executed. After executing the statements, the test expression is evaluated again. This process continues until the test expression becomes false.


If the test expression is false from the beginning or at any point during the loop, the loop terminates, and the program moves on to the next line of code after the loop.


The while loop allows for repeated execution of a block of code as long as the specified condition remains true.



Syntax:

while (testExpression) 
{
    // statements inside the body of the loop 
}

Example:

#include <iostream> 
using namespace std;  
int main() {         
 int i=1;      
 while(i<=10)   
       {      
            cout<<i <<"\n";    
            i++;  
          }       
    }  

Output:

1 2 3 4 5 6 7 8 9 10


3. Do While Loop

The do...while loop is a type of loop in C programming that ensures the body of the loop is executed at least once before checking the test expression.


In the do...while loop, the body of the loop is executed first, and then the test expression is evaluated. If the test expression is true, the body of the loop is executed again, and the test expression is evaluated once more. This process continues until the test expression becomes false.


If the test expression is false from the beginning or at any point during the loop, the loop terminates, and the program moves on to the next line of code after the loop.


The do...while loop is useful in situations where you want to execute a block of code at least once, regardless of the initial test expression's value.



Syntax:

do
{
   // statements inside the body of the loop
}
while (testExpression);

Example:

// Program to add numbers until the user enters zero
#include <stdio.h>
int main(){
    double number, sum = 0;

    // the body of the loop is executed at least oncedo
    {
        printf("Enter a number: ");
        scanf("%lf", &number);
        sum += number;
    }
    while(number != 0.0);

    printf("Sum = %.2lf",sum);

    return 0;
}

Output

Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70

Conclusion

Loop statements are an essential part of programming in C++. They allow us to repeat a block of code multiple times, making our programs more efficient and flexible. We have explored three types of loop statements in C++: the for loop, the while loop, and the do-while loop.

0 comments

Comments


bottom of page