Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:58:50+00:00 2026-05-16T00:58:50+00:00

I have often heard that using break s in Java is considered bad practice,

  • 0

I have often heard that using breaks in Java is considered bad practice, but after reading some threads on Stack Overflow, I’ve seen otherwise. Many say that it is acceptable in certain cases.

I’m a little confused as to what is/isn’t bad practice in this case.

For Project Euler: Problem 7, I’ve constructed the code below. The challenge was to find the 10001st prime.

int index = 2, count = 1, numPrime = 1;

while (true) {
    index++;

    if (isPrime(index)) {
        count = index;
        numPrime++;
    }

    if (numPrime >= 10001)
        break;
}

System.out.println(count);

This returns the correct answer (in 21ms), but am I overlooking a serious warning? It’s 100% possible to create a while loop without a break, but I find that this is a little easier to follow.

Is the way I use the break; bad practice? I know that there’s always a way around using one, but is it really that terrible here?

Many thanks

Justian

EDIT

Here’s my isPrime() code. I might as well optimize this while I’m at it.

public static boolean isPrime(long num) {  
    if (num == 2)
        return true;

    if (num % 2 == 0 || num <= 0)
        return false;

    for (long i = 3; i * i <= num; i += 2)
        if (num % i == 0)
            return false;

    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:58:50+00:00Added an answer on May 16, 2026 at 12:58 am

    In this case, it looks to me like it would be easier just to change the while condition:

    while (numPrime < 10001) {
    

    That’s usually the case when a while(true) loop ends with

    if (condition)
    {
        break;
    }
    

    … although you need to check whether anything else in the body of the loop performs a continue.

    Alternatively, you could restructure it slightly:

    int current = 1;
    for (int i = 0; i < 10001; i++)
    {
        current++;
        while (!isPrime(current))
        {
            current++;
        }
    }
    

    Then current will be the answer at the end.

    I generally prefer a for loop over a while loop, when you’re trying to do something a particular number of times. In this case the “something” is “find the next prime”.

    There are various bits of dogma in programming that I feel are taken too far – including “one exit point to a method” and “don’t use break.” Write code as readably as you can. If you look at some code and feel it’s not blindingly obvious what’s going on, try to work out other ways of structuring it. Sometimes that’s a case of changing a loop; sometimes it’s extracting a method; sometimes it’s inverting some logic (deal with a negative branch first, possibly exiting early, and then handle the normal case).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have often heard complaints against Java for not having unsigned data types. See
I've often heard the argument (in javascript, but many languages have an eval-like feature)
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have often heard people talking about hashing and hash maps and hash tables.
I often have a situation in my Java code when I need to set
After doing some reseach on how to break through a secondary loop while (true)
I have heard of several things, quoted from Wikipedia: Java Runtime Environment, A JVM
I have some image processing code that runs on a background thread and updates
I'm sure that all of us have had to deal with telecommuters at some
I do some numerical computing, and I have often had problems with floating points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.