Results 1 to 3 of 3

Thread: Do While Loop

  1. #1
    s4rd59 is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Do While Loop

    Hi,
    I am new to Java and would appreciate it if someone could me with the following:

    Write a do while loop so it calculates: 1; 1+2; 1+2+3; 1+2+3+4; 1+2+3+4+5.

    At the moment, I have done a basic do while loop for displaying 1 to 5.

    public static void main(String[] args){
    int i =1;
    do
    {
    System.out.println(+ i);
    i++;
    }while(i <=5);
    }
    }


    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Declare an int variable (I'd call it "sum") before the do-while loop and add i to the sum value inside of the loop.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    So yeah, code tags, read about them :D
    As per your question, I'm a bit confused, do you want to calculate the sum of 1, 1+2, 1+2+3, or do you want to print out the expression without calculating? If all you want to do is the sum, you were pretty well on your way, not much else needs to be done to your code.

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 09:45 PM
  2. using for loop...pls help
    By princess.blue in forum Eclipse
    Replies: 2
    Last Post: 12-13-2009, 09:30 AM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  4. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM
  5. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 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
  •