Results 1 to 6 of 6
  1. #1
    gt11990 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default How can I rewrite the following while loop using a for loop?

    int count=0, sum=0;
    while (count<5)
    {

    sum = sum + count;
    System.out.println(sum);
    count++;

    }

    thanks :)

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by gt11990 View Post
    int count=0, sum=0;
    while (count<5)
    {

    sum = sum + count;
    System.out.println(sum);
    count++;

    }

    thanks :)

    Java Code:
    		int count=0, sum=0;
    		for (int i=0; i < 5; i++) {
    			sum += i;
    		}
    Last edited by j2me64; 04-30-2010 at 02:20 PM.

  3. #3
    gt11990 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    can you explain (if possible) how you converted it into the for loop am really confused and new to this

    thanks

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by gt11990 View Post
    can you explain (if possible) how you converted it into the for loop am really confused and new to this

    easy, take the syntax of the for-loop

    Java Code:
    for (initialization; termination; increment) {
        statement(s)
    }

    and then replace the initialization, termination and increment according to your needs. your count was initialized with 0 so then initialization is 0, your while loop contain count < 5 so the termination is also i < 5 and the increment is 1 because your increment was count++

  5. #5
    gt11990 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Ohh i see thank you very much, this java is so complicated :)

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by j2me64 View Post
    Java Code:
    		int count=0, sum=0;
    		for (int i=0; i < 5; i++) {
    			sum += i;
    		}
    That's close, and your explanation is correct, but I think this is a more accurate translation:

    Java Code:
            for (int count = 0, sum = 0; count < 5; count++) {
                    sum += count;
                    System.out.println(sum);
            }
    Actually, that is probably what the instructor wants, but it's not really an accurate translation, because the count and sum variables will not survive the loop, as they do in the while case. This code works the same as the while loop, but is ugly:

    Java Code:
            int count, sum = 0;
            for (count = 0; count < 5; count++) {
                    sum += count;
                    System.out.println(sum);
            }
    -Gary-
    Last edited by gcalvin; 04-30-2010 at 06:09 PM.

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 10:45 PM
  2. do while loop?
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 11:32 AM
  3. For Loop
    By YiBoog in forum New To Java
    Replies: 6
    Last Post: 11-11-2009, 08:53 PM
  4. Loop Help!
    By Keno777 in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 02:11 AM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM

Posting Permissions

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