Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break statement in "if else" - java

I keep getting an error, if without else.

I tried else if as well

for (;;){
        System.out.println("---> Your choice: ");
        choice = input.nextInt();
        if (choice==1)
            playGame();
        if (choice==2)
            loadGame();
        if (choice==3)
            options();
        if (choice==4)
            credits();
        if (choice==5)
            System.out.println("End of Game\n Thank you for playing with us!");
            break;
        else
            System.out.println("Not a valid choice!\n Please try again...\n");=[;'mm
    }

also if you have a better idea on how to present this code please do not hesitate :)

like image 681
John Avatar asked Dec 18 '13 23:12

John


People also ask

Can I use break in if statement Java?

The "break" command does not work within an "if" statement. If you remove the "break" command from your code and then test the code, you should find that the code works exactly the same without a "break" command as with one.

Can we use break statement in if else?

The break statement ends the loop immediately when it is encountered. Its syntax is: break; The break statement is almost always used with if...else statement inside the loop.

How do you stop an if statement in Java?

The break statement is used inside the switch to terminate a statement sequence. The break statement is optional. If omitted, execution will continue on into the next case.

DO if statements need break?

you don't need multiple breaks. If the first case is true, that code is executed and the else's are skipped. The same holds true for the else if. So the only time the else gets executed is after the if and else if conditions have evaluated to false.


2 Answers

The "break" command does not work within an "if" statement.

If you remove the "break" command from your code and then test the code, you should find that the code works exactly the same without a "break" command as with one.

"Break" is designed for use inside loops (for, while, do-while, enhanced for and switch).

like image 141
Alan J. Robinson Avatar answered Oct 04 '22 04:10

Alan J. Robinson


Because your else isn't attached to anything. The if without braces only encompasses the single statement that immediately follows it.

if (choice==5)
{
    System.out.println("End of Game\n Thank you for playing with us!");
    break;
}
else
{
   System.out.println("Not a valid choice!\n Please try again...\n");
}

Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered.

In addition, using a switch here would make more sense.

int choice;
boolean keepGoing = true;
while(keepGoing)
{
    System.out.println("---> Your choice: ");
    choice = input.nextInt();
    switch(choice)
    {
        case 1: 
            playGame();
            break;
        case 2: 
            loadGame();
            break;
        // your other cases
        // ...
        case 5: 
            System.out.println("End of Game\n Thank you for playing with us!");
            keepGoing = false;
            break;
        default:
            System.out.println("Not a valid choice!\n Please try again...\n");
     }
 }         

Note that instead of an infinite for loop I used a while(boolean), making it easy to exit the loop. Another approach would be using break with labels.

like image 42
Brian Roach Avatar answered Oct 04 '22 04:10

Brian Roach