- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Do While and While Loop in C Programming Language
If you are looking for what is loop and why we use loop in C language then you can check my article on that topic. This article is entirely dedicated to do-while loop and its usage. I must tell you that do-while loop is similar to while loop but only one thing differentiate them. I will show that difference with example later.
do-while loop syntax
initialize loop variable; do { loop statement 1; loop statement 2; loop statement 3; …………………. loop statement N; increment loop variable; } while(condition);
while loop syntax
initialize loop variable; while(condition) { loop statement 1; loop statement 2; loop statement 3; …………………. loop statement N; increment loop variable; }
I have provided syntax of do-while loop along while loop so that we can compare them. As you can see do-while loop starts with “do” keyword and then statement to execute inside loop and then finally condition with “while” keyword. If we compare do-while loop syntax with while loop syntax we can see one big difference. In case of while loop we write condition at the beginning of loop whereas in do-while loop at the end of loop. That difference not limited to syntax, that different also makes them to logically behave differently.
There is one more minor difference in their syntax that is in do-while loop we put semicolon (;) after while statement but in while loop we don’t. This point is just to highlight this minor difference, I saw many people forget to put semicolon in do-while loop and sometimes put semicolon in while loop.
Let’s check one example of do-while loop then we will compare same example with while loop.
Do while loop example
#include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); do { printf("%d : do while loop.\n", i); i++; } while(i<=10); getch(); }
Do while loop example explanation
This is same example we used in while loop in C language tutorial, here, instead of while loop we have do-while loop to achieve same output. For those who are reading this article for the first time and never read while loop tutorial, here is a summary what above program does. Actually above program prints “do while loop” with line no. for 10 times as we have condition (i<=10).
Line no. 10-15 is our do-while loop block which prints message and increments value of “i” by 1. In line no. 10 we have “do” keyword which is beginning of do-while loop. Between line no. 11–14 we have statements which we are going to execute inside loop. In line no. 15 we are providing do-while loop execution condition. Here we are instructing it to loop line no. 12 & 13 for 10 times as condition is (i<=10) and initial value of “i” is 1. Also notice semicolon (;) in line no. 15, its end of do-while loop and guys never forget to put that.
Difference between While and Do-While Loop
As I said earlier that there is logical difference in while loop and do-while loop, I am going to show that in this section. There is only one major logical difference between while and do-while loop and by logical difference I mean their behavior in program (not in syntax). Remember this difference, in case of do-while loop whether provided condition is true and false, statement inside do-while loop will execute at least once. Or in other word, whatever is the do-while loop condition, it will execute its statements at least once. But in while loop it will execute loop statement (statements meant to be execute inside loop) only if condition is true.
This strange behavior of do-while loop is because we check loop condition at the end of loop whereas in while loop first we check loop condition and then loop statement executes if condition holds true. Let me give you one example where we will execute same statement in while loop as well as in do-while loop.
#include<stdio.h> #include<conio.h> void main() { int i=99; clrscr(); do { printf("%d : do while loop.\n", i); i++; } while(i<=10); i=99; // resetting value to 99. while(i<=10) { printf("%d : while loop.\n", i); i++; } getch(); }
Explanation
So above we have sample program which demonstrate the difference between while and do-while loop. From line no. 10-14, we have while loop and from line 16-21 do-while loop. In line no. 7 we are declaring and initializing variable “i” to 99 and in both loops we have same condition and loop statements.
When we run this program, one would expect no output, only blank screen. Why? Let me tell you, in line no. 7 we are assigning value of “i” to 99 and in both loop (while and do-while loop) we have same condition i.e. “i” less than equal to 10 ( i<=10). As you know, loop condition is not satisfied so all statements written inside loop block will not execute. Logically this is correct. But in case of do-while loop, it will execute loop statement at least once even condition is not satisfied. You can check output for confirmation. There you can see message from do-while loop only. This is the unique behavior of do-while loop, it executes its loop statement and then check for condition. If condition is satisfied then it loops again otherwise it exists from loop.
I hope this article somehow tried to help you understand do-while loop and its difference with while loop.
There are some important points to remember when we use loop in our program. Those points I already discussed in “Point to remember” section of while loop in C language article. Don’t forget to check that article.
Your Opinion
Did this help you to learn Do-While loop?
© 2011 RAJKISHOR SAHU