1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Do While loop syntax

Discussion in 'C#' started by codeunlock, Apr 29, 2007.

  1. codeunlock

    codeunlock New Member

    Joined:
    Apr 18, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Quick question, what is the point for forcing the semicolon at the end of
    the while statement? See example below:

    Code:
    x = 0;
    do
    {
      x = x + 1;
    }while (x < 3);
    What's the point of having the semicolon after the (x < 3)? Why can't the
    compiler figure out that's the end of the while statement without the need
    for the semicolon?

    Thank you.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Thats a very interesting question and thought of answering the question as thats how its been designed but then thought about it and I came to this.

    All the lines in the code gets converted to assembly or some other intermediate langauge. Now when a while loop start line is something like
    Code:
    while (x < 3)
    but if we have the same thing in the do while loop i.e. without a semicolon there can be potential problems in while statements inside the do while loop. Compiler may become dependent on the braces { } to know if its start / End which is not also guarantee in statement having only one statements and so we dont have braces.
     

Share This Page