by Dinesh Thakur Category: Control Structures

The statements that cause a set of statements to be executed repeatedly either for a specific number of times or until some condition is satisfied are known as iteration statements. That is, as long as the condition evaluates to True, the set of statement(s) is executed. The various iteration statements used in C++ are for loop, while loop and do while loop.

 

The for Loop

 

The for loop is one of the most widely used loops in C++. The for loop is a deterministic loop in nature, that is, the number of times the body of the loop is executed is known in advance.

The syntax of the for loop is

for(initialize; condition; update)

{

//body of the for loop

}

Note that initialize, condition and update are optional expressions and are always specified in parentheses. All the three expressions are separated by semicolons. The semicolons are mandatory and hence cannot be excluded even if all the three expressions are omitted.

To understand the concept of the for statement, consider this example.

Example: A program to display a countdown using for loop

 

#include<iostream>

using namespace std;

int main ()

{

int n;

for(n=l; n<=10; n++)

cout<<n<<" "; // body of the loop

cout<<"\n This is an example of for loop!! !";

                              / /next statement in sequence

return 0;

}

The output of this program is

1 2 3 4 5 6 7 8 9 10

This is an example of for loop!! !

 

for loop using comma operator: for loop allows multiple variables to control the loop using comma operator. That is, two or more variables can be used in the initialize and the update parts of the loop. For example, consider this statement.

                                 for (i=1,j=50 ;i<10;i++, j- -)

 

This statement initializes two variables, namely i and j and updates them. Note that for loop can have only one condition.

The while Loop

 

The while loop is used to perform looping operations in situations where the number of iterations is not known in advance. That is, unlike the for loop, the while loop is non deterministic in nature.

The syntax of the while loop is

while(condition)

{

/ / body of while loop

}

These points should be noted about the while loop.

• Unlike for loops where explicit initialize and update expressions are specified, while loops do not specify any explicit initialize and update expressions. This implies that the control variable must be declared and initialized before the while loop and needs to be updated within the body of the while loop .

• The while loop executes as long as condition evaluates to True. If condition evaluates to False in the first iteration, then the body of while loop never executes.

• while loop can have more than one expression in its condition. However, such multiple expressions must be separated by commas and are executed in the order of their appearance.

To understand the concept of the while loop, consider this example.

Example : A program to determine the sum of first n consecutive positive integers

#include<iostream>

using namespace std;

int main ()

{

int n,i,sum; // i is the control variable

cout<<” Enter the number of consecutive positive”

<< “\n integers(starting from 1): “;

Cin>>n;

sum=0;

i=l;      // initialize expression

while (i<=n)

{

sum+=i;

++i; //update expression

}

Cout<<”\nThe sum is "<<sum;

 

return 0;

}

 

The output of the program is

 

Enter the number of consecutive positive integers(starting from 1): 9

The sum is : 45

 

The do-while loop: in a while loop, the condition is evaluated at the beginning of the loop and if the condition evaluates to False, the body of the loop is not executed even once. However, if the body of the loop is to be executed at least once, no matter whether the initial state of the condition is True or False, the do-while loop is used. This loop places the condition to be evaluated at the end of the loop.

The syntax of the do-while loops is given here.

Do

{

//body of do while loop

}while(condition) ;

 

To understand the concept of do-while loop, consider this example.

 

Example : A program to calculate the sum of an Arithmetic Progression (AP)

 

#include<iostream>

using namespace std;

int main ()

{

int a,d,n,sum,term=0; /*a is the first term , d is

the common difference, n is the number of terms to be summed */

cout<<"Enter the first term, common difference,"

<<"and the number of terms to be summed"

<<"respectively:\n";

Cin>>a>>d>>n;

sum=0;

int i=l;

cout<<"\nThe terms are ";

do //do-while loop

{

term= a+ (i-1)*d;

sum+=term; //Adding each term to 'sum'

cout<<term<<" “;

++i;

 

}

while (i<=n) ;

cout<<"\nThe sum of A.P. is "<<sum;

return 0;

}

 

The output of the program is

 

Enter the first term, common difference, and the number of terms to be summed respectively:

3

6

5

The terms are 3 9 15 21 27

The sum of A.P. is 75

Note that, all the three loops (for, while and do-while) can be nested within the body of another loop



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles