maybe a funny question about 'for'

This is a discussion on maybe a funny question about 'for' within the C++ Programming forums, part of the General Programming Boards category; hi . maybe it is funny but I realy cant undrestand why the program go to loop with this instruction ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    177

    Question maybe a funny question about 'for'

    hi .
    maybe it is funny but I realy cant undrestand why the program go to loop with this instruction
    for(i=0;i=5;++i)

    when it is
    for(i=o;i<5;++i)
    program checks : if i<5 then it does the loop instructions and then ++i
    else it doesnt .

    when it is
    for(i=0;i=!5;++i)
    while i isnt 5 the program does the loop instructions and each time ++i

    I think when it is "for(i=0;i=5;++i)" PC shoulnt do the loop instruction
    (cause i is 0 and i!=5) , but it does .

    can u plz explain for me why the program does that and goes to loop ?

    TNX

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    I think when it is "for(i=0;i=5;++i)" PC shoulnt do the loop instruction
    (cause i is 0 and i!=5) , but it does .
    it loops because you
    set 5 to i and it loops until 'i' is not true...
    i think you have meant to write: for(... ;i == 5; ...) { ... } to test is 'i' 5 or something else...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > for(i=0;i=5;++i)
    This loops forever, it is interpreted as
    for(i=0;(i=5)!=0;++i)
    Since the test is always in effect 5 != 0, it will loop forever

    > for(i=0;i=!5;++i)
    You mean
    for ( i = 0; i != 5; ++i )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    thanks alot for ur help . I undrestand .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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