Hey please whats is the difference between do while loop and while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hey please whats is the difference between do while loop and while loop

3rd Apr 2018, 2:21 PM
Aiman Aqari
Aiman Aqari - avatar
7 Answers
+ 2
while () {} : It is an entry control loop i.e the condition is checked before entering into loop whereas do {} while() ;is exit control loop the conditions are checked at the end of do scope i.e after executing the do.. body Suppose this piece of code in cpp int i = 2; while(i < 1) // 2 < 1 //false so it doesn't enter inside while { cout << "Inside while " ; // this line isn't printed } do{ //enters do.. while body regardless of condition { cout << "Inside do while " ;//prints } while (i < 1); // checks : 2 < 1 which is false. Loop terminates
3rd Apr 2018, 2:37 PM
Bishal Sarang
Bishal Sarang - avatar
+ 10
//search for the question , U will find many more 👍 https://www.sololearn.com/Discuss/1126330/?ref=app
3rd Apr 2018, 2:57 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
okeeey i get it now thank you bro
3rd Apr 2018, 2:38 PM
Aiman Aqari
Aiman Aqari - avatar
+ 2
Thw while loop verifies rhe condition before the statements in the loop are executed. The do while loop execute the statements before checking the condition. Say: x = 0 do{ System.out.print(x); }while(x != 0); while(x != 0){ System.out.print(x); } In the first case 0 will be printed, then the condition checked. It will return false so the statement in the loop will no longer be executed. In the second case 0 will never be printed cause the condition returns false so no statement in the loop is executed
3rd Apr 2018, 2:32 PM
cyk
cyk - avatar
+ 2
they said that do while loop can run your programme once a time please can u explain it to me
3rd Apr 2018, 2:34 PM
Aiman Aqari
Aiman Aqari - avatar
+ 2
Yes. Sure. Because the condition is checked after the statements in the loop are executed, they are executed at least one time. Like in the example I gave you. Though the condition is not true the loop executed once.
3rd Apr 2018, 2:37 PM
cyk
cyk - avatar
0
If the condition specified for the loop is false,while loop will not run at all. But do while loop iterates at least once even if the condition is false,because condition gets checked at the end.
16th Apr 2018, 6:47 AM
DEBAYAN BHAUMIK
DEBAYAN BHAUMIK - avatar