Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By grumpy
  • 1 Post By grumpy

Infinite 'for' loop

This is a discussion on Infinite 'for' loop within the C Programming forums, part of the General Programming Boards category; Code: int i = 1; for ( ; ; ) if (i >= 10) break; else if (i % 2) ...

  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
    21,910
    You seem to have the basic idea right though. What exactly are you having problems with?
    drawar likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,433
    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).
    drawar likes this.
    Right 98% of the time, and don't care about the other 3%.

  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,433
    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).
    drawar likes this.
    Right 98% of the time, and don't care about the other 3%.

  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
    333
    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.
    http://www.malcolmmclean.site11.com/www

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21