By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Do While loop syntax

P: n/a
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.

Apr 29 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

Apr 29 '07 #2

P: n/a
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?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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.


Apr 29 '07 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
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?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP06.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>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.



Apr 29 '07 #4

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OT**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>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?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP06.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
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.





Apr 29 '07 #5

P: n/a
Rene,

Just a thought I got reading your code. Why do you use a while loop and not
the easier and better to understand For index in the case of indexes and
enumurating. The while loop is fine to test a real value.

Cor
D
"Rene" <a@b.cschreef in bericht
news:%2******************@TK2MSFTNGP04.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:OT**************@TK2MSFTNGP03.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>>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?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl. ..
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.
>
>
>




Apr 29 '07 #6

P: n/a
well in that case,

Compiler has to associate both "}" and "while..." because when we have
nested while loop inside a do..while; it might become ambiguous (if language
does not have ; at the end of do..while.)

Imagine this code,

do
{
....
while(..)
{
....
}
}while(..)

But this is also not possible.. because {} block can be used for scoping
also..

So, imagine this code (where {} is used for scoping as well as nested while
is also there in code)..

do
{
while(..)
{
.... //some code snippet
{ // I want to scope this portion of code so that my objects are free
after I come out of this block of code..
.... //some code..
}
while(...) //this is my nested while which I want in code
//Now above line is ambiguous.. because compiler will get confused whether
this is end of do..while or just while!! :)
.... ///some code !! compiler will stop on above line I think if they
implement what you are saying, Rene.

Regards,
Jigar Mehta

"Rene" <a@b.cwrote in message
news:##**************@TK2MSFTNGP04.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:OT**************@TK2MSFTNGP03.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>>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?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl. ..
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.
>
>
>



Apr 29 '07 #7

P: n/a
Rene wrote:
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.
That's just how the language is defined. The semicolon terminates every
statement.

There are a lot of things in the code that is not strictly neccesary,
but it makes the code easier to read and reduces the risk of writing
code that does something completely different from what is intended.

For example, the assignment on the first line wouldn't really need to be
terminated by a semicolon, as the do keyword following it can not be
part of the expression. Also the = could be omitted, as it could be
assumed that what follows the variable should be assigned to it.
Removing all kinds of redundancy and introducing default behaviour for
everything that is not specifically stated, would make the code look
something like this:

x 0
{
+
}<3

It would of course be very difficult to write and maintain code like
this. Any change in the code might cause the rest of the code to change
meaning completely.

--
Göran Andersson
_____
http://www.guffa.com
Apr 29 '07 #8

P: n/a

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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?
(1) It's not a while statement, it's a do...while statement.

(2) The compiler could figure out the ends of many kinds of statements, not
just this one, if we wanted to design the language this way. But the
decision was made to put a semicolon at the end of every statement in order
to make it easier for the programmer to remember where the semicolons are
needed.

BASIC simply puts one statement on each line. Pascal uses semicolons
between statements, not after them, which means that the last statement in a
block does not need a semicolon.
Apr 29 '07 #9

P: n/a

"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...
>
"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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?

(1) It's not a while statement, it's a do...while statement.

(2) The compiler could figure out the ends of many kinds of statements,
not just this one, if we wanted to design the language this way. But the
decision was made to put a semicolon at the end of every statement in
order to make it easier for the programmer to remember where the
semicolons are needed.

BASIC simply puts one statement on each line. Pascal uses semicolons
between statements, not after them, which means that the last statement in
a block does not need a semicolon.
Don't forget that C# and Pascal allows statements to run across multiple
lines. Basic requires a line continuation character for statements that run
across multiple lines. The end of statement marker is part of the
language's syntax.

Mike.
Apr 29 '07 #10

P: n/a
Michael A. Covington <lo**@ai.uga.edu.for.addresswrote:

<snip>
(2) The compiler could figure out the ends of many kinds of statements, not
just this one, if we wanted to design the language this way. But the
decision was made to put a semicolon at the end of every statement in order
to make it easier for the programmer to remember where the semicolons are
needed.
That's not *quite* accurate. For instance, there's no semi-colon
required at the end of an if statement, a using statement, a switch
statement, a for statement, or a plain while statement (as opposed to a
do statement).

I believe this is because the productions for all of those end with
"embedded statement", whereas the production for the do statement ends
with a boolean expression.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 29 '07 #11

P: n/a
On Sun, 29 Apr 2007 14:32:51 +0200, Göran Andersson <gu***@guffa.com>
wrote:
>Rene wrote:
>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.

That's just how the language is defined. The semicolon terminates every
statement.

There are a lot of things in the code that is not strictly neccesary,
but it makes the code easier to read and reduces the risk of writing
code that does something completely different from what is intended.

For example, the assignment on the first line wouldn't really need to be
terminated by a semicolon, as the do keyword following it can not be
part of the expression. Also the = could be omitted, as it could be
assumed that what follows the variable should be assigned to it.
Removing all kinds of redundancy and introducing default behaviour for
everything that is not specifically stated, would make the code look
something like this:

x 0
{
+
}<3

It would of course be very difficult to write and maintain code like
this. Any change in the code might cause the rest of the code to change
meaning completely.
For some more minimalism try http://en.wikipedia.org/wiki/Brainfuck

rossum

Apr 29 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.