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
Shopping For C++ Books Information Technology
0 Comments