• # Iterations

An Iteration involves repeating some portion of a program, a specified number of time or until a particular condition is reached. This repetitive operation in C and C++ is achieved through for, while and dowhile loop.

## How does a for loop work?

A for loop is the easiest iteration loop to understand the C and C++ loops. All the loop elements are gathered at one place unlike the other loops, where, the loop elements are scattered all over the loop body.

Syntax:

```for( initialization ; condition ; increment or decrement )
{
loop body ; - -
}```

E.g.:

```for(int i=0;i<5;i++)
{
loop body ;
}```
• Initialization: In the above example, initialization is i=0, the value of i will determine till what time the loop will run. As long as i, in this case, remain less than 5, the loop will run.
• Condition: This will check for the condition to be true. In the above example, the loop will check the value of i to be less than 5 and if the condition is true, the loop body will execute.
• Increment or Decrement : This statement increments or decrements the value of i. If the value of i is not incremented or decremented, the for loop can go into an infinite iteration.

A for loop can also have multiple initialization, condition checks and increment decrement operators, but they all must be separated by commas.

E.g.:

```for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--)
{
loop body ;
}```
```#include<iostream.h>
void main()
{
cout << "Program to show running of FOR loop" ;
cout << "This program will print first 10 natural numbers" ;
for(int i = 1; i <= 10; i++)
{
cout << "\n"<<i ;
}
}```

Output:

```Program to show running of FOR loop
This program will print first 10 natural numbers
1
2
3
4
5
6
7
8
9
10```

## How does a while loop work?

The second loop available, is the while loop which is an entry controlled loop. An entry controlled loop is one in which the condition check is done before the execution of the loop body. Where as a dowhile loop is an exit controlled loop in which the condition check is done after the execution of the loop body (we will learn more about do-while loop later)

Syntax:

```while(expression)
{
loop body ;
}```

Example:

```#include<iostream.h>
void main()
{
cout << "Program to show running of WHILE loop" ;
cout << "This program will print first 10 natural numbers" ;
int i = 1;
while(i <= 10)
{
cout << "\n"<<i ;
i++;
}
}```

Output:

```Program to show running of WHILE loop
This program will print first 10 natural numbers
1
2
3
4
5
6
7
8
9
10```

The above program prints the first 10 natural numbers using a while loop. In case of a for loop, the initialization, condition check and increment were all in the same line, while

In while loop, it is scattered over the program.

• int i=0 is the initialization
• i<=10 is the condition check
• i++ is the increment

Example 2: Program to calculate factorial of a number

```// Program to calculate factorial of a number
#include<iostream.h>
void main()
{
int i = 1, num, fact = 1;
cout << "\n Enter a number : "<<
cin >>num;
i = num;
while(num)
{
fact = fact * num;
--num;
}
}
cout << "\n The factorial of "<< i << " is " << fact;```

Output:

```Enter a number :
The factorial of 5 is 120```

The above program allows the user to input a number and print the factorial of that number using a while loop.

## How does a do-while loop work?

As mentioned earlier, the dowhile loop, unlike a while loop, is an exit controlled loop which means that it evaluates the test condition after executing the loop body statements. It implies that the dowhile loop executes at least once before the test condition is evaluated.

The loop body will execute at least once even if the test condition is false, where as the same would not happen in while loop.

Syntax:

```do
{
loop body ;
}
while
(
test-expression
);```

Example:

```#include<iostream.h>
void main()
{
cout << "The program shows the execution of DO-WHILE loop" ;
checr ch = 'A';
do
(
cout << "\n" << ch;
ch++;
)
while(ch <= 'M')
}```

Output:

```The program shows the execution of DO-WHILE loop

A
B
C
D
E
F
G
H
I
J
K
L
M```

The above program shows the execution of exit controlled loop, dowhile, which prints the alphabets from A to M. Here the test condition is tested after the execution of the loop body.

dowhile loops are used for making menu driven programs where the menu should be displayed at least once before the loop body is executed.

} 237 queries in 0.307 seconds.