About  Contact


Loops in C++

Introduction:Let us start with an example- suppose you have to write a program to display your name 10 times in 10 lines. One may write the program by using cout<<"Your Name"<<"\n"; 10 times. OK now suppose you have to write a program to display your name 1000 times ( it sounds useless ), do you use the above "cout" statement 1000 times? Here actually we need a way to tell the computer to repeat the "cout" statement 10 times or 1000 times. Here comes the concepts of Loop. Looping statement can solve this problem.
Loop:This statement is used to repeat a block of statement for a specific number of times. Sometime this statement is called iterative statement as well.
Types of loop:In C++ there are three loops
  1. for loop
  2. while loop
  3. do-while loop
Characteristics of looping statement :
  • In every loop there is a variable called looping variable on which loop executes or iterates.
  • Initial value of the looping variable.
  • Looping condition, takes care how long the loop executes.
  • Updation of looping variable, on completion of each cycle it takes a new value
  • Loop body, the block of statement to be repeated
Syntax of for loop
    int i;
    for(i=1; i<=10; i++){
        statement-1;
        statement-2;
        .
        .
   }
 
  • "i" is the looping variable
  • Initial value is 1
  • Looping condition - executes till the value of less than equal to 10
  • The looping variable increases by one in every cycle of the loop (i++ is same as i=i+1)
  • Statement-1, statement-2 .. are constitute the loop body ( statements execute in each cycle of the loop
Syntax of while loop
    int i=1;
    while(i<=10){
        statement-1;
        statement-2;
        .
        .
        i++;
   }
 
Notice that looping variable is initialized before the loop starts. Looping condition is given with while itself. The looping variable is updated as the last statement in the loop body.
Syntax do-while loop
    int i=1;
    do{
        statement-1;
        statement-2;
        .
        .
        i++;
   }while(i<=10);
 
Notice that looping variable is initialized as while loop. The loop starts executing the loop body without checking the looping condition for the first time. Updation of looping variable is done as while loop. Find that looping condition is given at the end of the loop body.

The first two loops i.e "for" and while loop are called entry controlled loop because before it starts executing the loop body it checks the looping condition,if it is true then only it executes the loop body otherwise it skips the loop altogether.

The last loop ie "do-while" loop is called exit controlled loop as it does not checks the looping condition before it executes the loop body but it checks the looping condition on exit.

Notice:From the above mentioned nature of the two types of loop it is clear that if the looping condition is false at the begining then entry controlled loop ( for and while ) will not execute the loop body at all. But exit controlled loop ( do- while ) will execute the loop body once.
In the "for" loop notice that two semicolon (;) is part of syntax. If only these two semicolon is given then the loop will execute infinite times.
In "do-while" loop the last semicolon( after while(i<=10); ) is part of syntax.
If the looping variable is not updated carefully then loop may cycle endlessly.
In "for" if curly brackets "{}" pair is not used then only one statement is considered within the loop. It is better practice to use the brackets always. In others loop the brackets are must.
Let us write the program to display your name 10 times using all the loops
Using for
#include <iostream.h>
void main(){
	int i;
    for(i=1; i<=10; i++){
    	cout<<"Your Name";
    }
}
Using while
#include <iostream.h>
void main(){
	int i=1;
    while(i<=10){
    	cout<<"Your Name";.
        i++;
    }
}
Using do-while
#include <iostream.h>
void main(){
	int i=1;
    do{
    	cout<<"Your Name";.
        i++;
    }while(i<=10);
}