The reason behind the ; is written after loop statement.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Starx
    New Member
    • Oct 2015
    • 1

    The reason behind the ; is written after loop statement.

    Why semi colon is written after loop statement, in c.
  • zuko32
    New Member
    • Oct 2015
    • 13

    #2
    There is actually a loop statement that we have to write ; and this is the do-while loop that exists in a lot of languages and has this form:
    Code:
    do{           
       statements;                    
    
    }while( condition );
    Now, for loops like for, while, if they are empty, i.e. they have no statements in the body, they should be terminated with a ;.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The semi-colon must appear at the end of each statement. The do-loop statement is not complete until the ) of the while so you need a semicolon to tell the compiler the statement is complete.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        @starx please show us an example of the kind of statement you're asking about.

        Comment

        • AX3M
          New Member
          • Oct 2015
          • 1

          #5
          Either of two reasons

          I assume, by "after", you mean "at the end (of a loop statement)."

          The reason a semicolon (;) is written "after" or "at the end of" a loop statement is either that...

          a. It is a do-while loop.
          Code:
          do
             x += y;
          while (x < y);
          b. For other types of loops, it or the last part of the loop has only one line of code to execute, in its body.
          Code:
          if (x < y)
             x += y;
          I hope this is the answer you're looking for.

          Comment

          Working...