Results 1 to 11 of 11
  1. #1
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Do....while Loop question

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

  3. #3
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Do....while Loop question

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

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Do....while Loop question

    Have You considered to put that condition in for loop?

  5. #5
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Do....while Loop question

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

  7. #7
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Do....while Loop question

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

  9. #9
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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. #11
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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

Similar Threads

  1. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 03:26 AM
  2. For loop syntax question
    By salad90 in forum New To Java
    Replies: 20
    Last Post: 12-07-2010, 07:28 AM
  3. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 09:29 PM
  4. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 06:08 PM
  5. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-16-2009, 12:23 AM

Posting Permissions

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