473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 25501
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.co m

"Rene" <a@b.cwrote in message
news:%2******** ********@TK2MSF TNGP05.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.c omwrote in
message news:%2******** ********@TK2MSF TNGP06.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.co m

"Rene" <a@b.cwrote in message
news:%2******** ********@TK2MSF TNGP05.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.ReadLin e();
}

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.co m

"Rene" <a@b.cwrote in message
news:eI******** ******@TK2MSFTN GP03.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP06.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.co m

"Rene" <a@b.cwrote in message
news:%2******* *********@TK2MS FTNGP05.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.c omwrote in
message news:OT******** ******@TK2MSFTN GP03.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.ReadLin e();
}

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.co m

"Rene" <a@b.cwrote in message
news:eI******** ******@TK2MSFTN GP03.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP06.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.co m

"Rene" <a@b.cwrote in message
news:%2****** **********@TK2M SFTNGP05.phx.gb l...
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******** **********@TK2M SFTNGP04.phx.gb l...
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.c omwrote
in message news:OT******** ******@TK2MSFTN GP03.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.ReadLin e();
}

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.co m

"Rene" <a@b.cwrote in message
news:eI******* *******@TK2MSFT NGP03.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP06.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.co m

"Rene" <a@b.cwrote in message
news:%2***** ***********@TK2 MSFTNGP05.phx.g bl...
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:##******** ******@TK2MSFTN GP04.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.c omwrote
in message news:OT******** ******@TK2MSFTN GP03.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.ReadLin e();
}

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.co m

"Rene" <a@b.cwrote in message
news:eI******* *******@TK2MSFT NGP03.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP06.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.co m

"Rene" <a@b.cwrote in message
news:%2***** ***********@TK2 MSFTNGP05.phx.g bl...
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******** ********@TK2MSF TNGP05.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.ed u.for.addresswr ote in message
news:Oe******** ******@TK2MSFTN GP02.phx.gbl...
>
"Rene" <a@b.cwrote in message
news:%2******** ********@TK2MSF TNGP05.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2510
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 equivalent to >>> for x in : but is shorter and easier to read. basically, the if works as a filter for
699
33349
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
24
2675
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
1504
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
13968
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: error BC30451: Name 'i' is not declared.
2
10631
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 step -1 response.write "<br>" & i next For some reason, that doesn't work. It shows nothing. I know I am missing something very simple, but,...
9
2734
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 doc_id assigned as the primary key in the doc table when the doc is entered into the database. What I can't do is use a form to select which docs to print...
0
2049
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 available, then the documentation above also contains (almost) equivalent Python code that might work for you.
2
1427
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" value="Y" <?php if $value=='Karen Lincoln'){ echo "checked=\"true\""; }?>>CAT <input type="checkbox" name="chk2" value="Y" <?php if($value=='Karen...
0
7473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.