What is the difference between infinite while loops and for loops?

前端 未结 6 1785
忘掉有多难
忘掉有多难 2020-12-18 04:24

I see the different conventions used in many books I had read, where you would create infinite loops with either loop structure such as:

while()
   foo();
fo         


        
6条回答
  •  清歌不尽
    2020-12-18 04:48

    Here is one small difference I saw with the VS2010 disassembly in debug mode. Not sure, if it is sufficient enough to count as a significant and universally true difference (across all compiler and with all optimizations).

    So conceptually these loops are same, but at a processor level, with infinite message loops, the clock cycles for the additional/different instructions could be different and make some difference.

       while(1)
    004113DE  mov         eax,1                       **// This is the difference**
    004113E3  test        eax,eax                     **// This is the difference**
    004113E5  je          main+2Eh (4113EEh)  
          f();
    004113E7  call        f (4110DCh)  
    004113EC  jmp         main+1Eh (4113DEh)          **// This is the difference**
       for(;;)
          f();
    004113EE  call        f (4110DCh)  
    004113F3  jmp         main+2Eh (4113EEh)          **// This is the difference**
    } 
    

提交回复
热议问题