do while loop in C programming language
Let us understand do while loop in C with help of an example.
syntax:
do { /*block of statement*/ }while(condition);
Explanation of do while loop in C:
Here, while and do are the keywords in C language which is know to the compiler. Condition can be any expression. This is very similar to while loop in C.
Here, the block of statement enclosed in the opening and closing braces after the keyword do is executed at least once. After the execution of the block of statement for the first time, the condition in the while is executed.
If the condition is satisfied then the block of statement inside the do block is executed. After the execution of block of statement, again condition is check. If the conditional expression returns true, the block of statement is executed again. This process is followed till the conditional expression returns false value.
Note:
It is necessary to write semicolon (;) after the while(condition) as shown in the syntax else you will get an error.
The example for while Loop in C can re-written as follows:
#include<stdio.h> #include<conio.h> void main() { int i=0; clrscr(); do{ i=i+1; printf("%d This will be repeated 5 times\n", i); }while(i!=5); printf("End of the program"); getch(); }
Output of the above do while loop example:
1 This will be repeated 5 times 2 This will be repeated 5 times 3 This will be repeated 5 times 4 This will be repeated 5 times 5 This will be repeated 5 times End of the program
Nice explanation. This site is like a book. something very different and interesting
great
a wonderful site to learn c
this site is 21st century learn c site
clear and short cut and easiest way to learn c
nice
LOVE THIS
AwesoMe Site!!! I loved this site It solved lot of doubts which were in my mind Thank You learnconline.com 🙂
good work…
while(i!=5)
here even 1 2 3 4 are not equal to 5 then hw can we get the output this is repeated 5 time for the first four steps
do{
i=i+1;
printf(“%d This will be repeated 5 times\n”, i);
}while(i!=5);
here 1 2 3 4 are also nt equal to 5 na (i!=5), then the o/p shud b end of the program for the first 4 steps 1 2 3 4…
Before checking while condition it will executed so it will executed for 5 also,
like while(4!=5)it will again enter the block & will execute once it executed only it will check for next condition while(5!=5)
very good site for the biginners
wht is clrscr
clrscr is an inbuilt function used to clear the output window screen
reply to mushtaq ali: here we condition while(i!=5).that means we are asking to execute only if i is not equal to 5.so 1 ,2 3 and 4 are not equal to 5.so program executes and gives output.bt after 4 comes 5 ..which is equal to 5.then our condition while(i!=5) becomes false and comes out of program and stops execution.
awesome site!!!!!
Is there any site like this for learn java & .net?
Interesting to learn
Thanks
whats difference between while and do while?
This is very good site for every one who want to learn c , i’m also a learner of urs… thanks a lot ….. i have a doubt…..
Where to use while statement and and where to use do while statement ? In which way they differ practically
In the while loop, the condition is first evaluated and then the block of code is executed based on the condition.
In do-while loop, first the block of statements inside do is executed and then while condition is evaluated.
Even if the condition is not satisfied for the first time, the block of statements inside do is executed at least once.
Hence, use do-while loop if you want your block of statements to be executed at least once.
Thanks,
LearnCOnline Team
#include
#include
int main(){
int i=0;
do{
i=i+1;
printf(“%d LISTEN TO YOUR COMPUTER DIE LMAO!!\n”,i);
}while(i!=10);
}