Advertisements

On Tuesday, 24 September 2013 12:23:20 UTC+3, Vinayakan Kuruvath wrote:
> I am just curious as to why a 'do ... while' loop syntax needs a semicolon
> at the end.
>
> Whereas for and while loop do not need a semi-colon terminator at end.

The 'for' and 'while' loops need statement at the end. The compound
statement does not need semicolon at end, but single statement or
empty statement need semicolon at end:

for (i=0; i<10; ++i) printf("Such i: %d\n", i); // <-- see?

It would be ambiguous to parse 'while' loop inside 'do-while' loop
if there was no semicolon required at end of 'do-while' loop.
Like that:

Advertisements

Ã–Ã¶ Tiib <> writes:
> On Tuesday, 24 September 2013 12:23:20 UTC+3, Vinayakan Kuruvath wrote:
>> I am just curious as to why a 'do ... while' loop syntax needs a semicolon
>> at the end.
>>
>> Whereas for and while loop do not need a semi-colon terminator at end.
>
> The 'for' and 'while' loops need statement at the end. The compound
> statement does not need semicolon at end, but single statement or
> empty statement need semicolon at end:
>
> for (i=0; i<10; ++i) printf("Such i: %d\n", i); // <-- see?
>
> It would be ambiguous to parse 'while' loop inside 'do-while' loop
> if there was no semicolon required at end of 'do-while' loop.
> Like that:
>
> int a = 10;
> int b = 10;
>
> do
> while(a > 0) // does do-while end here? No. There are no semicolon.

There's no ambiguity here. 'do' must be followed by a statement, so the
'while' is clearly the start of a statement, not the end of a
'do...while'. The shortest 'do...while' has an empty statement in the
middle:

On Tuesday, 24 September 2013 13:55:01 UTC+3, Ben Bacarisse wrote:
> Öö Tiib <> writes:
> > do
> > while(a > 0) // does do-while end here? No. There are no semicolon.
>
> There's no ambiguity here. 'do' must be followed by a statement, so the
> 'while' is clearly the start of a statement, not the end of a
> 'do...while'. The shortest 'do...while' has an empty statement in the
> middle:
>
> do ; while (C);

True. Thanks for correcting!
> I don't think there would be any ambiguity if the ';' at the end of
> 'do...while' where not there. I think it's needed for symmetry rather
> than for parsing.

Apparently. If to think of it then there is no parsing aid from
semicolon after several other statements as well like 'break', 'continue'
or 'goto somewhere'.

Ã–Ã¶ Tiib <> writes:
> On Tuesday, 24 September 2013 13:55:01 UTC+3, Ben Bacarisse wrote:
>> Ã–Ã¶ Tiib <> writes:
>> > do
>> > while(a > 0) // does do-while end here? No. There are no semicolon.
>>
>> There's no ambiguity here. 'do' must be followed by a statement, so the
>> 'while' is clearly the start of a statement, not the end of a
>> 'do...while'. The shortest 'do...while' has an empty statement in the
>> middle:
>>
>> do ; while (C);
>
> True. Thanks for correcting!
>
>> I don't think there would be any ambiguity if the ';' at the end of
>> 'do...while' where not there. I think it's needed for symmetry rather
>> than for parsing.
>
> Apparently. If to think of it then there is no parsing aid from
> semicolon after several other statements as well like 'break', 'continue'
> or 'goto somewhere'.

Yes. And the '(' after 'if', 'while', 'switch' and 'for' are there for
symmetry alone. Ditto the ')' after the type name in a compound literal.

On 9/24/2013 6:55 AM, Ben Bacarisse wrote:
> Ã–Ã¶ Tiib <> writes:
>
>> On Tuesday, 24 September 2013 12:23:20 UTC+3, Vinayakan Kuruvath wrote:
>>> I am just curious as to why a 'do ... while' loop syntax needs a semicolon
>>> at the end.
>>>
>>> Whereas for and while loop do not need a semi-colon terminator at end.
>>
>> The 'for' and 'while' loops need statement at the end. The compound
>> statement does not need semicolon at end, but single statement or
>> empty statement need semicolon at end:
>>
>> for (i=0; i<10; ++i) printf("Such i: %d\n", i); // <-- see?
>>
>> It would be ambiguous to parse 'while' loop inside 'do-while' loop
>> if there was no semicolon required at end of 'do-while' loop.
>> Like that:
>>
>> int a = 10;
>> int b = 10;
>>
>> do
>> while(a > 0) // does do-while end here? No. There are no semicolon.
>
> There's no ambiguity here. 'do' must be followed by a statement, so the
> 'while' is clearly the start of a statement, not the end of a
> 'do...while'. The shortest 'do...while' has an empty statement in the
> middle:
>
> do ; while (C);
>
>> {
>> printf("Such a: %d\n", a);
>> a--;
>> }
>> while(a = b--);
>>
>> Other option could be to require always compound statement inside
>> 'do-while' loop. The authors of C did choose to require semicolon
>> at end.
>
> I don't think there would be any ambiguity if the ';' at the end of
> 'do...while' where not there. I think it's needed for symmetry rather
> than for parsing.

