clear for loop doubt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rampraveen
    New Member
    • Apr 2010
    • 37

    clear for loop doubt

    for(;;)....what is the meaning for this loop...please tell....
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Well, it's a for loop without any initialisation code, condition, nor post-loop action. It's an infinite loop.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      It is equal to:

      "while (true)"

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        No, because 'true' is a condition which must continue to be fulfilled for the loop to continue. Whereas with for(;;) some other statement in the body of the loop is required to stop it

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Thought of posting that too, but in the pseudocode sense (probably what the OP wants), it is the same.

          If the compiler can optimize the instruction, it is the same. If not, it's an extra line of assembly.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by whodgson
            No, because 'true' is a condition which must continue to be fulfilled for the loop to continue. Whereas with for(;;) some other statement in the body of the loop is required to stop it
            If you use the literal boolean true, the loop's condition will remain true forever. I don't see how there is a difference between while (true) and for(;;). You can't change the condition of the loop in the loop's code. You can only get out of the while(true) loop by having a break or goto or something like that.

            Unless you are comparing while(true) to something like for(;true;). Which I guess is syntactically a bit closer but conceptually the same as for(;;).

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Assembly pseudo
              Code:
              a: and b: represent addresses in assembly and are not actually instructions.
              
              for(;;):
              a: // start of loop address, first line inside for loop code
              ...rest of code
              goto a
              b: continuation of code.
              
              while(true):
              a: if 0 neq 0 goto b
              first line of code
              rest of code
              goto a
              b: continuation of code.
              
              OR (depending on compiler)
              goto b:
              a: first line of code
              ...rest of code
              b: if 0 eq 0 goto a
              continuation of code.
              One line difference.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                jkmyoung,

                I don't think anyone is disputing the difference in assembly. But the quote was:

                No, because 'true' is a condition which must continue to be fulfilled for the loop to continue. Whereas with for(;;) some other statement in the body of the loop is required to stop it

                which leads me to believe that the original poster of this quote thinks there is a difference in the c/c++. If we are all taking about the differences in assembly then I think we all agree.

                Comment

                Working...