Thread: Why is this an infinite loop!?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Why is this an infinite loop!?

    Hi while writing a bit more code for a little project i'm doing, (trying to teach myself c)

    i wrote this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    int i, a;
    
    for (i = 0; i <= 8; i++)
    {
    	for (a = 0; a <= 8; i++)
    		printf("i = %d\n", i);
    
    }
    
    }
    The output was a little unexpected:

    Code:
    ...
    
    i = 10388
    i = 10389
    i = 10390
    i = 10391
    i = 10392
    i = 10393
    i = 10394
    i = 10395
    i = 10396
    i = 10397
    i = 10398
    i = 10399
    i = 10400
    i = 10401
    i = 10402
    i = 10403
    i = 10404
    i = 10405
    i = 10406
    i = 10407
    i = 10408
    i = 10409
    i = 10410
    i = 10411
    i = 10412
    i = 10413
    i = 10414
    i = 10415
    i = 10416
    i = 10417
    i = 10418
    i = 10419
    i = 10420
    i = 10421
    i = 10422
    i = 10423
    i = 10424
    i = 10425
    i = 10426
    i = 10427
    i = 10428
    i = 10429
    ...
    And so on! I can't understand what i'm doing wrong as i thought the conditions in the for loop would keep it in check. Surely i shouldn't be more than 8? No?

    Thanks in advance from a newb!
    Simon

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for (a = 0; a <= 8; i++)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Hi

    The problem is in the increment part
    First time i will be zero and will enter the second for loop .
    here the condition is a<8 but a is never incremented in this inner for loop and so it keeps incrementing i and prints them all,but will never go to the outer loop and hence the unexpected result.

    Praveen SP

  4. #4
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    I'm sorry, i don't understand.

    i++ ++i and i + 1 all have the same effect.

    i understand that i++ increments 'i' but returns the old non incremented value, but i thought that the condition in the for loop would limit the output?

    i.e i was under the impression that due to the condition 'i' could never be greater than 8?
    regards
    Simon.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah sorry, didn't see that, i++ should have been a++!

    Thanks, feel a bit stupid now!

    So whats the difference between i++ and ++i?

    Si.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by AmbliKai View Post
    So whats the difference between i++ and ++i?
    In C, when alone, nothing.

    When used as an expression, i++ will return the value of i before it was incremented, while ++i will increment i and used the incremented value in the expression.

    Example:

    Code:
    int x = 5;
    int y = x++;
    y will be 5 and x will be 6.

    Code:
    int x = 5;
    int y = ++x;
    Both y and x will be 6.

    In C++, apparently i++ does some copy stuff, which, if you're using primitive data types (ie. int, char, etc.) then this does not apply/matter. This is only really for objects.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Cool, sounds like a useful bit of knowledge to know!

    Thanks everyone.

    Si.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C++, apparently i++ does some copy stuff, which, if you're using primitive data types (ie. int, char, etc.) then this does not apply/matter.
    There was a discussion about this, long, long ago: http://cboard.cprogramming.com/showthread.php?t=68136
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. 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