Dylan Kelemen Dylan Kelemen - 1 month ago 5
Java Question

If-else should not have break?

So my professor mentioned that a break in an if/if-else statement is "bad" code.
What exactly does she mean by that? Also how am I able to fix my code that I currently have written, because it does work the way I want it to, it's just now I need to get ride of the break statement.

int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while(sumOne > 0 || sumTwo > 0){
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
sumOne = input.nextInt();

/**
* We use an if-else statment to ensure sumOne is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumOne <= 0){
break;
}else{
sumOneTotal = sumOneTotal + sumOne;
}

System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
sumTwo = input.nextInt();

/**
* We use an if-else statment to ensure sumTwo is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumTwo <= 0){
break;
}else{
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
//We print out the total of sumOneTotal and sumTwoTotal.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);


Essentially I want the user to enter ANY positive number and that number is added to the first or second sum. Once the user enters any number <= 0 I want the program to stop immediately. The issue that I keep having when I tinker with the code is that the code keeps running through. Meaning if I have the user enter in 0 to be added to the first sum, the code still asks the user to enter in a number for the second sum. I need it to stop right away and not continue. Any help would be a great help! I am using Java.

EDIT!!! So let's say hypothetically I want to make a program that does the exact same thing I am doing now just with no break statement. How would I do so? A few rules. The most outer statement must be a "while" loop. The inner workings of it can be anything. I also need the machine to print out "Enter a number to add to first sum:" and "Enter a number to add to second sum:" alternating. So if I entered in 1,2,3,4. The first sum would be 4 and the second sum would be 6. Last rule is that it can not contain any break statements!

Answer

If you draw the flowchart of your code you can see it exits a bucle in the middle of it, which isn't right, the right way is for it to exit when evaluated on the while, also when someone is reading your code they should expect the bucle is left when evaluated false on the while not in a random if inside the while block, I took your code and made some fixes to make it work as expected but I'm not sure if that's what your teacher expects.

    int sumOne = 1;
    int sumTwo = 1;
    int sumOneTotal = 0;
    int sumTwoTotal = 0;
    while (sumOne > 0 && sumTwo > 0) {
        System.out.print("Enter a number to add to first sum: ");
        // The user enters in a value for the first sum.
        sumOne = input.nextInt();

        /**
         * We use an if-else statment to ensure sumOne is never less than or
         * equal to 0. If it does it ends the program immediately and totals
         * the sums. This is because we only want the user to enter in
         * positive numbers.
         */
        if (sumOne > 0) {
            sumOneTotal = sumOneTotal + sumOne;
            System.out.print("Enter a number to add to second sum: ");
            // The user enters in a value for the second sum.
            sumTwo = input.nextInt();

            /**
             * We use an if-else statment to ensure sumTwo is never less
             * than or equal to 0. If it does it ends the program
             * immediately and totals the sums. This is because we only want
             * the user to enter in positive numbers.
             */
            if (sumTwo > 0) {
                sumTwoTotal = sumTwoTotal + sumTwo;
            }
        }
    }