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: first itteration of "for" statement acts differntly to all the rest, why?

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Lightbulb first itteration of "for" statement acts differntly to all the rest, why?

    Dear All I have the following code and it works, just trying to understand the results.
    public class Counter
    {
    public static void main(String[] args)
    {
    int value1 = 7;
    for (int i = 2, sum = 0; i <= value1; i += i, sum = i)
    {
    System.out.println("Sum = " + sum + "  i = " + i );
    }}}
    OUTPUT OF CODE:
    Sum = 0 i = 2
    Sum = 4 i = 4

    QUESTION
    So why, on the first go around of the "for" statement, is "sum" not equal to 2?

    MY ANSWER TO QUESTION
    If the third part of the "for" statement(; i += i, sum = i) is being ignored in the first iteration of the loop, the results I see make sense?

    FURTHER EVIDENCE FOR MY ANSWER
    This also makes sense, because the first part of the "for" statement is being ignored after the first iteration of the "for" loop, otherwise "sum" and "i" would be set back to there original values.

    SECOND QUESTION:
    Am I correct in my assumptions?

    SPACE MONKEY
    Last edited by SPACE MONKEY; March 1st, 2011 at 10:58 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: first itteration of "for" statement acts differntly to all the rest, why?

    What is value1 supposed to be? Better yet, post an SSCCE that we can copy and paste to play with.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: first itteration of "for" statement acts differntly to all the rest, why?

    Dear KevinWorkman:

    Sorry "value1" has been set, it is there to set the upper limit for "i", to indicate when the "for" statement should stop running.

    I made it so I could set "value1" to different values. I have turned "value1" into a fixed value as it is not integral to the question I have posed.
    Last edited by SPACE MONKEY; March 1st, 2011 at 10:58 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: first itteration of "for" statement acts differntly to all the rest, why?

    Quote Originally Posted by SPACE MONKEY View Post
    So why, on the first go around of the "for" statement, is "sum" not equal to 2.
    The answer to that question is because "the third part" (the increment expression) of the loop executes after each iteration of the loop, not before.

    Recommended reading: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I'd also recommend you step through this with a debugger. That will make it pretty obvious what's going on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    SPACE MONKEY (March 1st, 2011)

  6. #5
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: first itteration of "for" statement acts differntly to all the rest, why?

    Dear KevinWorkman:

    Thanks. I will use the tutorials first next time.
    I can not find the way to mark the post as completed, or answered. That option only appears in the initial post and not afterwords.

Similar Threads

  1. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 08:13 AM
  3. Replies: 1
    Last Post: March 31st, 2010, 10:42 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 12:54 PM