Thread: Infinite 'for' loop

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    16

    Infinite 'for' loop

    Code:
    int i = 1;
    for ( ; ; )
         if (i >= 10)
              break;
         else
         if (i % 2)
              i += 2;
         else
              i -= 2;
    printf("%d", i);
    I don't know how this code works, but the output is 11 so I guess the condition (i%2) is true so i is incremented by 2 until it's 11, which is >=10 and then the loop exits with the break statement.
    Can anyone give me some details about how this code is executed please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You seem to have the basic idea right though. What exactly are you having problems with?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    16
    Quote Originally Posted by laserlight View Post
    You seem to have the basic idea right though. What exactly are you having problems with?
    Thanks for clarifying my guess. I just don't know what (i%2) means, and why it's true in this case.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    i%2 is the remainder on dividing the integer i by 2. If i is a multiple of 2, i%2 is zero. Otherwise it is 1.

    Depending on age of your compiler, there are some caveats if i is negative (since some aspects of integer division involving negative values were implementation-defined in the 1989 C standard and earlier, although this was changed in the 1999 C standard).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    16
    Quote Originally Posted by grumpy View Post
    i%2 is the remainder on dividing the integer i by 2. If i is a multiple of 2, i%2 is zero. Otherwise it is 1.

    Depending on age of your compiler, there are some caveats if i is negative (since some aspects of integer division involving negative values were implementation-defined in the 1989 C standard and earlier, although this was changed in the 1999 C standard).
    Yes, but I still don't understand the if statement, I've seen if (i%2 == 0) .... but not if(i%2)

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    When doing any logical test, a zero value is false, and a non-zero value is true.

    So "if (i%2)" is shorthand for "if (i%2 != 0)". This is true in all conditionals (loop conditions, ternary ?:, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    16
    Quote Originally Posted by grumpy View Post
    When doing any logical test, a zero value is false, and a non-zero value is true.

    So "if (i%2)" is shorthand for "if (i%2 != 0)". This is true in all conditionals (loop conditions, ternary ?:, etc).
    Great explanation, thank you!

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    505
    Instead of telling you the answer, I'll tell you how to get the answer.

    Simply put in diagnostic printfs(). For each pass through the loop, print out i. You can also put in "Here"s to indicate which branch of the if is being taken.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite Loop
    By ASCII in forum C Programming
    Replies: 10
    Last Post: 09-03-2011, 09:11 AM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. Cant get out of infinite while loop
    By elusive in forum C++ Programming
    Replies: 12
    Last Post: 04-13-2005, 10:34 AM
  5. infinite loop !
    By black in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2002, 08:14 PM