2. INTRODUCTION
▪ A do-while loop is similar to a while loop, except
the fact that it is guaranteed to execute at least one
time.
▪ Unlike for and while loops, do-while loop in C
programming checks its condition at the bottom
of the loop .
2
7. • #include <stdio.h>
• int main () {
• /* local variable definition */
• int a = 10;
• /* do loop execution */
• do {
• printf("value of a: %dn", a);
• a = a + 1;
• } while ( a < 20 );
• return 0;
• }
Linear Search
code in C
7
8. Output
8
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do {
printf("value of a: %dn", a);
First statement always gets
printed before checking the
condition even if condition is
wrong.
a = a + 1;
The value is incremented by 1
each time.
while ( a < 20 );
return 0;
}
After checking the condition,
other statements gets printed
based on condition value we
have given. (a < 20)
9. Comparison of
while loop and
do-while loop
// Example Using While-Loop
#include <stdio.h>
int main() {
int i=0;
while(i==1) {
printf("while vs do-while");
}
printf("Out of loop"); }
Output:
Out of loop
// Same example using do-while loop
#include <stdio.h>
int main() {
int i=0;
do {
printf("while vs do-whilen");
}while(i==1);
printf("Out of loop"); }
Output:
while vs do-while
Out of loop