0

Working on a task where I should factorize a prime number. Here's the solution I've come up with:

import java.util.Scanner;

public class Task8 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Which number to factorize:");

        int number = input.nextInt();

        System.out.println();

        int counter = 1;

        for (int i = 2; i <= number; i++) {
            while (number % i == 0) {

                if (counter == 1 && i == number) {
                    System.out.println("The number is a prime, can’t be factorized.");
                    break;
                } else {

                    System.out.println("Prime" + " " + "#" + counter + ":" + " " + i);
                    number = number/i;
                    ++counter;
                }
            }
        }
    }
}

However, a book I'm currently studying, strongly advices against using break statements in loops. So how would I do without one in this case?

Cheers!

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Cerdarius
  • 85
  • 1
  • 2
  • 6
  • 2
    Does this book give reasons? – Blorgbeard Jun 27 '16 at 22:49
  • 1
    You could pull the loop out into a method, and use `return` instead. But of course, some people don't like that either (multiple return-points from a method). It's a style question. You'll have to decide for yourself what to do. – Blorgbeard Jun 27 '16 at 22:50
  • 1
    You could add a boolean flag outside of the while loop, include it in the `while` check, and set it inside the loop based on the `if` statement. – Cᴏʀʏ Jun 27 '16 at 22:50
  • Do you have an instructor? Does the instructor advice against it as well? – xiaofeng.li Jun 27 '16 at 22:51
  • @Cᴏʀʏ sure, don't use the `break` keyword, just re-implement the break feature using more code for the same effect :\ – Blorgbeard Jun 27 '16 at 22:53
  • Yes, instructor says the same thing: "When you add a break statement, the loop might end because either the controlling boolean expression is false or a break statement executes. Thus, using a break statement within a loop can make the loop more difficult to understand" – Cerdarius Jun 27 '16 at 22:58
  • I would disagree that using `break` makes a loop more difficult to understand. Compare your original code with the top-voted answer's. Which is simpler? Anyway, it's a matter of opinion. – Blorgbeard Jun 27 '16 at 23:03

4 Answers4

2

Here's one method of doing it. I made comments around my changes:

import java.util.Scanner;

public class Task8 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Which number to factorize:");

        int number = input.nextInt();

        System.out.println();

        int counter = 1;

        for (int i = 2; i <= number; i++) {
            boolean canBeFactored = true; // Add a flag
            while (canBeFactored && number % i == 0) { // Add a check
                if (counter == 1 && i == number) {
                    System.out.println("The number is a prime, can’t be factorized.");
                    canBeFactored = false; // Set that check to false
                } else {

                    System.out.println("Prime" + " " + "#" + counter + ":" + " " + i);
                    number = number/i;
                    ++counter;
                }
            }
        }
    }
}
Dan Smith
  • 763
  • 9
  • 14
0

One possible solution is to do a return in-place of the break. But this will return from the whole function execution, which may not be the desired outcome. Moreover, break statement is not necessarily bad to use. Please refer to this.

Community
  • 1
  • 1
toro
  • 194
  • 5
0

I see nothing wrong with using "break" in this case. Although if you would strongly not like to use it, you can make everything you have in main into a new subroutine, and use "return", like this:

import java.util.Scanner;

public class Task8 {

    public static void main(String[] args) {

        function();

    }

    public static void function() {

        Scanner input = new Scanner(System.in);

        System.out.print("Which number to factorize:");

        int number = input.nextInt();

        System.out.println();

        int counter = 1;

        for (int i = 2; i <= number; i++) {
            while (number % i == 0) {

                if (counter == 1 && i == number) {
                    System.out.println("The number is a prime, can’t be factorized.");
                    return;
                } else {

                    System.out.println("Prime" + " " + "#" + counter + ":" + " " + i);
                    number = number/i;
                    ++counter;
                }
            }
        }
        return;
    }
}
Anon
  • 1
  • 2
0

The book probably means that the excessive use of break (and continue, return too) make the code unreadable.

E.g. Instead of

for (int i = 0; i < N; i++) {
  if (b)
    break;
  ...
}

You could write

for (int i = 0; i < N && !b; i++) { ... }

In the end I think it's just a matter of style and what you want to express with the code.

You can also break outer loops by labelling them:

outer:
for (;;) {
  for(;;) {
    if (<some_special_case>)
      break outer; // execution...
  }
}
// ...continues here

Which can become pretty ugly depending on the scenario without break (additional flag to be set before breaking and checked in all outer loops). So the bottom line is, there are valid use-cases for break where it gets (in my opinion) the job done the cleanest and quickest way (to write the code).

maraca
  • 8,468
  • 3
  • 23
  • 45