LOOP in C++
×

LOOP in C++

3567

Looping is used in 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.

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 no 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.

Two types of the loop are available in the 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 "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;

The 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.

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 a 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 returns false, the control comes out and jumps to the further statement after a 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 a while loop whereas, in a do-while loop, the inside statements executed firstly, and then the evaluation of condition takes place.

Syntax:

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

Example:

#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 a nested loop.

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

There are various nested loop present in i.e. nested for loop, nested do-while loop, and nested while loop.

A number of loops are based upon the complexity of the program.

Example:

#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



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments