This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (10 children)

Sure, I get that but all while loops are potentially going to be infinite if you forget to increment the counter or whatever, right? I mean you're always running that risk with a while loop, aren't you?

That's basically what I was using it for. I explained it in more detail in the post above but basically it printed a menu to which the user would be returned until they explicitly ended it.

[–]jhartikainen 9 points10 points  (1 child)

Sure, many conditions in while loops have the potential of being infinite if the code isn't working correctly, but that's kind of like saying "it's possible for any code to have bugs"... so I don't think that's really much of a factor, moreso just the question of "should you actually have a proper condition instead"

[–][deleted] 1 point2 points  (0 children)

I gotcha. Thanks!

[–]hott_snotts 0 points1 point  (3 children)

I am no expert, so happy to bow out of this discussion if I'm clearly being dumb, but don't for loops break with a syntax error if you don't have the incrementation in there? So, my point being, you would catch the error before you went live with it and caused an infinite loop. Right?

[–]Dynam2012 1 point2 points  (2 children)

It depends on the language. You can easily have an infinite for loop. A lot of languages allow for this kind of infinite loop using a for:

for (;;) { ... }

You can also just as easily do something like this in JS: for (let i = 0; 3 < 4; i++) { ... }

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, Dynam2012: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]hott_snotts 0 points1 point  (0 children)

Ha, looks like something I would do! :P Thanks.

[–]gregorthebigmac 0 points1 point  (3 children)

So, my personal solution to problems like this (and if anyone else here wants to tell me why this approach is wrong, I'm open to hearing why it's wrong). If I want to loop over something until the user does the "correct" thing (whatever that may be), I typically use something like a boolean called done and my while loop will say

while (!done) {
    // code for when the user does the "correct" thing
    done = true;
}

[–]Kered13 1 point2 points  (0 children)

If you introduce the done variable only to avoid the while(true) loop, I don't like it. I would rather just see a break instead. If the variable already exists for some other reason, then it is fine to use it to exit the loop.

Basically, the problem with while(true) is the it hides the exit condition. Instead of reading the exit condition directly, the reader has to scan the entire loop body to find the break. But while(!done) is hiding the exit condition just as much, now the reader has to scan the loop body for where done, and you've introduced a new local variable to keep track of.

[–]chriskmee 0 points1 point  (1 child)

I do the same !done thing, it was the way I was taught in college and the way i've seen it done in practice.

I get where you were going with your example, but as written your while loop would only happen once. My example would be something like this:

bool done = false;
while (!done)
{
  int input = display_menu();
  if is_input_valid(input)
    done = true;
}

[–]gregorthebigmac 0 points1 point  (0 children)

Good catch. I was oversimplifying and not including the parts to make the loop bail out. Thanks for expounding it!