# Do....while Loop question

Printable View

• 01-25-2012, 03:43 AM
Roter
Do....while Loop question
This is a pretty simple question, and Im kindof a newbie at java.
I have this prime finder, but Im not sure how to find the next greatest prime, heres the code so far
Code:

```public class Prime {     public static void main(String[] args) {         long N = 15;         boolean isPrime = true;         if (N < 2) isPrime = false;                for (long i = 2; i*i <= N; i++) {             // if i divides evenly into N, N is prime, so break out of loop             if (N % i == 0) {                 isPrime = false;                 break;             }         }         do {                 N += 1;         } while (!isPrime);               if (isPrime) System.out.println(N + " is prime");     } }```
I was just wondering how to make the while (!isPrime) statement stop once it has found the next greatest prime. Any Help would be much appreciated
• 01-25-2012, 04:10 AM
diamonddragon
Re: Do....while Loop question
What is that You want with
Code:

```        do {             N += 1;         } while (!isPrime);```
• 01-25-2012, 04:21 AM
Roter
Re: Do....while Loop question
I want it to stop the search once it hits isPrime is true
• 01-25-2012, 04:49 AM
diamonddragon
Re: Do....while Loop question
Have You considered to put that condition in for loop?
• 01-25-2012, 04:53 AM
Roter
Re: Do....while Loop question
Sorry, Can you point me in the direction for that extra condition? I don't know quite yet what that would look like
• 01-25-2012, 05:10 AM
diamonddragon
Re: Do....while Loop question
What is the difference between isPrime in for loop, and isPrime in while loop?
• 01-25-2012, 05:14 AM
Roter
Re: Do....while Loop question
Nothing, Same function, The for loop checks for if its prime, I want the do while loop to find the next prime if the number N is not prime
• 01-25-2012, 05:19 AM
diamonddragon
Re: Do....while Loop question
Is same, can You avoid using isPrime in while loop?
• 01-25-2012, 05:22 AM
Roter
Re: Do....while Loop question
I guess, but what would that help with? its just the boolean operand, N is meant to be the prime
• 01-25-2012, 05:25 AM
diamonddragon
Re: Do....while Loop question
Try to throw out that while loop, and say what happened?

Also, using this:
Code:

`for (long i = 2; i*i <= N; i++)`
You will not check prime number.
Do You know why?
• 01-25-2012, 05:49 AM
Roter
Re: Do....while Loop question
I just replaced the program prime checker with a loop that finds next greatest prime regardless, thanks for helping though, I really appreciate it