LOOP in C++

by Monika Dadool 11-Sep-18

0 307


Looping is used in the C++ when the repetition of a block of statements is required. Loop permits you to repeatedly execute a block of statements.

Suppose we want to print "codingtag" 11 times, this can be possible by the iterative method i.e. in the code, you need to write the cout() statement 11 times. For example

// C++ program to illustrate need of loops

#include <iostream>
using namespace std;
int main()
{
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
cout << "codingtag\n";
return 0;
}
Result:

codingtag
codingtag
codingtag
codingtag
codingtag
codingtag
codingtag
codingtag
codingtag

.........


This method is long and will become complex if the no of time increases. This problem can be easily solved by looping because the statement required to be written only once and the loop will be executed 11 times. It will be illustrated in further examples.

In other words, the loop can be defined as the sequence of the various instructions which will repeat until the specific condition is reached.

There are two types of loop are available in C++ programming language:

1) Entry Control loop
2) Exit Control loop

Entry Control loop

Firstly, the condition is tested before entering the body of the loop.

Example:

1) For
2) While loop

Exit Control loop
The test condition is evaluated at the end of the loop body which means the body of the loop will execute at least once regardless of the test conditions, whether it is true or false.

Example

do- while loop
For loop

For loop is known as the entry controlled loop or repetition control structure which permits the user to write a loop that executes a specific line to a number of times. The user can be able to perform n number of steps with a single statement.

The syntax used for the "For loop" declaration is as under:

For (initialization expression; test expression; update expression)

{
// loop body
// statements required to be executed
}

Where,

Initialization expression is to initialize the loop counter to any value that may be int k = 9;

Test expression is used for evaluating the condition

Update expression is used to increments/decrements the loop variable by a certain value i.e. it can be k++ or k-

Working of "For loop"

Firstly, the initialization statement executes at the beginning after that the test expression is verified. If the test expression is wrong, for loop is terminated but if the test expression is not wrong, the inside code in the body will execute and update expression is updated. After that, the test expression is evaluated again and this process will continue until the test expression is false.

 // A program to illustrate the For loop in C++

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 11; i++)
{
cout << "Codingtag\n";
}
return 0;
}
Result:

Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag
Codingtag

WHILE LOOP
In while loop, firstly the condition is evaluated and if the condition is true it will execute inside statements and it will be repeated continuously until the condition returns false. Once the condition return false, the control comes out and jump to the further statement after while loop.

Syntax

while(condition)
{
Statement;
}

Where the statement can be single or block and condition can be any expression. When the condition is true, the loop iterates meanwhile, the condition is true. Program control passes the line instantly following the loop when the condition becomes false.

Program to illustrate while loop in C++

#include <iostream>
using namespace std;
int main ()
{
// declaration Local variable:
int j = 14;
// execution of while loop
while( j < 25 ) {
cout << "value of j: " << j << endl;
j++;
}
return 0;
}
Result:

value of j: 14
value of j: 15
value of j: 16
value of j: 17
value of j: 18
value of j: 19
value of j: 20
value of j: 21
value of j: 22
value of j: 23
value of j: 24

DO WHILE LOOP
Do while loop is very similar to while loop. Since a while loop can be used to repeat a block of statements until the condition of the given loop returns false. The prime difference between do-while and while loop is the condition is first evaluated after that statement gets executed in while loop whereas in a do-while loop, the inside statements executed firstly and then the evaluation of condition take place.

Syntax

do
{
Statement(s);
}
while(condition);

Example of Do while Loop

#include <iostream>
using namespace std;
int main(){
   int a = 1;
   do{
      cout<<"The number value is "<<a<<endl;
      a++;
   } while(a <= 6);
   return 0;
}

Results:

The number value is 1
The number value is 2
The number value is 3
The number value is 4
The number value is 5
The number value is 6

NESTED LOOP

When a loop is placed inside the body of another loop is known as nested loop.

while(condition first)
{
statement(s);
while(condition second)
{
statement(s);
... ... ...
}
... ... ...
}

There is various nested loop present in i.e. nested for loop, nested do while loop and nested while loop. A number of loops is based upon the number of complexity of the program.

Following are the example of for nested loop

#include <iostream>
using namespace std;
int main () {
int i, j;
for(i = 6; i<50; i++) {
for(j = 6; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}
return 0;
}
Result:

6 is prime
7 is prime
8 is prime
9 is prime
10 is prime
11 is prime
12 is prime
13 is prime
14 is prime
15 is prime
16 is prime
17 is prime
18 is prime
19 is prime
20 is prime
21 is prime
22 is prime
23 is prime
24 is prime
25 is prime
26 is prime
27 is prime
28 is prime
29 is prime
30 is prime
31 is prime
32 is prime
33 is prime
34 is prime
35 is prime
37 is prime
38 is prime
39 is prime
40 is prime
41 is prime
43 is prime
44 is prime
45 is prime
46 is prime
47 is prime

Share:

Comments

    No Comments Yet.

Leave a Reply