What is the distinction between while loop and do while loop. I supplied to think both are completely very same.Then I came across adhering to item of code:

execute printf("Word size... "); scanf("%d", &wdlen); while(wdlenThis code works perfectly. It prints word size and tascans the input. But as soon as I changed it to

while(wdlenIt gives a blank screen. It execute not work-related. So tright here is some practical distinction between both loops. Can anybody explain it?

Is tbelow any other difference in these two?


*

*

The carry out while loop executes the content of the loop when before checking the problem of the while.

Whereas a while loop will certainly inspect the condition first prior to executing the content.

You are watching: Does a while loop always execute once

In this instance you are waiting for user input via scanf(), which will never execute in the while loop as wdlen is not initialized and might simply contain a garbage value which might be better than 2.


*

*

While : your problem is at the start of the loop block, and also renders feasible to never before enter the loop.

Do While : your problem is at the end of the loop block, and makes obligatory to enter the loop at leastern one time.


*

perform printf("Word length... "); scanf("%d", &wdlen); while(wdlenA do-while loop promises the execution of the loop at least when bereason it checks the loop problem AFTER the loop iteration. Thus it"ll print the string and contact scanf, therefore updating the wdlen variable.

while(wdlenAs for the while loop, it evaluates the loop problem BEFORE the loop body is executed. wdlen probably starts off as more than 2 in your code that"s why you never before reach the loop body.


While:

enattempt control loop

problem is checked before loop execution

never before execute loop if condition is false

tbelow is no semicolon at the finish of while statement

Do-while:

departure manage loop

problem is checked at the finish of loop

executes false problem at least once since problem is checked later

tright here is semicolon at the end of while statement.


The difference is in as soon as the condition gets evaluated. In a do..while loop, the condition is not evaluated until the finish of each loop. That implies that a do..while loop will certainly always run at leastern as soon as. In a while loop, the condition is evaluated at the start.

Here I assume that wdlen is evaluating to false (i.e., it"s bigger than 1) at the beginning of the while loop, so the while loop never before runs. In the perform..while loop, it isn"t checked till the end of the initially loop, so you obtain the outcome you intend.


Share
Improve this answer
Follow
answered Sep 2 "10 at 9:30
StephenStephen
5,74933 gold badges3333 silver badges5252 bronze badges
Add a comment |
1
Do while loop will be executed atleast once.......but while loop will check the problem first and also then it might or might not gain executed depending upon the problem.

In your instance wdlen might assume any garbage value which is > 2 so while loop will never obtain executed.

whereas do while loop will certainly be ececuted and will tell u to enter the worth and also examine that value in terminating condition


Share
Improve this answer
Follow
answered Sep 2 "10 at 10:47
SPBSPB
3,6221515 gold badges4343 silver badges5959 bronze badges
Add a comment |
1
while(wdlenIf wdlen (assuming it"s a stack variable) is not initialized or assigned a value before the while loop is gone into, it will contain whatever before was in that space in memory prior to (i.e. garbage). So if the garbage value is do ...while(wdlenwill certainly execute when and also then checks on condition to run loop aacquire, and this time it might succeed if by possibility wdlen which is uninitialized is discovered to be much less than 2.


Share
Improve this answer
Follow
edited Apr 30 "15 at 8:18
answered Oct 8 "12 at 9:48
s.ss.s
13088 bronze badges
Add a comment |
0
Probably wdlen starts with a worth >=2, so in the second instance the loop problem is initially false and the loop is never gotten in.

In the second situation the loop body is executed before the wdlen condition is checked for the initially time, so the printf/scanf is executed at least once.


Share
Improve this answer
Follow
answered Sep 2 "10 at 9:29
sthsth
204k4949 gold badges265265 silver badges357357 bronze badges
3
Add a comment |
0
while test the condition prior to executing statements within the while loop.

execute while test the condition after having actually executed statement within the loop.

source: let us C


Share
Improve this answer
Follow
answered Jul 2 "14 at 13:34
PrimePrime
922 bronze badges
1
2
This question is 3 years old, and also has currently a solution... +1 for being a hero.
–user1585121
Jul 2 "14 at 13:38


Add a comment |
0
while test the condition before executing statements in the while loop.

carry out while test the condition after having actually executed statement inside the loop.


Share
Improve this answer
Follow
answered Dec 16 "14 at 8:10
BOUKERI BesmenBOUKERI Besguys
1388 bronze badges
2
Add a comment |
0
In WHILE first inspect the problem and also then execute the programIn DO-WHILE loop initially execute the regime at least one time then examine the problem


Share
Improve this answer
Follow
answered Sep 11 "16 at 4:42
IV REDDYIV REDDY
1
1
Add a comment |
0
The difference between carry out while (departure check) and while (entry check) is that while entering in perform while it will not examine however in while it will certainly initially check

The example is as such:

Program 1:

int a=10;doSystem.out.println(a);while(aProgram 2:

int b=0;while(boutput Program 1:

10

output Program 2:

note:

output of the regime 1 and routine 2 will certainly be same if we asauthorize a=0 and b=0 and also additionally put a++; and also b++; in the corresponding body of the routine.


Share
Improve this answer
Follow
answered May 17 "17 at 8:39
Runni KumariRunni Kumari
40033 silver badges66 bronze badges
Add a comment |
0
While Loop:

while(test-condition) statements; increment/decrement;Lower Execution Time and SpeedEntry Conditioned LoopNo addressed number of iterationsDo While Loop:

do statements; increment/decrement;while(test-condition);Higher Execution Time and SpeedExit Conditioned LoopMinimum one variety of iterationFind out more on this topic here: Difference Between While and also Do While Loop

This is valid for C programming, Java programming and other langueras as well because the ideas remain the exact same, just the syntaxes transforms.

Also, one more small however a separating factor to note is that the carry out while loop is composed of a semicolon at the finish of the while condition.


Share
Improve this answer
Follow
edited Aug 28 "17 at 11:20
answered Aug 28 "17 at 11:15
Tushar SoniTushar Soni
19111 silver badge44 bronze badges
Add a comment |
0
The distinction between a while constructs from Tip 1 versus a carry out while is that any type of expressions within the carry out will be running at leastern once regardless of the condition within the while() clausage.

println(" Tip 2: How to use perform while loop in Scala")var numberOfDonutsBaked = 0execute numberOfDonutsBaked += 1 println(s"Number of donuts baked = $numberOfDonutsBaked") while (numberOfDonutsBaked Here is information explaination: ExplanationVisit: coderforevers


Share
Improve this answer
Follow
answered Oct 24 "17 at 18:41
Ashish YadavAshish Yadav
12311 silver badge88 bronze badges
Add a comment |
0
The a lot of vital difference in between while and also do-while loop is that in do-while, the block of code is executed at least as soon as, also though the problem given is false.

See more: Simple Tips On How To Make Love All Night (And Drive Your Woman Wild)

To put it in a various means :

While- your problem is at the start of the loop block, and also renders feasible to never before enter the loop.In While loop, the problem is first tested and also then the block of code is executed if the test outcome is true.
Share
Improve this answer
Follow
edited Dec 12 "18 at 13:57
leopal
3,85011 gold badge2323 silver badges3030 bronze badges
answered Dec 12 "18 at 12:49
Yasar KhanYasar Khan
1933 bronze badges
Add a comment |
Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer task.

Not the answer you're looking for? Browse various other inquiries tagged c loops or ask your own question.


The Overcirculation Blog
Featured on Meta
Linked
-9
difference in between while and do while loop in C++
0
Difference in between Pre-Tested and Post-Tested Loops
Related
2685
What is the difference between #encompass and #include "filename"?
1003
What is the difference in between ++i and i++?
2830
How do you set, clear, and toggle a solitary bit?
774
What is the difference in between variety and also xarray features in Python 2.X?
3244
How execute I loop through or enumeprice a JavaScript object?
1551
What is the distinction between const int*, const int * const, and also int const *?
946
What is the distinction in between an interpretation and a declaration?
842
Difference in between malloc and calloc?
612
Difference in between static and also mutual libraries?
3540
Loop through an selection in JavaScript
Hot Network Questions more warm concerns
Question feed
Subscribe to RSS
Inquiry feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


lang-c
Stack Overflow
Products
Company type of
Stack Exadjust Netjob-related
site design / logo design © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.9.17.40238


Stack Overcirculation works ideal through JavaScript allowed
*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exreadjust can keep cookies on your tool and also disclose indevelopment in accordance via our Cookie Policy.