Loop Types:

A statement or set of statements that is executed repeatedly is known as loop. He structure that repeats a statement is known as repetitive, iterative, or looping construct. Loops are basically used for two purposes.

To execute a statement or number of statements for a specified number of times for example, a user may display his name on screen 10 times.

To use a sequence of values e.g. a user may display a set of natural numbers from 1 to 10.

There are three types of loops in C++:

  1. While loop
  2. For loop
  3. Do-while loop

 

While loop

It is the simplest loop of C++. This loop executes one or more statements while the given condition remains true. It is useful where the number of iteration are not known in advance.

SYNTAX

While (condition)

         {

Statements;

}

Condition: the condition is given relational expression. It controls the iteration of loop. The statements are executed only if the condition is true.

Statement: it is the instruction that is executed when the condition is true. Two or more statements are specified in braces {}. It is called the body of the loop.

Working of While loop:

First of all, the condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in the body. After executing the statements it again moves to the start of loop and check the condition. This process continues as long as the condition remains true.

When the condition becomes false, the loop is terminated. While loop terminates only when the condition becomes false if the condition remains true, the loop never ends. A loop that has no end point is known as infinite loop.

 

Flowchart

EXAMPLE

#include<iostream>

Void main ()                                                 working-of-while-loop

{

Int N;

N =1;

While (n<=5)

{

Cout<<”country”<<endl;

}N++;

 country

 

Do-while loop:

The do-while is an iterative control in C++ language. This loop executes one or more statements while the given condition is true. In this loop, the condition comes after the body of loop. The loop is important in a situation where a statement must be at executed at least once.

SYNTAX
do-while-loop
do

{

Statements;

}

while (condition);

Working of Do-while loop

First of all, the body of loop is executed. After executing the statements in the loop body, the condition is evaluated. If it is true, the control again enters the body of loop and executes all statements in the body again.

This process continues as long as the condition remains true. The loop terminates when the condition becomes false. This loop is executed at least once even if the condition is false in the beginning.

EXAMPLE

# include<iostream>

Void main ()

{

Int N;

N = 1;

do

{

Cout<<”country”<<endl;

N++;

}

while (n<=5);

}

working-of-do-while-loopFor loop

For loop executes one or more statements for a specified number of times. this loop is also counter controlled loop. It is the most flexible loop. That is why the most programmers use this loop in programs.

SYNTAX

For (initialization; condition; increment/decrement)

{

Statements

}

Initialization

It specifies the starting value of initialization.

Condition

The condition is given as a relational expression. The statement is executed only is the condition is true.

Increment/Decrement

This part specifies the change in counter variable.

Statement

Statement is the instruction that is executed when the condition is true.

Working of “for” loop

The number of iteration depends on the initialization, condition and increment/decrement parts. The initialization part is only once executed when the control enters the loop. After initialization, the given condition is evaluated.

It it is true, the control enters the body of the loop and executes all statements in it. Then the increment/decrement part is executed that changes the value of counter variable.

The control again moves to condition part. This process continues while the condition remains true. The loop is terminated when the condition becomes false.

 

Flowchart

Example:working-of-for-loop

#include <iostream>

{Void main ()

int n;

for (n=1;n<=5;n++)

{

Cout<<”country”<<endl;
}

 

 

Working of “for” loop