Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: hi. i want to rewrite this do loop into a while loop.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default hi. i want to rewrite this do loop into a while loop.

    hi help me rewrite the following do loop into a while loop.


    int n = 1;
    double x = 0;
    double s ;
    do
    {
    s = 1.0 / (n * n);
    x = x + s;
    n++;
    }
    while (s > 0.01);


    is double s given a default value when it was declared?

    s is getting smaller and smaller...

    hmm, i'm confused!


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: hi. i want to rewrite this do loop into a while loop.

        int n = 1;
        double x = 0;
        double s ;
     
        do
        {
            s = 1.0 / (n * n);
            x = x + s;
            n++;
        } while (s > 0.01);

    Yes, all primitives will receive a default value of zero if they are numbers or false if its a boolean.

    The reason for s going smaller and smaller is the fact that s equals 1.0 divided by (n * n). For each loop you increment n which means that s will be 1.0 divided by a bigger number every time.

    1.0 / 1 = 1.0
    1.0 / 4 = 0.25
    1.0 / 9 = 0.11
    and so on.

    The only difference with using a do/while loop and a normal while loop is that the do/while loop will always be run at least once. The normal while loop will always check the condition before it executes the code block.

        int n = 1;
        double x = 0;
        double s ;
     
        while(s > 0.01)
        {
            s = 1.0 / (n * n);
            x = x + s;
            n++;
        }

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    etidd (January 26th, 2010)

  4. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: hi. i want to rewrite this do loop into a while loop.

        int n = 1;
        double x = 0;
        double s ;
     
        while(s > 0.01 || n == 1)
        {
            s = 1.0 / (n * n);
            x = x + s;
            n++;
        }

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    etidd (January 26th, 2010)

  6. #4
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: hi. i want to rewrite this do loop into a while loop.

    yep, sure got it... figured it out myself shortly after posting this. it's great to voice it out to figure it out!

Similar Threads

  1. Do While Loop
    By connex in forum Loops & Control Statements
    Replies: 6
    Last Post: December 7th, 2009, 03:54 PM
  2. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  3. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  4. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM