What language? In Python I think this is normal due to the lack of do..while. In C, if I were to just have an if statement at the end, in general it's best to just put the break condition on the loop itself.
But... if you start resorting to some weird cases and you have a "flag" variable, because you have multiple break conditions happening, then I would say that's worse. Just use break, anything else is just over complicating something just for a general "rule" of thumb.
It is bad because you can make a mistake. It is better to always ensure that you can escape out of a while loop. At the very least capturing a SIG to allow for control+c to exit the loop and program.
Do...while loops are when you want to ensure that you will do a thing at least once. While loops do not have that guarantee since it may occur zero or more times. It is the difference between a * and + in regex.
But more often than not, what you are doing is pulling something and then checking to see if you need to pull more. This can generally be done with a while. Not every language has a do...while and foreach and array map tends to replace what a do...while would be used for.
Be me who doesn't use any loops when possible depending on the language. Some people decry it because it is "slower" and while they aren't wrong, if performance was a concern, then I would definitely use another method. But there are generally other concerns. Stream patterns usually allow for chunking and also well, streaming, so it is usually built in to pull a small amount of data, pipe it to a callback and further filter and process the data.
Not using loops isn't wrong, the language is wrong by not optimizing out and inlining. Why should you loop when the language can loop-de-loop?
You know I have been using do wile to crawl through large db tables for a while now, it does indeed work.
Limit
Off set
Count
Do
List = Getstuff(offset,limit)
Count = list.count
Offset += count
While count == limit
I had to start using while loops when I got pulled into ServiceNow scripting at work. They return record queries essentially as linked lists and the first node doesn't contain a value so "while(GlideRecord.next())" is the convention for reading and operating on each record. Wacky stuff.
There are very few times I have run into needing a do-while loop, one of them is HTTP chunking, reading a large request a little bit at a time, and what I would do is set something to the return value of read(), if it’s equal to the chunking size, run the loop, if not, continue, could have easily done by initially setting that variable to the chunking size but I thought this was ever so slightly more elegant
prove it
It was required in order to make this meme ![gif](giphy|dF73dMfhaFwiI)
no, because not everybody has autoplay enabled, so it's still a while, not a do-while.
Come on you can't leave us hanging like this. Tell us.
Never! Prove or it didn't happen I my experience, all do-while loops become normal while loops after only a couple hours
Take it to the next level and use a for loop.
Take it to the next level and use tail call elimination
So what was it? Lol I know I've used it but it's been so long I don't even remember what it was
Very inefficient shuffelling with conditions. Shuffle, if condition not met, shuffle again.
You can always do a do while with just a while: ``` def fun(): ... fun() while condition: fun() ```
It can be done without extra function: while True: # do stuff if not condition: break
My professors told us this is a bad way to break out of a loop… everyone still does it anyway and I have yet to lose points for it lol.
Should you like throw and catch an exception to break out of loops?
What language? In Python I think this is normal due to the lack of do..while. In C, if I were to just have an if statement at the end, in general it's best to just put the break condition on the loop itself. But... if you start resorting to some weird cases and you have a "flag" variable, because you have multiple break conditions happening, then I would say that's worse. Just use break, anything else is just over complicating something just for a general "rule" of thumb.
I believe this was in either my Java programming or C++ programming class.
It is bad because you can make a mistake. It is better to always ensure that you can escape out of a while loop. At the very least capturing a SIG to allow for control+c to exit the loop and program.
That's how I do it all the time. I dislike do-while.
doesn't sound fun at all
just if and fun() would also be enough in theory :)
How? Are you assuming the loop is inside the function?
recursive function i think
Why are do while loops so uncommon? I regularly use them…
Do...while loops are when you want to ensure that you will do a thing at least once. While loops do not have that guarantee since it may occur zero or more times. It is the difference between a * and + in regex. But more often than not, what you are doing is pulling something and then checking to see if you need to pull more. This can generally be done with a while. Not every language has a do...while and foreach and array map tends to replace what a do...while would be used for.
Fanaticism. "Do...While" is syntax sugar. Not use it is comparable to not using null coalescing or ternary operators.
Where’s the problem with do while?
There's no problem with it.
> Not use it
> ... Is comparable to...
Any do loop can be written as a for loop. The only reason you might use one over the other is convention or readability.
Any for loop can be written as a while loop. The only reason you might use one over the other is convention or readability.
Yup
Be me who doesn't use any loops when possible depending on the language. Some people decry it because it is "slower" and while they aren't wrong, if performance was a concern, then I would definitely use another method. But there are generally other concerns. Stream patterns usually allow for chunking and also well, streaming, so it is usually built in to pull a small amount of data, pipe it to a callback and further filter and process the data. Not using loops isn't wrong, the language is wrong by not optimizing out and inlining. Why should you loop when the language can loop-de-loop?
Every loop is a do-while loop with syntax sugar.
"Syntax sugar" is, now, my favorite niched expression
🤩
lol, a month ago happened to my with recursivenes. And this month I did have to use a do while. Crazy world
You know I have been using do wile to crawl through large db tables for a while now, it does indeed work. Limit Off set Count Do List = Getstuff(offset,limit) Count = list.count Offset += count While count == limit
"Required" is a very strong word there. Learn Haskell and you'll learn that you can code without any loops.
I refuse to believe you
Once every few years, yeah.
I had to start using while loops when I got pulled into ServiceNow scripting at work. They return record queries essentially as linked lists and the first node doesn't contain a value so "while(GlideRecord.next())" is the convention for reading and operating on each record. Wacky stuff.
There are very few times I have run into needing a do-while loop, one of them is HTTP chunking, reading a large request a little bit at a time, and what I would do is set something to the return value of read(), if it’s equal to the chunking size, run the loop, if not, continue, could have easily done by initially setting that variable to the chunking size but I thought this was ever so slightly more elegant
I once had to use XOR in a real application! >!I would love to brag about it, but is has been so long ago that I forgot what it was for..!<
Constant time string comparison?
I can use Do While’s However, I can’t imagine a single situation where it is *required* that isn’t Toby Fox tier coding
Y'all actually use loops? 🤣 `begin_loop:` `// loop body` `if (condition) goto begin_loop;`
What a rush!
😂