473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Do While loop syntax

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
11 25484
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
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
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
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
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
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
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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
6
by: Bengt Richter | last post by:
E.g., so we could write for x in seq if x is not None: print repr(x), "isn't None ;-)" instead of for x in (x for x in seq if x is not None): print repr(x), "isn't None ;-)"
8
by: Jon Maz | last post by:
Hi All, Quick one: several times in vb.net code I have got off the net there are lines such as: For i As Integer = 0 To 10 which, on compiling with VS2002, produce the following error: ...
2
by: Adrienne Boswell | last post by:
Today, I am braindead... I can't seem to find this in Google, and I don't remember where I used this code before (so I can't steal it from myself). Anyway, all I want to do is: for i = 0 to 20...
9
by: Ron Wan | last post by:
I am inexperienced at this (posting and writitng code). I am using an Access2000 datasheet form with checkboxes to select PDF documents for printing. The PDFs are given filenames based on the...
0
by: Mark Dickinson | last post by:
On Nov 19, 5:48 pm, Johannes Bauer <dfnsonfsdu...@gmx.dewrote: Python 2.6 has itertools.product: http://docs.python.org/library/itertools.html#itertools.product If you don't have Python 2.6...
2
ddtpmyra
by: ddtpmyra | last post by:
I have check box on my page which has its equivalent value. How do I insert the value inside my tables in single row for each check box using LOOP ex. <input type="checkbox" name="chk1"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.