Thread: Simple loop clarification

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Simple loop clarification

    I was given a couple pieces of code that don't make any sense to me. I was hoping you could explain where my comprehension fails.

    Code:
    int row = 0, col = 0;
     
         while(row++ < 10);{
              printf("row = %d\n",row);
         }
    It seems like it should print out
    row = 0
    ...
    row = 10

    But it prints out "row = 11". Why?

    Code:
      int row = 0, col = 0; 
         for(row = 0; row < col; row++); {
              printf("Row = %d\n",row);
         }
    And this shouldn't run at all, because row is not less than col, but it does. Why?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In both examples you have a misplaced semi-colon, which changes the meaning of the code completely from what you appear to expect.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    OOOOOHHHHHH!!! No wonder! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very simple clarification about strings (I hope).
    By figarojones in forum C Programming
    Replies: 3
    Last Post: 11-12-2012, 04:42 PM
  2. Newbie Question, Need clarification on for loop
    By mlabcdzs in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2010, 02:53 PM
  3. Simple Loop
    By blk007 in forum C Programming
    Replies: 6
    Last Post: 05-28-2010, 08:06 AM
  4. Simple loop
    By chaos787 in forum C Programming
    Replies: 3
    Last Post: 02-25-2009, 09:58 PM
  5. for next and if else loop - simple
    By rofler in forum C Programming
    Replies: 3
    Last Post: 06-10-2004, 08:50 PM