Loops

In order to perform repeated task we use loops i.e. to execute a set of statements repeatedly or  a number  times we use loops. These are very important in C++ .complex programs contain these loops almost every were. Loops generally have a conditional statement.

So basically let us consider your name being printed four times that is easy to perform as we can write he cout statement four times but generally when we use loops in C++ we generally do not know the number of times we have to repeat the loop or maybe we need to print the statement 10000 times in that case we cannot write these statements 10000 times.

A loop is a statement which repeats a set of statements until a condition is met. These set of statement are noting but are the statement enclosed in the loop.

There are basically three main types of loops

While loop

Do-while loop

For loop

First we will take on the while loop

 

while

Syntax:

 

while(condition)

{

Statements;

}

 

Here the condition is the conditional statement which is checked every time the loop has to be executed if the condition is true the loop is executed and if the condition is false then the loop is not executed that means the statement s in side the lop are not executed and the control passes to the statement after the while loop. Here is an example:

 

#include <iostream.h>

void main()

{

int a,b=0;

cout<<”enter the number:”;

cin>>a;

while(b<a)

{

cout<<”sameer”;

b++;

}

}

This program prints the string sameer a number of times. Here we need to understand the meaning of the statement b++ which is actually incrementing the value of a or to be more specific increases the value of a by one.

Here the condition b<a is checked before entering the loop the condition will be true only till b is just one less than a. The logic of the program is that the value of b will change from 0 to a-1 .so the number of times it value changes is a so sameer will be printed a times. Logic of the program is very important so before learning or writing the program please try to make the logic of the program because without it the program cannot be made. Here it is to be noted that if the value of a is entered 0 then the loop will not be executed even ones this a advantage for some program but is a disadvantage for some program but this feature is knocked out by the following loop.

 

Do-while

Syntax

do

{

Statements

}while(condition)

Example

#include <iostream.h>

void main()

{

int a,b=0;

cout<<”enter the number:”;

cin>>a;

do

{

cout<<”sameer”;

b++;

}

while(b<a)

}

This program is same as the above but only thing is that it has a do-while loop

This is basically an exit controlled loop that means that the condition is checked after the loop executes and depending on the condition the next repetition is done.

The added advantage of this loop is that if the user enters 0 in the above program then also the string will be printed once as the condition will be checked after the statement is executed difference between the while loop and this loop is that in the first one the statement might not be executed at all (if the condition is false from the very beginning), while in the second it is always executed at least once. So if you want the statement to be executed at least once than we you do-while loop.

 

for

for loop is the most important as well as the most difficult loop to handle but the features that this loop offer is not met by any other loop of course most of the time we can replace for by while loop but using for is easy and it also has added advantage like easy debugging and better data handling.

Syntax:

for(initialization ; condition ; incrementation or decrementation)

{

Statements;

}

Initialization is the place were we can initialize any variable generally th variable which we use is initialized here. Initialization we have already learnt I will not explain it again. You will see how to use this in the example explanation.

Incrementation and decrementaton is the place where we can put the increment and decrement command of the variable.

Concentrate on the below example because the for loop is very important

#include<iostream.h>

void main()

{

int num,fact=1;

cout<<”enter the number:”;

cin>>num;

for(int i=1;i<=num;i++)

{

fact=fact*i;

}

cout<<”factorial =”<<fact;

}

This program prints the factorial of the input number. First we enter the number and initialize the value 1 to the fact variable. The logic of the program is that we will multiply the fact with all the number from 1 to the number whose factorial we need to find. So we initialize a number i in the for loop and we increment the value of I from 1 to num and multiply it to fact and store the result in fact simultaneously as show in the for loop .here we observe that the  for statements are executed repeatedly and incrementation of the variable takes place automatically at the end of the program.

So basically following things happen in for loop:

When the control reaches the for loop the initialized value is stored as the command then the condition is checked and accordingly the loop is executed .after the control reaches the end of the loop the incrementation  is done and the condition is checked again and accordingly the loop is again executed and then again at the end the incrementaion is done and the condition checked. These steps are followed till the condition becomes false once after which the loop is exited and control passes to the statements following the for loop. It is to be noted that if the condition is false in the very first execution then also the initialization is executed.

 

Jump statements

These statements are used to transfer the control from point to a different place in the program. These can be used by an able programmer to play with the control of the program. These are very important for making a program because a loop all alone can be used to make a simple program but in order to make a complex program we need to make excellent use of these jump statement.

Their are three types of jump statement they are:  

 

goto statement

goto is a command that can transfer the control from any point to any possible point of the program. It has a goto statement and a label.

Syntax:

goto label;

This statement is used to tell the compiler to jump the control from this point to the point were we place the label. Placing label is very easy as we can simply place label in the following way.

Label:

Label is any valid identifier (name) in C++ as we have discussed before.

It is to be noted that generally a professional programmer does not use goto statement because while debugging it becomes very frustrating and confusing for the programmer who debugs the program. It becomes very difficult to find the label of the program and then continue the execution from their.

 

break

Break statement is used to exit the loop as soon as this statement is executed that means this statement exits the loop in which it is used even though the condition in the loop is true.

For example:

#include<iostream.h>

void main()

{

int a;

cout<<”enter the number:”;

cin>>a;

for(int i=0;i<=a;i++)

{

if(a<0)

break;

cout<<i;

}

}

In this program the number a is entered and number from 1 to a are displayed on the screen but if the user enters 0 then the if statement is executed and the condition becomes true and the break statement is executed .as soon as the break statement is execute the control leaves the loop even though the condition is true. Here we should understand oone thing that the loop is exited and the next incrementation is not done so value of I in the above program remains 0 even after the loop is exited.

 

continue

This statement performs just the opposite task as performed by the break statement in the above explanation .continue statement performs the next incrementation on the loop ignoring all the statements after the continue statement that means the statements after the continue statement are not executed and as soon as ther control reaches the end of the for loop the next incrementation is done and the condition is checked and accordingly the loop is further executed.

For example:

#include<iostream.h>

void main()

{

int a;

cout<<”enter the number:”;

cin>>a;

for(int i=0;i<a;i++)

{

if(i%2==0)

continue;

cout<<I;

}

}

The following program prints all the odd numbers from 0 to a .a is entered by the user. In this program every time the value of I is even the if loop is executed so the continue statement is executed which increments the value of I but does not execute these cout statement as it follows the continue statement.

 

Now that you have learnt these C++ concepts its time for you to write programs of your own and if you want any help just put your program request and questions in the program request. Yes you can put any theory question you have in the program request page.

 

Make a Free Website with Yola.