Skip to main content

Featured

functions in Python

Loops in C++ Programming


Loops in C++

A loop means “executing some set of code repeatedly till a certain no of times”. For Example if we want to print the table of 3, we can make the program using loop. Why? Because in printing the table of 3, we have to start with 3 and then repeatedly add 3 till we get 30. So we can say this is a program of loop.

There are two categories of loop in C++ Programming:

·        Entry Controlled

o   While Loop

o   For Loop

·        Exit Controlled

o   Do…While Loop

A Loop in C++ has essentially 3 parts:

·        Initialization of Loop Variable

·        Test Condition

·        Updating /Re-Initialization  of Loop Variable

Let us understand the concept of these 3 parts by following examples:

// Program to print the table of 3 using While Loop

Line 1.           #include<iostream.h>

Line 2.           void main()

Line 3.           {

Line 4.           int a=3;                                  //Initialization of loop variable 

Line 5.           while(a<=30)                       //Test Condition

Line 6.           {

Line 7.           cout<<a<<endl;

Line 8.           a=a+3;                                   ////Re-initialization of loop variable

Line 9.           }

Line 10.      }

 

Code Explanation:

At Line 4 we start with the value of Loop Variable as 3

At Line 5 condition is checked for “a” to be either less than or equal to 30. If the condition is true, the program enters the loop body which starts at Line 6 and ends at Line 8. Here the condition is true so Line 7 and Line 8 executes and again the program reaches at Line 5 to check the condition with updated value of loop variable “a”. This process continues till we get value of “a” as 33 which ends the loop because condition becomes false.

 // Program to print the table of 3 using For Loop

For Loop Program

Code Explanation:

Line 4: In for loop all three parts of while loop come into one place as we can see at line 4.  We start with the value of Loop Variable as 3 as Step 1 (Marked in Red). Then condition is checked for “a” to be either less than or equal to 30 Step 2(Marked in Red).  If the condition is true, the program enters the loop body which starts at Line 5. Here the condition is true so Line 6 executes and again the program reaches at Line 4 Step 4 (Marked in Red). Now again the program reaches Step 2 to check the condition with updated value of loop variable “a”. This process of Step  2-3 -4 continues till we get value of “a” as 33 which ends the loop because condition becomes false.   

  
// Program to print the table of 3 using Do…While Loop

Line 1.           #include<iostream.h>

Line 2.           void main()

Line 3.           {

Line 4.           int a=3;                                  //Initialization of loop variable 

Line 5.           do

Line 6.           {

Line 7.           cout<<a<<endl;

Line 8.           a=a+3;                                   //Re-initialization of loop variable

Line 9.           } while(a<=30);                   //Test Condition

Line 10.      }

 
Code Explanation:

At Line 4 we start with the value of Loop Variable as 3

At Line 5 do statement causes the program to enter the loop body (from Line 6 to Line 9) without any condition check. Statement 7 and 8 execute and then at Line 9 the updated value of “a” is checked to be either less than or equal to 30. If the condition is true, the program enters the loop body again which starts at Line 6 and ends at Line 9. This process from Line 9 to Line 6,  Line 7, Line 8 continues till we get value of “a” as 33 which ends the loop because condition becomes false.

 
We get the output as following from each program explained above:

3

6

9

12

15

18

21

24

27

30

 

Now we understand the difference among these 3 loops

 What if we initialize the variable a with the value 100?

There will be no output in while loop and for loop because the condition becomes false at the entry of the loop and loop does not execute even a single time. That’s why while and for are called “Entry Controlled” Loop.

But in case of do….while loop,  the condition is checked at the end of the loop at Line 9 and so the loop executes 1 time even though the condition is false. SO we can say that “do..while loop executes at least 1 time compulsorily”.

Do..while LOOP is most suited for Menu Driven Programs when we definitely want to display a Menu to the user.  

Comments

Popular Posts