Lecture 8
Lecture 8
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming language checks its condition at the bottom of
the loop.
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested. If the condition
is true, the flow of control jumps back up to do, and the statement(s) in the loop
execute again. This process repeats until the given condition becomes false.
Flowchart:
1
Example;
#include <stdio.h>
int main (void)
{ Output:
int a,b;
printf ("Enter first value");
scanf ("%d", &a);
printf ("Enter second value");
scanf ("%d", &b);
do
{
printf ("%d\n",a) ;
a=a+1;
}
while (a<b);
return 0;
}
1.The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If
it is false, the body of the loop does not execute and flow of control jumps to the next
statement just after the for loop.
3. After the body of the for loop executes, the flow of control jumps backup to the
increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.
4. The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again condition).
After the condition becomes false, the for loop terminates.
2
Flowchart
Example;
#include <stdio.h> Output:
int main (void)
{
int a=1;
for (a=1; a<10; a++)
{
printf ("%d\n", a);
}
return 0 ;
}