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

Thread: For Loop with condition

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Loop with condition

    [public static void PrintNum () {

    int i;
    int j = 0;

    for (i =100; j<5; i=i)j=100-(i-=1);
    System.out.println(i);

    } ]
    I'm not following how it calculated to 95 since I'm confused with this
    part i=i)j = 100-(i-=1);

    Thank you in advance for clarification
    Last edited by mindful314; May 11th, 2014 at 02:07 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop with condition

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loop with condition

    [public static void PrintNum () {

    int i;
    int j = 0;

    for (i =100; j<5; i=i)j=100-(i-=1);
    System.out.println(i);

    } ]

    I has initial value 100, the condition is less than 5 with i=i. After that I have no idea

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop with condition

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    To see what the code is doing add a println() statement first thing inside of the loop that prints out the value of i and j so you can see how they change as the loop executes.

    One trick the coder has used is the fact that the value of an assignment state is returned by the statement. For example:
      int x, y;
      x = (y = 2);   //  assigns y a value of 2 and returns that 2 to x
      //  here x is 2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loop with condition

    What you are saying the condition depends on J's value as j = 100-(99)...(98)..(97)..(96)...(95) <5 which will stop when it hits 5. I think I have little better understanding.

    Thanks a lot

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For Loop with condition

    The loop continues as long as (j < 5) is true.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] If condition not working.
    By Variumzky in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2013, 01:03 AM
  2. [SOLVED] Conditionals with more than one condition.
    By mstabosz in forum Java Theory & Questions
    Replies: 4
    Last Post: July 12th, 2013, 06:11 AM
  3. condition number? what is it?
    By gspease839 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 9th, 2013, 10:00 PM
  4. Have Problem with if condition...Please Help me...
    By Jalpesh Ruparel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:54 AM
  5. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM

Tags for this Thread