Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 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();
}
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 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();
}
{
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:
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.
int main()
{
int i;
for(i=2; 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();
}
sum=sum+1/i;
}
cout<<"\nThe sum of the given series is\t"<<sum;
getch();
}
#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;
Jump Statements
The jump statements unconditionally transfer program control within a function.
goto statement
break statement
continue statement
#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;
}
}
Output: