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 5 of 5

Thread: While (logical confusing output)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default While (logical confusing output)

      public static void main(String[] args) {
     
            int sum = 0; int number = 0;
     
            while (number <= 10) {
     
                sum = sum + number;
                number = number + 1;
            }
     
            System.out.println(sum);
            System.out.println(number);
        }

    whye is the output like this ?
    55
    11

    where doest the 55 came from? it supposed to add continuesly and it supposed to be 10 not 55..
    duh?..


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: While (logical confusing output)

    Sum is essentially 1+2+3+4+5+6+7+8+9+10 = 55 (for each pass through the loop: 0+0, 0+1, 1+2, 3+3, 6+4, 10+5, etc....), and the final setting for 'number' is 10+1 = 11.

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

    chronoz13 (December 20th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: While (logical confusing output)

    it doesnt mathematically added?

    it just simply concatenate? 5 and 5?

    sori for using the word Concatenate... because its not string...

    what i mean is... it doesnt add.. it just display the value 5 together with another 5...
    Last edited by chronoz13; December 19th, 2009 at 11:59 PM.

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: While (logical confusing output)

    and one more thing sir.... its not fifty-five? ryt? its five and five?

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: While (logical confusing output)

    never mind.. i've already solved it..... using pen and paper.... i just misunderstood the flow

Similar Threads

  1. non well-formed output
    By luca.santaniello in forum Web Frameworks
    Replies: 1
    Last Post: November 19th, 2009, 02:52 AM
  2. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM
  3. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  4. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 06:58 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM