Sei sulla pagina 1di 18

Looping statement

It is also called a Repetitive control structure. Sometimes we require a set of statements to be


executed a number of times by changing the value of one or more variables each time to obtain a
different result. This type of program execution is called looping. C++ provides the following
construct

while loop
do-while loop
for loop

While loop
Syntax of while loop
while(condition)
{
statement(s);
}

The flow diagram indicates that a condition is first evaluated. If the condition is true, the loop
body is executed and the condition is re-evaluated. Hence, the loop body is executed repeatedly
as long as the condition remains true. As soon as the condition becomes false, it comes out of the
loop and goes to the statement next to the while loop.
Write a program to print DPSMIS 5 times by using While loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
while(i<=5)
{
cout<<"\nDPSMIS";
i++;
}
getch();
}

Write a program to print first 5 natural numbers by using While loop.


#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
while(i<=5)
{
cout<<i<<"\n";
i++;
}
getch();
}

Write a program to print all the even numbers between 1 to 10 by using while loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=2;
while(i<=10)
{
cout<<i<<"\n";
i=i+2;
}
getch();
}

Write a program to print all the odd numbers between 1 to 10 using while loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<"\n";
i=i+2;
}
getch();
}

Write a program to print the following series :


0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
int a=0,b=1,c,i=1;
cout<<a<<" "<<b;
while(i<=23)
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
i++;
}
getch();
}

Write a program to print the sum of digits of a given number.


#include <iostream.h>
#include <conio.h>
int main()
{
int num,a,sum=0;
cout<<"\nEnter the number\t\t\t";
cin>>num;
while(num>0)
{
a=num%10;
sum=sum+a;
num=num/10;
}
cout<<"\nThe sum of digits of the number is\t"<<sum;
getch();
}

Write a program to reverse the number.


#include <iostream.h>
#include <conio.h>
int main()
{
int num,a,rev=0;
cout<<"\nEnter the number\t";
cin>>num;
while(num>0)
{
a=num%10;
rev=rev*10+a;
num=num/10;
}
cout<<"\nThe Reverse number is\t"<<rev;

getch();
}

do-while loop
Syntax of do-while loop
do
{
statements;
} while (condition);

Note : That the loop body is always executed at least once. One important difference between
the while loop and the do-while loop the relative ordering of the conditional test and loop body
execution. In the while loop, the loop repetition test is performed before each execution the loop
body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop
termination test is Performed after each execution of the loop body. hence, the loop body is
always executed atleast once.
Write a program to print DPSMIS 5 times by using DoWhile loop.

#include <iostream.h>
#include <conio.h>
int main()
{

int i=1;
do {
cout<<"\nDPSMIS";
i++;
}while(i<=5);
getch();
}

Write a program to print first 5 natural numbers by using While loop.


#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
do
{
cout<<i<<"\n";
i++;
}while(i<=5);
getch();
}

Write a program to print all the even numbers between 1 to 10 by using dowhile loop.

#include <iostream.h>
#include <conio.h>
int main()
{
int i=2;
do
{
cout<<i<<"\n";
i=i+2;
}while(i<=10);

getch();
}

Write a program to print all the odd numbers between 1 to 10 by using dowhile loop.
#include <iostream.h>
#include <conio.h>

int main()
{
int i=1;
do
{
cout<<i<<"\n";
i=i+2;
}while(i<=10);

getch();
}

Write a program to print the following series :


0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
int a=0,b=1,c,i=1;
cout<<a<<" "<<b;
do

{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
i++;
}while(i<=23);
getch();
}

for loop
It is a count controlled loop in the sense that the program knows in advance how many times the
loop is to be executed.
syntax of for loop
for (initialization; decision; increment/decrement)
{
statement(s);
}

The flow diagram indicates that in for loop three operations take place:

Initialization of loop control variable


Testing of loop control variable
Update the loop control variable either by incrementing or decrementing.

Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test
whether the condition is true or false. If the condition is true, the program executes the body of
the loop and then the value of loop control variable is updated. Again it checks the condition and
so on. If the condition is false, it gets out of the loop.

