Professional Documents
Culture Documents
in C++
What is Repetition Structure?
Forloop
While loop
Do-while loop
Importance of loops…
}
While loops
Syntax: Example: This program will print
out the number from 1-5.
While (condition) #include <iostream.h>
{ main()
Statement1; {
Statement2; int ctr;
Statementn; ctr=1;
} while(ctr<=5)
{
cout<<“ \n”<<ctr;
++ctr;
}
}
What is wrong in the program?
Example: This program will print out the number from 1-5.
#include <iostream.h>
main()
{
int ctr;
ctr=1;
while(ctr<=5)
{
cout<<“ \n”<<ctr;
}
}
Do-while loops
Syntax: Example: This program will
print out all even nos. from
do{ 1-10.
#include <iostream.h>
main()
Statement1;
{
Statement2;
int ctr;
Statementn;
}while(condition); ctr=1;
do{
cout<<“ \n”<<ctr;
++ctr;
}while(ctr<=10);
What do this program display?
Example: This program will print out all even nos. from 1-10.
#include <iostream.h>
main()
{
int ctr;
ctr=1;
do{
++ctr;
cout<<“ \n”<<ctr;
} while(ctr<=10);
While vs. Do-while
#include <iostream.h>
main()
{
int ctr;
for(ctr=1;ctr<=5;++ctr)
{
cout<<“siena\n”;
}
}
Using three kinds of loops, Create a program
to display All ODD numbers from 0 -15 in
decreasing order.
Ex:
– 15 13 11 9 7 5 3 1