[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Is it really that bad to do a while(true) loop and rely on breaks?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 15
Thread images: 4
File: plus.png (48 KB, 270x270) Image search: [Google]
plus.png
48 KB, 270x270
Is it really that bad to do a while(true) loop and rely on breaks? I see people say it's bad form because infinite loops, although that's usually the reason why I do a while(true) loop, and because it's much easier to read because you go straight to the while and see the loop condition.

It's really annoying though, because I just want to test the input to see if I get a sentinel and leave the loop and skip the rest of the code. Testing the input a second time for the while seems redundant.
>>
use a do while loop
>>
If the conditions for breaking the loop don't fit well into the while/for/whatever (e.g. when parsing something complicated), I don't see what the problem is. Code that isn't full of security holes probably has many checks that abort its loops and procedures anyways.

If possible I'd probably try to get _something_ that indicates the loop's purpose into while(<expression>) anyways, e.g. a check for EOF / end token when the loop's intended to read data of unknown length.
>>
>>53675070
It is a do while loop, I want it to loop infinitely unless it gets the sentinel value.

do {
cin >> input;
if (input == "sentinel")
break;
else if ...
...
else
cout << "Invalid input";
}while(true)

I could have it test for the exit condition, then have it do nothing until the end of the loop, then text for the exit condition again in the while, but it just seems redundant. I also have to nest everything one step more.

do {
cin >> input;
if (!(input == "sentinel"))
{
if ...
...
else
cout << "Invalid input";
}
}while(!(input == "sentinel"))
>>
>>53675147
Unless I'm missing an easier way to do infinite loops without recursive functions.
>>
>>53675048
read more open source. infinite loops are a normal part of programs. every program runs within a loop or is blocking somewhere before going back to looping or approaching its exit.
>>
>>53677529
pic related. it's nvim source code.
>>
>>53677550
neat. thanks for the example anon
>>
>>53675048
>Is it really that bad to do a while(true) loop and rely on breaks?

What do you mean 'rely'?

It is a machine, your instrument; and you are commander of it. It will obey your any instruction.

If you decide that it should loop until certain condition, let it be so.
>>
>>53677550
>nvim
Isn't that also a buggy piece of shit
>>
File: 1411543196696.png (245 KB, 900x851) Image search: [Google]
1411543196696.png
245 KB, 900x851
>pwd
>/home/chi/q.r&d/tmux
>ag while | wc
> 253 1496 11767

next you'll tell me tmux is buggy
>>
>>53677751
It's was at one point, but it's been stable for a while now. I've been using it exclusively at work since November, and the only time it fucks up is when I try to do something really stupid with the embedded terminal emulator, like play my music from it.

Other than that it's a dream. I don't have to use tmux to manage my terminals anymore and get to use vim's powerful buffer/window/tab management for that.
>>
>>53677821
>only searching for instances of while
Good thing that's not what op is talking about
>>
File: 1438099463668.gif (568 KB, 240x291) Image search: [Google]
1438099463668.gif
568 KB, 240x291
>>53678169
please explain the difference between while(0) that breaks on an a condition vs while(condition).
>>
Infinite loops are ugly. It's like going on a highway then suddenly braking and going backwards
Thread replies: 15
Thread images: 4

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.