Write a program to display name five times.


#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=5; i++)
cout<<\nDPSMIS;
getch();
return 0;
}

Write a program to find first 10 natural numbers.


#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
cout<<"\n"<<i;
getch();
}

Write a program to input all the even number between 1 to 10.


#include <iostream.h>
#include <conio.h>

int main()
{
int i;
for(i=2; i<=10; i+=2)
cout<<"\n"<<i;
getch();
}

Write a program to input all the odd numbers between 1 to 10.


#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=10; i+=2)
cout<<"\n"<<i;
getch();
}

Write a program to find the sum of all the even numbers between 1 to 100.
#include <iostream.h>
#include <conio.h>
int main()
{
int i, sum=0;
for(i=2; i<=100; i+=2)
{
sum=sum+i;

}
cout<<"\nSum\t"<<sum;
getch();
}

Write a program to input a number and find its factorial.


#include <iostream.h>
#include <conio.h>
int main()
{
int i,n,fact=1;
cout<<"\nEnter the number\t\t";
cin>>n;
for(i=1; i<=n; i++)
{
fact=fact*i;
}
cout<<"\nFactorial of\t"<<n<<"\tis\t"<<fact;
getch();}

Write a program to find the sum of the following series :


1, 1/2, 1/3, . . . 1/15
#include <iostream.h>
#include <conio.h>
int main()
{
float sum=0,i;
for(i=1; i<=15; i++)
{

sum=sum+1/i;
}
cout<<"\nThe sum of the given series is\t"<<sum;
getch();
}

Write a program to display the given series :


0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
int a=0,b=1,c,i;
cout<<a<<" "<<b;
for(i=1; i<=23; i++)
{
c=a+b;
cout<<c<<" ";
a=b
b=c;
}
getch();
}

Print the following pattern :

#include <iostream.h>
#include <conio.h>
int main()
{
int i,n;
cout<<"\nEnter the value of n\t";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<"*\n";
}
getch();
}
Print the following pattern :

#include <iostream.h>
#include <conio.h>
int main()
{
int r,c,i,j;
cout<<"\nHow many rows and columns ?\t";
cin>>r>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<"*";
cout<<"\n";

}
getch();
}

#include <iostream.h>
#include <conio.h>
int main()
{
int n,i,j;
cout<<"\nEnter the value of n\t";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n";
for(j=1;j<=i;j++)
cout<<"*";
cout<<"\n";
}
getch();
}

#include <iostream.h>
#include <conio.h>
int main()
{
int n,i,j;

cout<<"\nEnter the value of n\t";


cin>>n;
for(i=n;i>0;i--)
{
cout<<"\n";
for(j=i;j>0;j--)
cout<<"*";
cout<<"\n";
}
getch();
}

Jump Statements
The jump statements unconditionally transfer program control within a function.

goto statement
break statement
continue statement

The goto statement


goto allows to make jump to another point in the program. goto pqr;
pqr: pqr is known as label. It is a user defined identifier. After the execution of goto statement,
the control transfers to the line after label pqr.
The break statement
The break statement, when executed in a switch structure, provides an immediate
exit from the for, while, do-while, or switch statement. Similarly, you can use the break
statement in any of the loop. When the break statement executes in a loop, it immediately exits
from the loop.

#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for (i=1; i<10;i++)
{
cout<<i;
if (i>5)

break;
cout<<"Next"<<"\n";
}
getch();
return 0;
}

When a continue statement is encountered inside any c++ loop, control


automatically passes to the beginning of the loop. The continue statement can be
included within a while, a do-while or a for statement.
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for (i=1; i<10;i++)
{
cout<<i;
if (i>5)
{
cout<<\n;
continue;
}
cout<<"Next"<<"\n";
}
getch();
return 0;

}
Output:

The exit ( ) function


The execution of a program can be stopped at any point with exit ( ) and a status code can be
informed to the calling program. The general format is
exit (code) ;
where code is an integer value. The code has a value 0 for correct execution. The value of the
code varies depending upon the operating system.

Potrebbero piacerti anche