C++ Loop Types

The looping statement is used to repeatedly execute set of statements or instructions until a condition is met. This looping statement is also known as an iteration statement. There are three kinds of loops in C++:

For Loop

Loop is very easy to understand and For loop is the simplest loop in C++. All elements of the loop gathered at one place (at the top of the loop), while others are scattered about the program.

Syntax For Loop

for(initalisation expression(s);test expression;update expression(s))
    body-of-the-loop


ExampleOf For Loop:
// An example of For Loop
#include <iostream>
using namespace std;
int main()
{
   system("cls");
   int i;      \\declaring variable
   for(i=1;i<=5;i++)      \\For loop
   cout << "\t"<< i;
   return 0;
}


Output :
1  2  3  4  5


The above statement will print the first 5 natural numbers i.e. the values ​​from 1 to 5. The variable i is initialised with the value 1 (initalisation expression: i = 1); As long as i is less than or equal to 5 (test expression: i <= 5) the loop prints the value of i and the value incremented by 1 (update expression ++ i) at the end of each iteration.

In a for loop, there may be multiple initalisation and multiple update expressions, but this multiple expression must be separated by a comma. Also if the body of the loop has more than one statement, then loop must begin and end with {} curly braces.



While Loop

While loop is also known as the entry-controlled loop because the test expression is checked while entering in the loop. While loop iteration occurs when the test expression evaluates to true, when the test expression evaluates to false the loop goes to the program control line after the loop body code.

Syntax While Loop

initalisation expression(s);
  While(test expression)
  {
    body-of-the-loop
    update expression(s)
  }


Example Of While Loop:
// An example of while Loop
#include <iostream>
using namespace std;
int main()
{
   system("cls");
   int i=1;      \\declaring variable
   while(i<=5)      \\While loop
   {      
    cout << "\t"<< i;
    i=i+1;      
  }     
return 0;   
}


Output :
1  2  3  4  5


The above statement will print the first 5 natural numbers i.e. the values ​​from 1 to 5. The variable i is initialised with the value 1 (initalisation expression: i = 1); As long as i is less than or equal to 5 (test expression: i <= 5) the loop prints the value of i and the value incremented by 1 (update expression ++ i) at the end of each iteration.



C++ Do-While Loop

Do-while loop is also known as the exit-controlled loop because the test expression is checked at the time of loop end. This means that the loop always executes at least once, even when the test expression initially evaluates to false.

For loops and while loops not executed at least once because the test expression initially evaluates to a false then the program control goes to the loop program control line after the loop body code. Do-while loop is used when the loop should be executed at least once.

Syntax do-While Loop

do {
        // code block to be executed
    }while (condition);


Example do-while Loop:
 // An example of do-while Loop 
// An example of do-while Loop int i = 0; do { cout << i << "\t"; i++; }while (i < 5);

Output :
0  1  2  3  4


The example uses do-while loop. The loop will always be executed at least once, even if the condition is false, as the code block is executed before the condition is checked.



Exercise:-

1. Which loop is known as the exit controlled loop?

A. For
B. While
C. DO-while
D. Foreach

View Answer


2. Which loop is not in C++?

A. For
B. While
C. DO-while
D. Foreach

View Answer



Program :-

Sum of Natural Numbers using loop

#include <iostream>
using namespace std;
int main()
{
    int n, sum = 0;
    cout << "Enter a positive integer: ";
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        sum += i;
    }
    cout << "Sum = " << sum;
    return 0;
}


Output :

Enter a positive integer: 50
Sum = 1275





Visit :


Discussion


* You must be logged in to add comment.