Results 1 to 11 of 11
Thread: Do....while Loop question
- 01-25-2012, 02:43 AM #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"); } }
- 01-25-2012, 03:10 AM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 7
Re: Do....while Loop question
What is that You want with
Java Code:do { N += 1; } while (!isPrime);
- 01-25-2012, 03:21 AM #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
- 01-25-2012, 03:49 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 7
Re: Do....while Loop question
Have You considered to put that condition in for loop?
- 01-25-2012, 03:53 AM #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
- 01-25-2012, 04:10 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 7
Re: Do....while Loop question
What is the difference between isPrime in for loop, and isPrime in while loop?
- 01-25-2012, 04:14 AM #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
- 01-25-2012, 04:19 AM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 7
Re: Do....while Loop question
Is same, can You avoid using isPrime in while loop?
- 01-25-2012, 04:22 AM #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
- 01-25-2012, 04:25 AM #10
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 7
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++)
Do You know why?Last edited by diamonddragon; 01-25-2012 at 04:27 AM.
- 01-25-2012, 04:49 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Loop question
By rich123 in forum New To JavaReplies: 6Last Post: 02-08-2011, 02:26 AM -
For loop syntax question
By salad90 in forum New To JavaReplies: 20Last Post: 12-07-2010, 06:28 AM -
loop question
By ccie007 in forum New To JavaReplies: 22Last Post: 08-15-2010, 08:29 PM -
for Loop with Yes/No Question! help..please!
By mastercrimson in forum New To JavaReplies: 8Last Post: 06-02-2010, 05:08 PM -
Question about for loop..
By sivakumar_sakam in forum New To JavaReplies: 4Last Post: 05-15-2009, 11:23 PM
Bookmarks