Professional Documents
Culture Documents
Programming
Toqeer Mahmood
Dept. of Civil Engg.
UET, Taxila
The “for” Loop
“for” loop is used to execute a set of statements
repeatedly for a fixed number of times.
It is also known as a counter loop.
Syntax:
for(initialization; condition; increment/decrement)
Statement;
Syntax:
while (condition)
statement;
while (condition)
{
statement(s);
}
The “do-while” loop
The “do-while” loop is also a conditional loop
statement.
It is like a while loop, but in this loop the condition is
tested after executing the statements of the loop.
Syntax:
do
{
statement/s ;
}
while (condition);
Lecture
#
08
Nested Loops
Loop/s inside the loop is known as Nested
Loop
Nested “for” Loop
Syntax:
for(initialization; condition; increment/decrement)
{
Statement/s;
Statement/s;
}
Nested “while” Loop
Syntax:
while (condition)
{
Statement/s;
while (condition)
{
statement/s;
}
Statement/s;
}
Nested “do-while” loop
Syntax:
do
{
Statement/s;
do
{
statement/s ;
}
while (condition);
Statement/s;
}
while (condition);
Program
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int n, r;
getch();
}
Program
Write a program to print the output as shown below
* #include<conio.h>
* * #include<iostream.h>
* * * void main()
* * * * {
* * * * * clrscr();
* * * * * * int i, j;
cout<<endl;
}
getch();
}
Program
Write a program to print the output as shown below
* #include<conio.h>
*** #include<iostream.h>
***** #include<iomanip.h>
******* void main()
{
clrscr();
int s, i;
getch();
}
Lab work
Write a program to print the output as shown below using while or do-while loop
*
* *
* * *
* * * *
* * * * *
* * * * * *
Write a program to print the output as shown below using while or do-while loop
* * * * * *
* * * * *
* * * *
* * *
* *
*
Write a program to print the output as shown below using while or do-while loop
*******
*****
***
*