What is the difference in between while loop and do while loop. I offered to think both are fully same.Then ns came throughout following piece of code:

do printf("Word length... "); scanf("%d", &wdlen); while(wdlenThis code works perfectly. That prints native length and tascans the input. But when I changed it to

while(wdlenIt offers a blank screen. It carry out not work. So over there is some practical difference between both loops. Have the right to anybody describe it?

Is there any type of other distinction in this two?


*

*

The do while loop executes the contents of the loop once prior to checking the condition of the while.

Whereas a while loop will check the condition an initial before executing the content.

You are watching: Does a while loop always execute once

In this instance you space waiting for user input v scanf(), which will never ever execute in the while loop as wdlen is not initialized and also may simply contain a garbage value which may be greater than 2.


*

*

While : your condition is in ~ the begin of the loop block, and also makes possible to never enter the loop.

Do While : your condition is at the finish of the loop block, and makes obligatory to go into the loop at least one time.


*

do printf("Word length... "); scanf("%d", &wdlen); while(wdlenA do-while loop guarantees the execution that the loop at the very least once because it check the loop condition AFTER the loop iteration. Because of this it"ll print the string and also call scanf, hence updating the wdlen variable.

while(wdlenAs for the when loop, it evaluate the loop condition before the loop body is executed. Wdlen probably starts turn off as more than 2 in your code that"s why you never reach the loop body.


While:

entry manage loop

condition is checked before loop execution

never execute loop if condition is false

there is no semicolon in ~ the finish of when statement

Do-while:

exit regulate loop

condition is confirm at the end of loop

executes false problem at least once because condition is checked later

there is semicolon at the end of while statement.


The distinction is in once the condition gets evaluated. In a do..while loop, the condition is no evaluated till the finish of every loop. That means that a do..while loop will certainly always run at the very least once. In a when loop, the problem is evaluate at the start.

Here i assume the wdlen is analyzing to false (i.e., it"s bigger than 1) in ~ the start of the while loop, so the when loop never runs. In the do..while loop, that isn"t checked till the finish of the first loop, for this reason you gain the result you expect.


re-publishing
boost this price
follow
answered Sep 2 "10 in ~ 9:30
StephenStephen
5,74933 gold badges3333 silver badges5252 bronze badges
include a comment |
1
Do while loop will be executed atleast once.......but while loop will examine the condition first and then it may or may not get executed depending on the condition.

In your instance wdlen may assume any kind of garbage worth which is > 2 for this reason while loop will never obtain executed.

whereas do while loop will certainly be ececuted and will call u to get in the value and also check that value in terminating condition


re-publishing
improve this answer
monitor
answer Sep 2 "10 in ~ 10:47
SPBSPB
3,6221515 yellow badges4343 silver- badges5959 bronze badges
include a comment |
1
while(wdlenIf wdlen (assuming it"s a ridge variable) is no initialized or assigned a value before the while loop is entered, it will certainly contain everything was in that space in memory before (i.e. Garbage). Therefore if the garbage worth is do ...while(wdlenwill execute once and also then check on problem to run loop again, and also this time it could succeed if by opportunity wdlen which is uninitialized is found to be much less than 2.


share
enhance this prize
monitor
edited Apr 30 "15 in ~ 8:18
answered Oct 8 "12 at 9:48
s.ss.s
13088 bronze title
add a comment |
0
Probably wdlen starts through a value >=2, therefore in the second case the loop problem is originally false and the loop is never ever entered.

In the second case the loop human body is executed before the wdlen problem is checked for the very first time, therefore the printf/scanf is enforcement at the very least once.


re-superstructure
improve this price
follow
answer Sep 2 "10 in ~ 9:29
sthsth
204k4949 yellow badges265265 silver badges357357 bronze title
3
include a comment |
0
while check the condition prior to executing statements in ~ the while loop.

do while test the condition after having executed statement within the loop.

source: let us C


re-publishing
boost this price
follow
answer Jul 2 "14 in ~ 13:34
PrimePrime
922 bronze title
1
2
This concern is 3 year old, and also has currently an answer... +1 for being a hero.
–user1585121
Jul 2 "14 at 13:38


include a comment |
0
while test the problem before executing explanation in the when loop.

do while test the problem after having executed statement inside the loop.


re-superstructure
enhance this prize
monitor
reply Dec 16 "14 in ~ 8:10
BOUKERI BesmenBOUKERI Besmen
1388 bronze badges
2
include a comment |
0
In WHILE first check the condition and also then execute the programIn DO-WHILE loop an initial execute the program at the very least one time then examine the problem


share
boost this price
follow
answered Sep 11 "16 at 4:42
IV REDDYIV REDDY
1
1
add a comment |
0
The difference between do if (exit check) and also while (entry check) is that while beginning in execute while it will not check yet in while that will an initial check

The example is as such:

Program 1:

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

int b=0;while(boutput regime 1:

10

output program 2:

note:

output the the program 1 and program 2 will be very same if us assign a=0 and b=0 and additionally put a++; and b++; in the particular body that the program.


share
enhance this answer
follow
answered may 17 "17 at 8:39
Runni KumariRunni Kumari
40033 silver- badges66 bronze title
include a comment |
0
While Loop:

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

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

This is valid because that C programming, Java programming and also other languages as well because the principles remain the same, just the syntax changes.

Also, another tiny but a separating factor to keep in mind is that the carry out while loop consists of a semicolon at the finish of the while condition.


re-superstructure
enhance this answer
monitor
edited Aug 28 "17 in ~ 11:20
reply Aug 28 "17 at 11:15
Tushar SoniTushar sony
19111 silver badge44 bronze title
include a comment |
0
The difference between a if constructs from action 1 matches a do while is that any kind of expressions in ~ the execute will certainly be to run at the very least once nevertheless of the problem within the while() clause.

println("\nStep 2: just how to use carry out while loop in Scala")var numberOfDonutsBaked = 0do numberOfDonutsBaked += 1 println(s"Number the donuts small = $numberOfDonutsBaked") while (numberOfDonutsBaked here is information explaination: ExplanationVisit: coderforevers


share
boost this answer
follow
answer Oct 24 "17 in ~ 18:41
Ashish YadavAshish Yadav
12311 silver badge88 bronze title
add a comment |
0
The most essential difference between while and do-while loop is the in do-while, the block of password is executed at least once, even 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 different means :

While- your condition is at the start of the loop block, and also makes feasible to never enter the loop.In when loop, the problem is very first tested and then the block of code is executed if the test result is true.
re-superstructure
boost this prize
follow
edited Dec 12 "18 at 13:57
leopal
3,85011 yellow badge2323 silver badges3030 bronze title
reply Dec 12 "18 in ~ 12:49
Yasar KhanYasar khan
1933 bronze badges
include a comment |
Highly energetic question. Knife 10 reputation (not counting the association bonus) in order come answer this question. The reputation requirement helps safeguard this question from spam and non-answer activity.

Not the prize you're feather for? Browse other questions tagged c loops or ask your own question.


The Overflow Blog
Featured on Meta
Linked
-9
difference between while and also do while loop in C++
0
Difference between Pre-Tested and Post-Tested Loops
connected
2685
What is the difference between #include and also #include "filename"?
1003
What is the difference in between ++i and also i++?
2830
exactly how do friend set, clear, and also toggle a solitary bit?
774
What is the difference between range and xrange features in Python 2.X?
3244
how do i loop with or enumerate a JavaScript object?
1551
What is the difference between const int*, const int * const, and int const *?
946
What is the difference in between a definition and a declaration?
842
Difference between malloc and also calloc?
612
Difference in between static and shared libraries?
3540
Loop through range in JavaScript
warm Network questions an ext hot questions
inquiry feed
i ordered it to RSS
question feed To subscribe to this RSS feed, copy and also paste this URL right into your RSS reader.


lang-c
stack Overflow
products
firm
stack Exchange Network
site style / logo design © 2021 stack Exchange Inc; user contributions licensed under cc by-sa. Rev2021.9.17.40238


Stack Overflow works best with JavaScript allowed
*

her privacy

By clicking “Accept every cookies”, girlfriend agree ridge Exchange deserve to store cookie on your machine and disclose information in accordance through our Cookie Policy.