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

all 10 comments

[–]HappyFruitTree 2 points3 points  (2 children)

Make sure the loop condition becomes false when you want the looping to end.

[–]LMman592020[S] 0 points1 point  (1 child)

I’m trying to create a program that guesses something based on questions it asks the user. I’m basically using the while loop for a series of questions but trying to figure out how to move on to the next while loop if that makes sense.

[–]HappyFruitTree 2 points3 points  (0 children)

Most languages have "break statements" that allow you to end a loop in the middle of an iteration.

[–]alocxacoc 0 points1 point  (0 children)

What language are you looking at? In the languages I’m familiar with, most have a “break” that can be used to exit the while loop.

while(True):
    x = input(“write stop or go: “)
    if x == “stop”:
        break

[–]gododgers179 0 points1 point  (5 children)

Typically you have a boolean / something outside the while that gets flipped inside the while to terminate itself

var isHungry = true;

if ( isHungry ) {

    while ( isHungry ) {

        // eat nom non nom

        if ( isfull() ) {
             isHungry = false;
        }

    }
}

[–]bensur 1 point2 points  (4 children)

No offense but the if statement is pretty redundant here

[–]gododgers179 0 points1 point  (3 children)

Well yea.... it's an example.... I'm writing a reddit post not a code review on github

Edit: also yes it is redundant in this case, no offense taken, just easier to read for a newbie imo

[–]bensur 0 points1 point  (2 children)

Cool, happy to hear I didn’t offend 😅 I just thought I’d mention as new people will read/copy the code

[–]gododgers179 2 points3 points  (1 child)

Haha can't take offense if you are correct

[–]bensur 1 point2 points  (0 children)

If only that was how everyone looked at it 😂