Previous

C++ Iteration Statements

Next

The statements which are responsible for the execution of a certain set of statement again and again till a condition is true

Iteration statement also known as loop statement 

C++ provided three types of loop control structures:

(I) while loop.

(II) do while loop.

(III) for loop.

=>   C++ While Loop:-

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

Syntax:-

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

Example:-

write the program to print 0to 4 number using while loop.

#include <iostream>
using namespace std;

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

OUTPUT

0
1
2
3
4

=> C++ do while foop:-

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:-

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

Example:- 

Write the program to print 0  to 4 using do while loop.

#include <iostream>
using namespace std;

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

OUTPUT

0
1
2
3
4

 

C++ For Loop

The for loop is the most commonly used loop in C++. It allows you to specify the initialization, condition, and update expressions in a single line.

 

Syntax:

for (initialization; condition; update) {
    // Code to be executed
}

Example:-

write the program to print 0 to 4 using for loop.

#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 5; i++) {
    cout << i << "\n";
  }
  return 0;
}
OUTPUT:- 

0
1
2
3
4

Nested Loops:-

It is also possible to place a loop inside another loop. This is called a nested loop.

The "inner loop" will be executed one time for each iteration of the "outer loop".

 

syntax:-

for (initialization; condition; increment/decrement) {
    // Outer loop statements

    for (initialization; condition; increment/decrement) {
        // Inner loop statements
    }
}

Exapmle:-

#include <iostream>
using namespace std;

int main() {
  // Outer loop
  for (int i = 1; i <= 2; ++i) {
    cout << "Outer: " << i << "\n";  // Executes 2 times
    
    // Inner loop
    for (int j = 1; j <= 3; ++j) {
      cout << " Inner: " << j << "\n";  // Executes 6 times (2 * 3)
    }
  }
  return 0;
}
OUTPUT:-

Outer: 1
 Inner: 1
 Inner: 2
 Inner: 3
Outer: 2
 Inner: 1
 Inner: 2
 Inner: 3

*Difference between while and do-while loop in C, C++.

while do-while
(I) Condition is checked first then statement(s) is executed. (I)  Statement(s) is executed atleast once, thereafter condition is checked.
(II) It might occur statement(s) is executed zero times, If condition is false. (II)  At least once the statement(s) is executed.
(III) No semicolon at the end of while. while(condition) (III)  Semicolon at the end of while. while(condition);
(IV) Variable in condition is initialized before the execution of loop. (IV)  variable may be initialized before or within the loop.
(V) while loop is entry controlled loop. (V)  do-while loop is exit controlled loop.
(VI) while(condition) { statement(s); } (VI)  do { statement(s); } while(condition);

*Difference between for and while loop in C, C++

for Loop

while Loop

(I) Initialization may be either in the loop statement or outside the loop. (I) Initialization is always outside the loop.
(II) Once the statement(s) is executed then increment is done. (II) The increment can be done before or after the execution of the statement(s).
(III) It is normally used when the number of iterations is known. (III) It is normally used when the number of iterations is unknown.
(IV) Condition is a relational expression. (IV) The condition may be an expression or non-zero value.
(V) It is used when initialization and updation of conditions are simple. (V) It is used for complex initialization.
(VI) For loop is entry controlled loop. (VI)While loop is also entry controlled loop.
(VII)Syntax: for ( init ; condition ; iteration ) { statement(s); } (VII) Syntax: while ( condition ) { statement(s); }
(VIII) The for loop is used when the number of iterations is known. (VIII) The while loop is used when the number of iterations is unknown.

*Difference between for and do-while loop in C, C++

For loop Do-While loop
(I) Statement(s) is executed once the condition is checked. (I) Condition is checked after the statement(s) is executed.
(II) It might be that statement(s) gets executed zero times. (II) Statement(s) is executed at least once.
(III) For the single statement, bracket is not compulsory. (III) Brackets are always compulsory.
(IV) Initialization may be outside or in condition box. (IV) Initialization may be outside or within the loop.
(V) for loop is entry controlled loop. (V) do-while is exit controlled loop.

(VI) for ( init ; condition ; iteration )
{

statement (s);

}

(VI) do {

statement(s);

}
while (condition);