+ 1
when we can use do while loop in c programming language?
advantage of do-while loop?
2 Antworten
+ 2
So let me explain.
This explanation is for c language (can be valid for othes languages but i won't
take risk for saying tha every language follows that)
Let's Begin with some context:
In C while loop and for loop implementation are pratically equal from a
low-level implementation view. Everithyng you can use for loop you can use while loop.
And do - while loop vs while loop and for loop?
Here do - while loop implementation have a signically difference in your low level implemtation.
So if is different what's different in praticall terms?
While loop: The expression inside ( While(expression) ) is avaliated FIRST. So
if the expression is false you break the loop
do-while loop: The loop is executed ONE TIME first. After that the expression is avaliated. if the expression is false you break the loop.
And what's the advantage?
In my Opinion : Almost nothing. Why? Because mostly problems can be resolved
or most code can be written using While Loop with no problems. Rarely cases happens that you can use only do-while loop. And most coders can't
use correctely do-while loop estabelished by definition view of do-while loop.
In the end. I recommend use while loops and for loops. Just use do-while in last case. If you
keep switching while loop for do-while and vice-versa. You can commit a error easilly.
0
Theyre basically the same. Usage is dependant on coders taste or mood. lol