Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Program not breaking out of loop?

while (choice != 7) { 

    System.out.println("---  Mathematical Calculator  ---");

    System.out.println("");

    System.out.println("Pick an operation from the list - Use nos. 1 to 7");
    System.out.println("1) Multiplication");
    System.out.println("2) Division");
    System.out.println("3) Addition");
    System.out.println("4) Subtraction");
    System.out.println("5) Find the area of a regular object");
    System.out.println("6) Find the volume of a regular object");
    System.out.println("7) Exit");

    choice = userInput.nextInt(); 

    switch (choice) {

    case 1: {
        System.out.println("");
        System.out.println("You have chosen multiplication");
        System.out.println("Enter a number");
        double num1 = userInput.nextDouble();
        System.out.println("Enter another number");
        double num2 = userInput.nextDouble();

        double num3 = num1 * num2;

        System.out.println(num1 + " * " + num2 + " = " + num3);         
        num1 = num3;

        while (choice2 != 5 || choice2 != 6) {

            System.out.println(""); System.out.println("");
            System.out.println("If you would like to build on the answer obtained - pick an operation from the list - Use nos. 1 to 4");
            System.out.println("Else press 5 to return to the main menu");
            System.out.println("1) Multiplication");
            System.out.println("2) Division");
            System.out.println("3) Addition");
            System.out.println("4) Subtraction");
            System.out.println("5) Start new calculation");
            System.out.println("6) Exit");

            choice2 = userInput.nextInt();

            switch (choice2) {

            case 1: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 * num2;
                System.out.println(num1 + " * " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 2: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 / num2;
                System.out.println(num1 + " / " + num2 + " = " + num3);
                num1 = num3;
                reak;
            }

            case 3: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 + num2;
                System.out.println(num1 + " + " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 4: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 - num2;
                System.out.println(num1 + " - " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 5: choice = 0; break;

            case 6: choice = 7; break;

            default: System.out.println("Invalid choice");
        }
        choice2 = 0;
    }
    break;
}

I've posted a short piece of my code. My issue is that, when I input 5 or 6 in the second query (choice2), my program continues to loop instead of breaking out of the loop and going back to the main menu/ terminating the program.

Would appreciate some feedback on what I'm doing wrong.

like image 569
TheHoboMaster Avatar asked Jan 08 '23 19:01

TheHoboMaster


2 Answers

Your while (choice2 != 5 || choice2 != 6) is wrong, since it will always result in true. If choice2 is 5, then the second clause will be true.

Not to mention that you always set choice2 to 0 at the end of the loop, so the loop will continue forever even if you replace || with &&.

like image 54
Kayaman Avatar answered Jan 10 '23 09:01

Kayaman


The condition choice2 != 5 || choice2 != 6 is always true, because there is no number that is equal to 5 and to 6 at the same time.

If you would like to break out of the loop when 5 or 6 is entered, use && instead:

while (choice2 != 5 && choice2 != 6)

Make sure that choice2 is set prior to continuing with the next iteration of the loop.

Note that you could break out of the loop from within your switch statement by using a labeled break construct:

calc: // Label the loop for using labeled break
while (true) {
    ...
    switch(...) {
        case 1:
            break;          // This will exit the switch
        ...
        case 6: break calc; // This will exit the loop
    }
}
like image 42
Sergey Kalinichenko Avatar answered Jan 10 '23 08:01

Sergey Kalinichenko