-5

New learner in java. Need to know how do I stop while loop from looping? Help plz

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
    }
}
6
  • 9
    You can beak from a loop using break; Commented Feb 25, 2016 at 15:12
  • May I know whr should i put the break; statement? Commented Feb 25, 2016 at 15:16
  • @nur wherever you wish to stop... Commented Feb 25, 2016 at 15:16
  • Maybe the real need is "I want to reprint the menu while the user does not input 1, 2 or 3"? Commented Feb 25, 2016 at 15:17
  • @nur To stop a while loop. Commented Feb 25, 2016 at 15:18

6 Answers 6

4

first of all while(true) is an infinite loop. So you will want to change the conditions I guess you will be saving the chose option as an integer so basically you will be doing something like this would be your best option

int choice = 0;
while(choice !=3)
{
    System.out.println ("Menu");
    System.out.println ("1. Make Guess");
    System.out.println ("2. List Winner");
    System.out.println ("3. Exit");
    choice = keyboard.nextInt();
    //do something with your choice or ignore if it is 3   
}

this answer presumes you are taking input from somewhere as your menu choice

Sign up to request clarification or add additional context in comments.

1 Comment

Whilst break; is the simplest answer to the question, I think this actually leads OP down the correct path. +1
2

You can break from the loop using break;

3 Comments

so you copied and pasted a comment and you got +20 reputation, not fair.
@A.DiMatteo I am agree with you.
in those cases the suggested approach is to copy and paste the comment, transform it in an answer AND mark the answer as community wiki, so you don't gain reputation on it (fair) but you help the community (and the question) anyway (SO spirit)
2

Add this somewhere in your loop:

if(condition) break;

but essential for breaking a loop is:

break;

Comments

1

You'd have to do it like this:

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
        break;
    }
}

but this is equal to:

public String displayMenu()
    {
         System.out.println ("Menu");
         System.out.println ("1. Make Guess");
         System.out.println ("2. List Winner");
         System.out.println ("3. Exit");
    }

This would display "Menu", "1. Make Guess" [...] once. So according to what you want to print to the screen, you don't even need a loop here.

Comments

1

I disagree of "breaking" the while loop by using break. I think you will always have and "ending condition" and that's what you should aim for. By using break you are thinking also in not to think about that, although sometimes makes sense for some — I haven't found a single use case where you cannot find a condition to exit.

Using a break statement in your while loop is like having the same statements without it, there has to be some logic you are looking for to repeat the statements you want to repeat.

If you give more context you might get a better result. Moreover, a while (true) is a way to define a infinite loop (something not very useful in real life software).

Comments

0

With this break; statement your loop will only execute once.

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
        break;  // break out of loop execution
    }
}

2 Comments

The loop in this code is pointless. If you break there you might as well skip the loop alltogether.
@barq that is true, but it's the OP's code, I was just showing an example of how to exit the loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.