1. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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
XML 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

2. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Do....while Loop question

What is that You want with
Java Code:
do {
N += 1;
} while (!isPrime);

3. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## Re: Do....while Loop question

I want it to stop the search once it hits isPrime is true

4. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Do....while Loop question

Have You considered to put that condition in for loop?

5. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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

6. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Do....while Loop question

What is the difference between isPrime in for loop, and isPrime in while loop?

7. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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

8. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Do....while Loop question

Is same, can You avoid using isPrime in while loop?

9. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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

10. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Do....while Loop question

Try to throw out that while loop, and say what happened?

Also, using this:
Java Code:
for (long i = 2; i*i <= N; i++)
You will not check prime number.
Do You know why?
Last edited by diamonddragon; 01-25-2012 at 05:27 AM.

11. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•