What is Looping? Looping statements used in c++ language

Spread the love

Looping: When a single statement or a group of statements are executed again and again in a program then such type of processing is called loop.
Entry controlled loop: In the such entry loop,first of all condition will be checked, if it is true,then body of loop will be executed otherwise the control will be moved out of the loop i.e. we can exit from the loop.

Exit controlled loop

In the exit controlled loop, first of all body of loop wil be executed and then condition will be checke. If condition is true,then again body of the loop will be executed. If the codition is false,then control will move out from out from the loop. Example of exit controlled loop is do statement or do-loop.

 Looping statements used in c++ language are

       1.       While statement or while loop.
       2.       Do statement or DO loop.
       3.       For statement or For loop.
       4.       Nesting of loops.

1. While loop:-

While statement or while loop is any entry controlled loop. In this first of all condition is checked and if it is true then group of statements or body of loop will is true then group of statements
or body of loop will be executed. It will executed again and again till the condition becomes false. The general syntax is:-
While(test condition)
{
Block of statement;
}
Statement-x;
# Program to find the average of N numbers using while loop.
#include<isotream.h>
#iinclude<conio.h>
Void main()
{
Int i, sum,n;
Float av;
Clrscr();
Cout<<”n enter the value of n:”;
Cin>>n;
Sum=0;
i=1;
while (I<=n)
{
Sum=sum+i;
i=i+1;
}
av=(float)sum/n;
cout<<”n sum is:<<sum;
cout<<”n average is:”<<av;
getch();
}
2. do statement or do-while loop:-

Do statement is exit controlled loop. It is also called do-while statement. In this statement, first body of the loop is executed and then the condition is checked. If condition is true, then the body of the loop will be executed. When condition becomes false, then it will exit from the loop.
The systax of do-while loop is as follow:-
do
{
Block of statement;
}
While(condition);
Statement-x;

# Program to find the average of N numbers using do-while loop


#include<iostream.h>
#include<conio.h>
Void main()
{
Int sum,n,i;
Float av;
Cout<< enter the value of n:”;
Cin>>n;
Sum=0;
I=1;
do
{
Sum=sum+i;
i=i+1;
}
while(I<=n);
cout<<”n sum is:”<<sum;
av=(float)sum/n;
cout<<”n average is:”<<av;
getch();
}
Output:-
enter  the value of n:=10
sum  is: 55
average is : 5.5
3. for statement or for loop

It is  a looping statement, which gets reapted again and again upto a defined condition it is one step loop, which initializes, checks the condition and increaments / decreaments in a single statement.
The general syntax is :-

for (nitial value;test condition; increament/decrement)
{
body of the loop;
}
Statement-x;
      4.    Nested for statement:
When for statement is executed with in another for statement  , then it is called nested for statement. We can apply number of nested for statement  in  c++ language.
The general syntax is:-
for( intial  value 1; test condition1; increments/decrements 1)
{
for (intial  value2;  testcondition2;   increments2/decrement2)
{
inner-body of the loop;
}
Outer-body of the loop;
}

Statement-x;

Shopping For C++ Books Information Technology

Design by Mohit Bhardwaj

MOHIT BHARDWAJ

Hello Visitors, myself Mohit Bhardwaj working in blogging sector since last 8 years. my qualification in bachelor degree in computer science steam. contact Number: 80910-51002

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Our Visitor

003588
Users Today : 17
Users Yesterday : 21
Users Last 7 days : 129
Users Last 30 days : 524
Users This Month : 515
Users This Year : 3587
Total Users : 3588
Views Today : 29
Views Yesterday : 191
Views Last 7 days : 522
Views Last 30 days : 1753
Views This Month : 1733
Views This Year : 9350
Total views : 9351
Who's Online : 0
error: Content is protected !!