Re: Do While loop syntax

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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@xxx> schreef in bericht
news:%23%23MV$SgiHHA.4624@xxxxxxxxxxxxxxxxxxxxxxx
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]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:OTgcnMgiHHA.4516@xxxxxxxxxxxxxxxxxxxxxxx
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Rene" <a@xxx> wrote in message
news:eIf$rHgiHHA.1216@xxxxxxxxxxxxxxxxxxxxxxx
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]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:%23bxIkofiHHA.4704@xxxxxxxxxxxxxxxxxxxxxxx
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Rene" <a@xxx> wrote in message
news:%23SSZAjfiHHA.4496@xxxxxxxxxxxxxxxxxxxxxxx
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.













.



Relevant Pages

  • Re: Do While loop syntax
    ... I would had imagine that the compiler could easily figure ... code block with the while statement above it? ... That's why the semi-colon is needed. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Do While loop syntax
    ... What is the compiler suppose to do? ... code block with the while statement above it? ... That's why the semi-colon is needed. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... >> both less efficient and less safe than the Fortran and Basic standard. ... >> The C for loop is actually trying to do what a do loop does. ... sloppy thinking that results from confusing a programming language ... > I do not believe that you are capable of writing a conforming C compiler. ...
    (comp.programming)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... it's a for loop in the C sense. ... > sloppy thinking that results from confusing a programming language ... >> I do not believe that you are capable of writing a conforming C compiler. ... Does Microsoft's C compiler perform this optimisation? ...
    (comp.programming)
  • Re: Histogram of character frequencies
    ... generated object code may simply be a loop in which elements are ... believe any C compiler anywhere would reject it. ... On the first iteration of the loop you test the end of file indicator ...
    (comp.lang.c)