Loops in C++


Before starting about Loops in C++ we have to understand what is statement. A computer program is a set of instructions is known as statement. These statements are executed sequentially one after the other. In this article we are going to talk about loops in C++ . So before we talk about loop we have to identify what is statement. Statement is a single line instruction for a computer. But loop is a multi line instruction for a computer. This is the main difference between statement a loop, Loop and statement is not a different loop is a extended form of statement.

loops in C++

As in our last article we told about what is C++ from where it develop and when, what are the specialities and some basic like tokens data type, how to add two numbers using C plus plus program and extra, if you miss that article you can go from the following link.

Selection or branching statements :

When a programmers are required to execute a particular set of statements depending upon a particular test condition, a branching statement is required.
There are some following branching statements available in C plus plus:

1.If statement

If statement is a conditional statement we put a condition in brackets with if, then if condition satisfied then statement will run otherwise not.
Us take a example:


As given the example there is a condition if if n is equal to 5 then she will be increment by one otherwise not. So this is a if statement

2.If else statement

If else statement is in modified version of if statement in if statement there is only one condition but in if else we can give two conditions or we can say that in if statement if condition will satisfy then the statement will run but if not then program stop working but in if else we give two statements if condition satisfied then first statement will run otherwise second statement will run.
Let us take a example:

cout<<"I Hate Her";
cout<<"I love Her";

NOTE: if loop line will not be terminate

For more Queries visit contact us and if u want to submit your article like this please visit submit your post page.

3. If else if statement

In if else if statement we can give many conditions with many statements here we will give first condition with if if that will not satisfy then next condition with else if will be checked if that will be satisfied the statement will run otherwise next else if will be bothered.
Let us take a example for more clarification.

cout<<"I will Fail";
else if(n=2)
cout<<"I can pass";
else if(n=3)
cout<<"I can pass or not";
cout<<"surely I am fail";

NOTE: if loop line and else will not be terminate

4. Nested if statement

From the name it’s clear. This statement is like in nest a wire under a wire will make a nest same here if statement then inside that if statement there will be one more if statement that is called as a nested if statement.
Let us take example for more clarification:

if(condition 1)
 statement 1;
 if(condition 2)
 statement 2;
statement 3;

NOTE: if loop line and else will not be terminate

5. Switch statement

This statement will give a alternative to if else if statement it is a branching statement. In this statement take any expression and set it values if that value will come then the connected statement will be Run and in the syntax of this statement there is a one key word which is break which means that stop after every case we put a break keyword which stop the program execution.
Let us take example for more clarification:

 case a=1:
 case a=2:
 case a=n:

        cout<<"Sorry no match";

Note that in the program I set a default value which is that if not any one case will be satisfied then this value will be Run.

Loops in C++

Sometimes there is a need to be repeat some statements or some group of statements. So iteration statements allow to do this… IIteration Lopps in C++ will run till the loop test will be false. Following are iteration loops in C++ :

1. For loop

In the iteration loops in C++ there is one for loop, for loop is used to set a condition for finite period to be repeat. This loop is called an entry control loop because condition is checked before entering into the scope of the loop. We use this loop only when we know that how many times we have to repeat that statement

for loop in C++

Let us take a example:

for(n=1; n=100; n++)
 cout<<"I  Love Programming";
/* here at n=1 i am initialising the value from where the loop will be start, n=100 this is the test point of the loop here loop is checking that it has to begun or not or it is a condition Point and  at n++ it is a update Point or increment Or decrement point.
So this loop will be Run for 100 times it will start from 1 and stop at 100.
This whole code will be paint I love programming for hundred time.*/

2. While loop

While loop is also iteration loops in C++ . In the while loop this loop will be Run till the condition is not going to be false.

while loop in C++

Let us take a example:
We declare a variable int a= 200; and set a condition in while loop that until a not achieve a value 100 loop will run:


/* in this code the value of a will be printed on the screen till a will be equal to 100 every time a will be decreased by 1 */

3. Do while loop

Do while loop is also iteration loops in C++ . This loop allow its body of instruction to Execute one or more time. A do while loop always execute at least one. The body of the do statement continues to execute as long as a result of the condition in the while statement remains true. This is often called as exit control loop. The do while loop is the flip side of a while loop.
There is only one difference between while and do while and that is do while statement execute the statement first then evaluate condition, but while is vice versa.
See the syntax of do while loop:


4. Nested loops

If we add a for loop inside a for loop then that loop will be called as nested loop.

int i,j;
for(i=1; i<=3; i+=2)
 for(j=1; j<=1; j++)
 cout<<i<<" "<<j<<endl;

Output will be:
1 1
3 1

Jump statements

Jump statements are also come in loops in C++ . These statements unconditionally e transfer the program control within a function. In C plus plus there are two types of jump statements first one is goto statement and second is continue statement.

Goto Satement

This statement is used to alter the program execution sequence by transferring the control to some other part of the program.
See the syntax and example of the program:

void main()
 int a,b;
 cout<<"Enter first number=";
 cout<<"Enter second number=";
 goto large;
 goto small;
 large: cout<<"larget is="<<a;
 goto end;
 small: cout<<"smallest is="<<b;
 end: cout<<endl;

Continue statement :

Continue statement is also a part of loops in C++ . In this statement jumps over all the rest of the statements in the loops block and goes to the next statement after the loop. The continue statement does the same thing that, instant of terminating the loop, it goes back to the beginning of the loop’s block to continue the iteration and across the rest of the loop.
See the example and send text of the continue statement:

void main()
 int n,count,number=0;
 float x,avg,sum=0;
 cout<<"How many numbers=";
 for(count=1; count<=n; ++count)
  sum=sum + x;
 cout<<"The average is ="<<avg;

This the all about the Iteration Loops in C++ . Loops in C++ Offer mainly for and while loop. Loop basically mean that anything which is repeating. So, for that in C++ we have only two loops in C++. I hope from this section you learn good about C++ well Loops in C++ statements in C++, Selection and Branching statements. I also hope that By this article you also clear by difference of loops in C++ and Statements.

In Our next C++ article we will elaborate about functions in C++ .

One word, before we finish. We shall be grateful for your feedback. It will help us improve our services. Thanks in advance!

Leave a Reply

error: Content is protected !!