Do-While Loop
Do-While Loop
Do-While Loop
true false
statement
Write a program that display counting from 1
to 1 10 using loop while loop
#include<stdio.h>
#include<conio.h>
int main()
{
Int n;
n=1;
while(n<=10)
{
printf("%d\n",n);
n++;
}
getch();
}
DO-WHILE LOOP
Q: What is do-while Loo?
Do
{
Statement 1;
Statement 2;
.
.
Statement N;
}
While (condition);
Syntax Explanation
Loop Body
T T
Conditio
n
F
Difference of While & Do-While Loop
Increment /
Decrement
Condition
T Loop Body
F
Write a program that display counting
from 1 to 5 using for loop
#Include <stdio.h>
#Include <conio.h>
void main ()
{
{ Int n;
n=1;
for (n=1, n<5, n++)
printf (“%d\n”,n);
getch ();
}
Nested Loop
Loop with in a loop is called nested loop
Outer Loop
Syntax
For (initialization; condition; increment/decrement )
{
For (initialization; condition; increment/decrement )
{
Inner Loop Statement ();
}
}
Flowchart
Types of nested loop
while (condition1) {
statement (s);
while (condition2)
{
statement(s);
... ... ...
}
... ... ...
}
Nested do-while loop
A do-while loop inside another do-while loop is called nested do-while loop
} while (condition-2);
// body of outer while loop
} while (condition-1);
Nested for loop
A for loop inside another for loop is called nested for loop.
while (condition-1)
{
// body of outer while loop
while (condition-2)
{
// body of inner while loop
}
// body of outer while loop
}