Thread: for(;;) vs. while(true)

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    for(;;) vs. while(true)

    Just curious, is there any difference at all, or are both represented the same when put into asm?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not compile and see for yourself? It'll be compiler dependant, even down to the optimisation settings you use.

    There's another thread going this way on the C forum.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    I use while(true), or while(1)

    I don't think I've ever seen for(;;) in a real program... only in books. Not that I've studied that many real programs!

    To me, while(true) just looks better!

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I personally only use while(true), but I wanted to know in case of an argument with my teacher or something. Knowledge is power

    Hammer, when you say compile and see for myself, do you mean run it in a profiler or do you mean find some way to look at the assembly myself? And I'll check out that thread you're talking about
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    Post

    I've seen this used with looping thru a menu for example
    sort of like :
    Code:
    for (; ; ) ...                      
    { 
    int choice = menu( );      
       switch(choice)              
       {
       case ( 1 ):
       some function( );
       break; 
       case ( 2 ):
       some other function();
       break;
       etc.
         etc.
           etc.
    That way the break statements gets you out of the infinite loop &
    you can go thru as many case statements as you want....
    If you had quite a few choices "while (true)" would lead to a lengthy "If statement"
    the infinite for(; ; ) loop lets you use a switch statement instead..

  6. #6
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Hammer, when you say compile and see for myself, do you mean run it in a profiler or do you mean find some way to look at the assembly myself? And I'll check out that thread you're talking about
    Using something like MSVC you can look at the ASM while debugging. There are many compiler products that offer this feature.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    the infinite for(; ; ) loop lets you use a switch statement instead..
    What? You mean you can't put a switch statement inside a while loop? I'm not so sure about that, but I'll test it out... And also are you sure break will break out of both? I thought it would only break out of the switch, and if it DID break out of both, I would think that it would act in the same way with the while

    Using something like MSVC you can look at the ASM while debugging. There are many compiler products that offer this feature.
    Ok, thanks. But if I try compiling on Release settings, I won't have the debugger anymore. Is there some easy way to get the assembly from non-debug-mode executables, or will I need to get a decompiler of some sort?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Ummm... there are some apps that will convert the binary to ASM, since the conversion is rather easy. I don't know the names of any... some sort of hex editor/"disassembler" though
    Away.

  9. #9
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    the infinite for(; ; ) loop lets you use a switch statement instead..
    You know what, so does a while loop.

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Hopefully your compiler would optimize the two to the same thing.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    While(1) because im lazy and dont want to type true. When i read it outloud i always say "Loop Forever". I dont think there should be any major difference between while(1) and for(;. true is just an enumeration of 1 isnt it? A literal 1? or am i mistaken...

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hopefully your compiler would optimize the two to the same thing.
    Ok, I was hoping it would be that. But just in case, you know... like, if doing the while might require a check of some sort while the for would just loop without checking anything, maybe the for would be marginally more efficient or something. I'm about to grab a disassembler from somewhere though, I guess I'll have to see if there's any difference myself (even though I won't have a clue what's going on in the assembly )
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    Arrow

    Well yes you could you a switch statement in a while loop, but somebody said they had never seen a for(; ; ) loop used in a real program and someone wanted to see it used so they could discuss it with their teacher.....Actually with either it depends on how you code it.......Here is the full code with the snippet I posted earlier....A fellow over at source-forge wrote this for me..
    The purpose of the example was to put as many examples of C++ code in one short snippet..... and I think he gave me 5 stars worth...........classes, reference, for(; ; ), whatever..it's in here...great learning example.........
    Code:
     
    // First, notice that the main() calls the function 'OperationWindow( )'
    // that calls the 'menu()' and used in the switch within the 'OperationWindow( )' 
    // function. 
    
    // Note when 'n' is the choice for quit ?... the switch "re-calls" the 
    // function 'OperationWindow( )' to start a new.
    
    // Also in main(), a pointer to mainClass 'mainClass *openNewMain = new mainClass ;'
    // and the using 'new' sets aside the space in memory.
    
    // Last after the program ends, the 'delete openNewMain ; openNewMain = 0 ;'
    // frees the memory... and then sets 'openNewMain = 0 ' so not to leave trash 
    // in memory.
    
    // Notice that no 'system("PAUSE")' has been added to the end of main().
    // ********************************************************************
    // This may not be world class programming, but I put a number of basic
    // things to give an idea or two maybe.
    
    
    #include<iostream>
    
    class mainClass                    	
    {                                  	
    	public:                         
    	mainClass( ){}			
    	~mainClass( ){}                 
    
    	int OperationWindow( );        
    	void saldot ( );               
    	void goback ( );               
    	void endcall ( );              
    
    	protected:
    
    	int menu( );                   
    
    };                                 
    
    
    int mainClass::OperationWindow( )
    {
    bool nul_operation = false;
    	for (;;)                       
    	{                              
    	int choice = menu( );          
    	                               
    	                               
    	                               
    	                               
    	switch(choice)              
    		{
    		case ( 1 ):
    		saldot( );
    		break; 
    		case ( 4 ):
    		nul_operation = true; 
    		std::cout << "Quit Y-OR-N ? ";
    		char con; std::cin >> con;
    		if(con == 'y' || con == 'Y'){}
    		else OperationWindow( ); 
      		break; 
    		default:
    		goback( );
    		break;
    		} 
    		if ( nul_operation )
    		break;
    	}
    }
    
    
    int mainClass::menu( )
    {
    int choice;
    
    std::cout << "\n mainClass Demo\n";
    std::cout << "(Enter (1)> The only Choice !.\n";
    std::cout << "(Enter (4)> Quit.\n\n";
    std::cout << "|Here> ";
    std::cin >> choice;
    
    return choice;
    OperationWindow( );     
    }
    
    void mainClass::saldot( ) 
    {
    system("CLS");
    std::cout <<"\n What ? ... Press Any Key to continue"<<"\n";
    system("PAUSE > NUL");
    }
    
    void mainClass::goback( ) 
    {
    system("CLS"); 
    std::cout <<"\nNot a selection... try again ... Press Any Key to continue"<<"\n";
    system("PAUSE > NUL");
    } 
    
    void mainClass::endcall( ) 
    {
    system("CLS");
    std::cout <<"\n!!! A Request to close this program has been made ... close ?"<<"\n";
    system("PAUSE > NUL");
    }
    
    
    int main( )
    {
    mainClass *openNewMain = new mainClass ;
    
    openNewMain->OperationWindow( );
    delete openNewMain ; openNewMain = 0 ;
    
    return 0;
    }
    
    
    
    //j@ck_
    Heh !
    This all started over How to make a counsel app stay up without using system("pause") or getch() or cin.get() etc.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Mm, well, I wasn't asking to see an example of for(;; ) in code, I was asking if there was any difference between it and while(1) () when compiled (with full optimizations I meant, although I didn't say so). Anyways, I don't see where the "reference" is in that code, or for that matter, why a pointer to a mainClass is used instead of a simple object of it. And besides, I don't agree with this:
    and then sets 'openNewMain = 0 ' so not to leave trash in memory.
    It's only setting a pointer which will never be used again anyways to NULL, the delete line is what got rid of the trash in memory. I appreciate your good intent though

    **EDIT**
    Ok, I give up with the disassemblers. Both of the 2 that I tried had problems; the first was missing some debugging DLL, and the second was a trial version and said something about the test-file not being a valid hex file or something... I thought a hex file is just a normal text/binary/whatever file that you've opened in a hex editor or something??
    Last edited by Hunter2; 09-04-2003 at 10:02 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    and then sets 'openNewMain = 0 ' so not to leave trash in memory.
    You should set it to NULL not 0. The only thing that this actually does is prevents you from accidently dereferencing a pointer that isn't pointing anywhere. The compiler should pick up the error.

    It is also worth reading this link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. best way to brake a while(TRUE) loop?
    By g1i7ch in forum C Programming
    Replies: 6
    Last Post: 07-10-2006, 02:58 AM