Unless otherwise there should be a gap?

So, my professor said that the gap in the if / if-else statement is "bad" code. What exactly does she mean? Also, how can I fix my code that I have written now, because it works the way I want it, now I just need to go to 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 this number is added to the first or second amount. As soon as the user enters any number <= 0, I want the program to stop immediately. The problem that I continue to have when I mess with the code is that the code continues to work. Value, if I have a user, enter in 0 to be added to the first amount, the code still asks the user to enter a number for the second amount. I need him to stop immediately and not continue. Any help would be a big help! I am using Java.

CHANGE !!! Therefore, suppose that, hypothetically, I want to create a program that does the same thing as it is now, without the break statement. How should I do it? A few rules. The outermost statement should be "while". The inner workings of this can be any. I also need a printing machine “Enter the number to be added to the first sum:” and “Enter the number to add to the second sum”, alternating. Therefore, if I enter 1,2,3,4. The first sum will be 4, and the second sum will be 6. The last rule is that it cannot contain any break statements!

+6
source share
7 answers

If you draw a block diagram of your code, you will see that it leaves the heap in the middle, which is wrong, the right way is to exit when it is evaluated at that time, and also when someone reads your code, he should expect a bunch will remain false if it wasn’t random, if inside the while block I took your code and made some corrections to make it work properly, but I'm not sure 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; } } } 
+2
source

This is a return to the fact that structured programming was new, back when goto statements, etc. were everywhere. Theoretically, ideally, you would never have to use breaks / continue and have only one return point. In fact, it can make your work a lot harder, making programs harder to write, harder to read and use more computing resources. Multiple returns, continuations, and breaks are middle men between truly structured programming and spaghetti code. Used correctly, there is nothing wrong with them.

As a rule, I found that they will only hide your code if you are already using bad methods that make it difficult to read your code (for example, writing huge blocks of logic, not parsing them, tightly linking objects, etc.).

If you're interested, here is a link to an interesting perspective, why NOT use them. And here is a look at why they are beneficial.

Many others have already answered with the code, but here is my snapshot :)

 public class Main { public static void main(String args[]) { int sumOne = 1; int sumTwo = 1; int sumOneTotal = 0; int sumTwoTotal = 0; Scanner input = new Scanner(System.in); while(sumOne > 0 || sumTwo > 0){ System.out.print("Enter a number to add to first sum: "); sumOne = input.nextInt(); if (is_positive(sumOne)){ sumOneTotal = sum_numbers(sumOneTotal, sumOne); System.out.print("Enter a number to add to second sum: "); sumTwo = input.nextInt(); if(is_positive(sumTwo)){ sumTwoTotal = sum_numbers(sumTwoTotal, sumTwo); } } } System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal); return; } public static int sum_numbers(int x, int y){ int total = x + y; return total; } public static boolean is_positive(int x){ boolean is_pos = true; if(x < 0){ is_pos = false; } return is_pos; } } 

I would say that reading is difficult now. The more to the right my code starts to gravitate, the more I'm sorry who needs to support it .. Of course, I could remove a level or two indents by wrapping (more) bits in the methods. Then it becomes easier to read, but there is a point where black boxing, every tiny bit of logic just seems superfluous ...

+6
source

Much cleaner without interruptions.

 int candidate = 0; int [] sums = {0,0}; int index = 1; System.out.print("Enter a number to add to first sum: "); while((candidate = input.nextInt()) > 0){ sums[index] = sums[index] + candidate; index = (index + 1)%2; System.out.print("Enter a number to add to " + ((index == 0) ? "first":"second" ) + " sum: "); } //We print out the totals. System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sums[0], " ", "Second sum: ", sums[1]); 

This does not mean that you should always avoid interruptions, but in this case you can avoid this to make your code shorter, less redundant.

+1
source

Sometimes avoiding a break is worse than using it. I would write it like this with a shorter break.

 int sumOneTotal = 0; int sumTwoTotal = 0; while (true) { System.out.print("Enter a number to add to first sum: "); //The user enters in a value for the first sum. int sumOne = input.nextInt(); if (sumOne <= 0) break; sumOneTotal += sumOne; System.out.print("Enter a number to add to second sum: "); //The user enters in a value for the second sum. int sumTwo = input.nextInt(); if (sumTwo <= 0) break; sumTwoTotal += sumTwo; } 

You can avoid a break, but this does not make the code more understandable / simple IMHO.

 int sumOneTotal = 0; int sumTwoTotal = 0; boolean okay = true; do { System.out.print("Enter a number to add to first sum: "); //The user enters in a value for the first sum. int sumOne = input.nextInt(); if (sumOne <= 0) { okay = false; } else { sumOneTotal += sumOne; System.out.print("Enter a number to add to second sum: "); //The user enters in a value for the second sum. int sumTwo = input.nextInt(); if (sumTwo <= 0) { okay = false; } else { sumTwoTotal += sumTwo; } } while (okay); 

The same applies to using labels. Avoid them where possible, if avoiding them does not mean that they are doing something worse.

0
source

I disagree that using <<20> inside if always bad. However, it is more a matter of opinion than anything, and in fact it is not here. I will answer part of your question, which is on the topic, namely: how can I fix my code so as not to use break inside if .

The code below will continue the cycle, asking the user to enter until a valid number is entered. This avoids the original problem and has the additional advantage of allowing the user to enter a new number if they make a mistake, instead of exiting the cycle and starting them to start.

 int sumOne = 1; int sumTwo = 1; int sumOneTotal = 0; int sumTwoTotal = 0; while(sumOne > 0 || sumTwo > 0){ do { System.out.print("Enter a number to add to first sum: "); //The user enters in a value for the first sum. sumOne = input.nextInt(); System.out.print("Enter a number to add to second sum: "); //The user enters in a value for the second sum. sumTwo = input.nextInt(); }while(sumTwo <= 0 || sumOne <= 0); sumOneTotal = sumOneTotal + sumOne; 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); 
0
source
  while (sumOne > 0 && sumTwo > 0) { System.out.print("Enter a number to add to first sum: "); sumOne = input.nextInt(); if (sumOne > 0) { sumOneTotal = sumOneTotal + sumOne; System.out.print("Enter a number to add to second sum: "); sumTwo = input.nextInt(); if (sumTwo > 0) sumTwoTotal = sumTwoTotal + sumTwo; } } 

but I agree with others - there is no point in avoiding a “break”

0
source

I do not agree with your teacher. There is nothing wrong with using the break statement. There are suitable break places to be used. In fact, the reason that exceptions and exception handling have been introduced into modern programming languages ​​is because you cannot solve every problem using only structured methods.
There are a few things you need to keep in mind.
When an interruption condition is used at the beginning of a block, they act as a precondition as a first condition, so this is good. But when they are used in the middle of the block, with some code around, they act like hidden traps, so this is bad.
Now back to your code. Your code is absolutely simple and easy to read.

-one
source

Source: https://habr.com/ru/post/1011528/


All Articles