Back to Top

Wednesday 8 April 2015

Begginer's Guide to C++- Chapter 4: Looping

Now that we have learnt to set conditions on variables, what if we want to run a particular code multiple times till a condition is true? In this article we will do just that.


Learnt in Previous Chapter:
  1. if statement
  2. switch statement


Some important terms:
  1. Initialization: Initializes the variable which is to be put in the condition. Normal initialisation rules are followed.
  2. Condition: The loop is run till the condition is true. These conditions are similar to the ones used in if-else statements.
  3. Updation: For the condition to be set to false, the value of the variable in the condition need to be updated in each run of the loop. You will see this in the first example.


while loop:
A particular code inside while loop is executed again and again until a particular condition set by the programmer is valid.
Syntax-
while(Condition)
{Your favorite code;
Updation; //The variable in the condition needs to be updated till the condition is false
}


Eg- I want to print numbers from 1 to 100. Obviously, I will not write a cout<< statement 100 times, this is where loops come into use.


int n=1; //Initialisation
while(n<=100) //Condition
{cout<<n<<’/t’;
n++; //Updation
}


And this will give output as-
1 2 3 …. 100
The loop will run until n is less than or equal to zero.


Q- If I write cout<<n; directly below the above example, what will the value of n be displayed as?
A- 101, as n was incremented until it was <=100, so the condition is dissatisfied when n is incremented to 101, therefore the current value of n will now be 101.


do-while loop:
This is same as a simple while loop, but now instead, this loop does not check the condition in the starting but in the end. You will understand what I am saying by reading the syntax itself.
Syntax-
do
{Your favorite code;
Updation;
}while(condition);


Notice that the condition check is now in the end of the code now. This is helpful in creating menu-driven programs, where you need to run the program, until the user wishes to exit.


Eg-
For a menu like this-
  1. Choice 1
  2. Choice 2
  3. Exit
The loop will look like-
do
{cout<<”1. Choice 1”;
cout<<”2. Choice 2”;
cout<<”3. Exit”;
cin>>choice;
switch(choice)
{//Not writing the code for switch
}
}while(choice!=3);
This will run the loop until the user enters 3 to exit the program. This is extremely efficient and will help you run programs more than 1 time.


for statement:
for loop is exactly like while loop, but the difference lies in the syntax, here all the variables and updates take place in the same place.
Syntax-
for(initialisation;condition;updation)
{Your favorite code;
}


As you see, everything from initialisation to updation is in one place. You can leave field empty.
Eg-
for(int i=0;i<n;i++)
{
}
Now if you want to leave all the fields empty
int i=0;
for(;;)
{if(i<n)
{
}
i++;
}


In the above code I have show you how to skip over any arguement you want, and still have the same code.


Now, we will be practising all the concepts learnt so far till now. I will not post a tutorial, but several programmes in regular intervals, so that you get used to the concepts and also get familiar with looping, as it requires a great deal of time and practice.


I will be posting the next tutorial in approximately 14 days, but you will be getting loads of programs almost every alternate days, so that you attain perfection in the way you think and also implement the programming. I will try to teach you looping in depth, as it is one of the most integral aspects of C++, and I can’t afford anyone not knowing it. So make sure that you check the site every alternate days,for some new techniques and also some programs.


Practice Questions:
Q1. Print alternate numbers starting from 1 till the number user has entered. Report error, if the number entered by the user is even.


Q2. Print fibonacci series till the number user has entered.
0,1,1,2,3,5,8,13,21……..


Q3. Find the factorial value of the number the user has entered.

Q4. Check if the number entered by the user is prime or composite.

Q5. Check whether a number entered by the user is a perfect number or not. (Perfect numbers are those, whose sum of factors is same as the number itself. Eg- 6=1+2+3.)