do while(A) while(B) while(C) while(D);

This is unambiguous in C-as-it-stands, but if the trailing
semicolon after `do-while' were omitted there would need to be
some other rule to decide whether a `while' begins a statement
nested within the `do-while' or whether it ends the `do-while'.
Such a rule could certainly be invented, something like the rule
for associating an `else' with the proper `if'. But I don't
think simply dropping the semicolon would work without some
other compensating change to the language.

"Ben Bacarisse" <> wrote in message
news:...
> Öö Tiib <> writes:
>
>>
>> Apparently. If to think of it then there is no parsing aid from
>> semicolon after several other statements as well like 'break', 'continue'
>> or 'goto somewhere'.
>
> Yes. And the '(' after 'if', 'while', 'switch' and 'for' are there for
> symmetry alone.

Well, no, if you don't insist on the parens in an 'if'statement, then a
string of tokens such as:

if a * b == c - d ;

becomes ambiguous; yes, you could come up with rules to disambiguate,
however (in the above example) if the rules don't depend on the types of
'a', 'b', 'c' and 'd', they'll quite likely not to match what the author
meant.

Eric Sosman <> writes:
> On 9/24/2013 6:55 AM, Ben Bacarisse wrote:
>> Ã–Ã¶ Tiib <> writes:
<snip>
>>> do
>>> while(a > 0) // does do-while end here? No. There are no semicolon.
>>
>> There's no ambiguity here. 'do' must be followed by a statement, so the
>> 'while' is clearly the start of a statement, not the end of a
>> 'do...while'. The shortest 'do...while' has an empty statement in the
>> middle:
>>
>> do ; while (C);
>>
>>> {
>>> printf("Such a: %d\n", a);
>>> a--;
>>> }
>>> while(a = b--);
>>>
>>> Other option could be to require always compound statement inside
>>> 'do-while' loop. The authors of C did choose to require semicolon
>>> at end.
>>
>> I don't think there would be any ambiguity if the ';' at the end of
>> 'do...while' where not there. I think it's needed for symmetry rather
>> than for parsing.
>
> do while(A) while(B) while(C) while(D);
>
> This is unambiguous in C-as-it-stands,

Hmm, it's a syntax error, is it not? (I'm not contradicting you, a
syntax error is as unambiguous as anything else, but I am not 100% sure
what you gain from the example).
> but if the trailing
> semicolon after `do-while' were omitted there would need to be
> some other rule to decide whether a `while' begins a statement
> nested within the `do-while' or whether it ends the `do-while'.

I don't follow. In a construct like the above, each while must be the
start of a new statement (the first inside the do and the rest inside
the previous while. The semicolon ends the chain, so the parser now
expects a 'while' to end the 'do'. When it seems it, it need not care
if there is a ';' after the ')' or not. I.e the valid form of the above:

do while(A) while(C) while(C) while(D); while(E);

is as unambiguous (to my casual review of the syntax) with or without
the trailing ';'.
> Such a rule could certainly be invented, something like the rule
> for associating an `else' with the proper `if'. But I don't
> think simply dropping the semicolon would work without some
> other compensating change to the language.

"Scott Fluhrer" <> writes:
> "Ben Bacarisse" <> wrote in message
> news:...
>> Ã–Ã¶ Tiib <> writes:
>>
>>>
>>> Apparently. If to think of it then there is no parsing aid from
>>> semicolon after several other statements as well like 'break', 'continue'
>>> or 'goto somewhere'.
>>
>> Yes. And the '(' after 'if', 'while', 'switch' and 'for' are there for
>> symmetry alone.
>
> Well, no, if you don't insist on the parens in an 'if'statement, then a
> string of tokens such as:
>
> if a * b == c - d ;

I said nothing about omitting the ')'. The '(' is there for symmetry.
Because we had been talking about symmetry, I mentioned a couple of
other cases.

"Ben Bacarisse" <> wrote in message
news:...
> "Scott Fluhrer" <> writes:
>
>> "Ben Bacarisse" <> wrote in message
>> news:...
>>> Öö Tiib <> writes:
>>>
>>>>
>>>> Apparently. If to think of it then there is no parsing aid from
>>>> semicolon after several other statements as well like 'break',
>>>> 'continue'
>>>> or 'goto somewhere'.
>>>
>>> Yes. And the '(' after 'if', 'while', 'switch' and 'for' are there for
>>> symmetry alone.
>>
>> Well, no, if you don't insist on the parens in an 'if'statement, then a
>> string of tokens such as:
>>
>> if a * b == c - d ;
>
> I said nothing about omitting the ')'. The '(' is there for symmetry.
> Because we had been talking about symmetry, I mentioned a couple of
> other cases.

In <aJj0u.112945$4> "BartC" <> writes:
> > I am just curious as to why a 'do ... while' loop syntax needs a semicolon
> > at the end.
> You don't need a semicolon following a closing brace such as }.
> That's a simple way of explaining it.

A semicolon following a closing brace is required in some contexts, such
as declaring a struct.

struct foo
{
int i;
char c;
float f;
};

--
John Gordon A is for Amy, who fell down the stairs B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

John Gordon <> writes:
> In <aJj0u.112945$4> "BartC" <> writes:
>
>> > I am just curious as to why a 'do ... while' loop syntax needs a semicolon
>> > at the end.
>
>> You don't need a semicolon following a closing brace such as }.
>
>> That's a simple way of explaining it.
>
> A semicolon following a closing brace is required in some contexts, such
> as declaring a struct.
>
> struct foo
> {
> int i;
> char c;
> float f;
> };

Also following a initialiser list, and after a compound literal in some
situations:

int a[] = {1, 2, 3};
return (struct foo){1, 2};

However, I'm sure that none of these relate to what BartC meant (rather
than what he said). I'm sure the '}' referred to is the one that closes
a compound statement, but, even so, I don't see how it answers the OP's
question, simply or otherwise.

