Thread: how would you make a for loop infinite?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    how would you make a for loop infinite?

    thank

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you think about it for 0.45 seconds, you'll figure out how to do it.

    It will take a lot longer to figure out why you think you want to.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Code:
    for(;;)

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Is there any advantage (I mean in terms of CPU efficiency etc) over doing:
    Code:
    for(;;)
    Over something like:
    Code:
    while(1)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since neither of them require any work, I'm pretty sure they would be exactly the same.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    Since neither of them require any work, I'm pretty sure they would be exactly the same.
    Sometimes guessing isn't enough.

    Code:
    for(;;);
    0041298E  jmp         main+1Eh (41298Eh) 
    
    while(1);
    00412990  mov         eax,1 
    00412995  test        eax,eax 
    00412997  je          main+2Bh (41299Bh) 
    00412999  jmp         main+20h (412990h)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EOP
    Sometimes guessing isn't enough.
    You probably did not enable optimisations, since modern compilers are smart enough to recognise that while(1) is an infinite loop and avoid evaluating its condition as a trivial optimisation.
    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

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Another way is to use a do ... while(1), and yet another way is to use that language construct starting with g that I shall not speak of.
    I find that for(;;) is the best though because with certain compilers on high warning levels you can get a warning about a conditional expression being constant with the while's.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You have to use two of those g statements if you wish to break out of that loop.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    #include <stdio.h> 
    
    int main () { 
    label:	
    	goto label ; 
    	return 0;
    }
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *gasp* He dare speak such treacherous words in the form of example!?

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Is this like Voldemort - aka, he whose name is not spoken?
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    iMalc said he would not speak of goto statements by name, so I ran with it. Then your very next post was a goto example. So perhaps we can call you Gotomort?

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by iMalc View Post
    I find that for(;;) is the best though because with certain compilers on high warning levels you can get a warning about a conditional expression being constant with the while's.
    It also saves 1 keystroke for those lazy people out there.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by master5001 View Post
    *gasp* He dare speak such treacherous words in the form of example!?
    I know it's off topic, but here's an even more unspeakable command:

    COBOL Alter Verb

    We now move on to the early COBOL ALTER verb that probably deserves the award for the worst programming language command.
    http://www.gavilan.edu/csis/language...l#_Toc76036283

    When I learned COBOL in the late 80's I asked my teacher if we may use alter in structured programming - he got almost a heart attack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM