Yes, I though of that too but the thing is that as far as I know, there is
no such thing as a "do" existing by itself so something like the code
below won't compile:
Do
{
/// code goes here
}
Having said that, I would had imagine that the compiler could easily
figure out that a "do" must have a "while" just like a "{" must have a
"}". Therefore anything after a "while" that is part of a "do" should be
treated not being part of the "while"
Nicholas Paldino said:
Rene,
Well, I feel it is necessary because if you didn't terminate, and you
did something like this:
x = 0;
do
{
x = x + 1;
} while (x < 3)
// Ambiguous code block
{
Console.ReadLine();
}
What is the compiler suppose to do? Does it associate the ambiguous
code block with the while statement above it?
That's why the semi-colon is needed.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Rene said:
Thanks Nicholas,
So are you saying that is not really necessary but the language rules
dictate that you should so therefore you have to? or something like
that?
in message Rene,
Mostly because the while statement can be used like so:
while (x < 3)
{
}
Because the statement or statement block is not the last part of the
do/while statement, you need the semi-colon.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Quick question, what is the point for forcing the semicolon at the end
of the while statement? See example below:
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.