Öö Tiib <> wrote:
> On Tuesday, 24 September 2013 12:23:20 UTC+3, Vinayakan Kuruvath wrote:
>> I am just curious as to why a 'do ... while' loop syntax needs
>> a semicolon at the end.
>> Whereas for and while loop do not need a semi-colon terminator at end.
> The 'for' and 'while' loops need statement at the end. The compound
> statement does not need semicolon at end, but single statement or
> empty statement need semicolon at end:
> for (i=0; i<10; ++i) printf("Such i: %d\n", i); // <-- see?
> It would be ambiguous to parse 'while' loop inside 'do-while' loop
> if there was no semicolon required at end of 'do-while' loop.

There are languages like Pascal, and I believe Algol, where the
semicolon is a statement separator. In PL/I and C, it is a statement
terminator. (Look at a description of Pascal to see the difference.)

In PL/I, grouping of statements is done with DO; and END;

IF A=B THEN DO;
PUT SKIP LIST(A);
PUT SKIP LIST(B);
END;

(I learned PL/I before C.)

Following that, the C { replaces DO; and } replaces END;

You will find sometimes people use do ... while(0) in macros
to avoid the semicolon confusion that otherwise results.

"Ben Bacarisse" <> wrote in message
news:...
>> In <aJj0u.112945$4> "BartC" <> writes:
>>
>>> > I am just curious as to why a 'do ... while' loop syntax needs a
>>> > semicolon
>>> > at the end.
>>> You don't need a semicolon following a closing brace such as }.
>>
>>> That's a simple way of explaining it.
> However, I'm sure that none of these relate to what BartC meant (rather
> than what he said). I'm sure the '}' referred to is the one that closes
> a compound statement, but, even so, I don't see how it answers the OP's
> question, simply or otherwise.

The OP was presumably thinking about other loops such as these:

for (a; b; c) {d; e;}
while (a) {b; c;}

which all finish with a compound statement and hence no semicolon ends the
loop, and comparing to this:

do {a; b;} while (c);

which also has a compound statement as a body, that doesn't need a
semicolon, but it's not the end of the loop.

So the first two examples end with }, the last with ). According to my
simple rule, the first two don't need a semicolon.

"BartC" <> writes:
> "Ben Bacarisse" <> wrote in message
> news:...
>
>>> In <aJj0u.112945$4> "BartC" <> writes:
>>>
>>>> > I am just curious as to why a 'do ... while' loop syntax needs a
>>>> > semicolon
>>>> > at the end.
>
>>>> You don't need a semicolon following a closing brace such as }.
>>>
>>>> That's a simple way of explaining it.
>
>> However, I'm sure that none of these relate to what BartC meant (rather
>> than what he said). I'm sure the '}' referred to is the one that closes
>> a compound statement, but, even so, I don't see how it answers the OP's
>> question, simply or otherwise.
>
> The OP was presumably thinking about other loops such as these:
>
> for (a; b; c) {d; e;}
> while (a) {b; c;}
>
> which all finish with a compound statement and hence no semicolon ends
> the loop, and comparing to this:
>
> do {a; b;} while (c);
>
> which also has a compound statement as a body, that doesn't need a
> semicolon, but it's not the end of the loop.
>
> So the first two examples end with }, the last with ). According to my
> simple rule, the first two don't need a semicolon.

The OP asked "why the do while needs a semicolon" and that, to me, needs
a deeper answer than giving a rule which re-states a consequence of the
syntax (the key words being "why" and "needs"). Since I don't think
it's needed at all, any explanation of why it's needed, is going to look
wrong to me.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!