Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does a 'do .. while' loop need a semicolon terminator.

2,597 views
Skip to first unread message

Vinayakan Kuruvath

unread,
Sep 24, 2013, 5:23:20 AM9/24/13
to
Hi,

#include <stdio.h>

int main() {
int i=0;
do {
i++;
printf("Before continue --> i: %d\n", i);
if ( i == 7) continue;
printf("After continue --> i: %d\n", i);
} while (i < 10);

printf("Over\n");
}


In the above code, if I remove the semi-colon after the 'while (i< 10)' the parser will complain.

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.

Regards,
Vinayakan.

Öö Tiib

unread,
Sep 24, 2013, 6:08:09 AM9/24/13
to
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.
{
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.

Vinayakan Kuruvath

unread,
Sep 24, 2013, 6:18:34 AM9/24/13
to
Thanks Tiib, I suspected it is something to do with making parsing easy. You have explained it so well.

Ben Bacarisse

unread,
Sep 24, 2013, 6:55:01 AM9/24/13
to
Öö Tiib <oot...@hot.ee> 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.

--
Ben.

Öö Tiib

unread,
Sep 24, 2013, 9:14:08 AM9/24/13
to
On Tuesday, 24 September 2013 13:55:01 UTC+3, Ben Bacarisse wrote:
> Öö Tiib <oot...@hot.ee> 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'.

Ben Bacarisse

unread,
Sep 24, 2013, 10:01:12 AM9/24/13
to
Yes. And the '(' after 'if', 'while', 'switch' and 'for' are there for
symmetry alone. Ditto the ')' after the type name in a compound literal.

--
Ben.

Eric Sosman

unread,
Sep 24, 2013, 10:11:54 AM9/24/13
to
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.

--
Eric Sosman
eso...@comcast-dot-net.invalid

Scott Fluhrer

unread,
Sep 24, 2013, 10:34:12 AM9/24/13
to

"Ben Bacarisse" <ben.u...@bsb.me.uk> wrote in message
news:0.f13c827a02b64b7a8d56.2013...@bsb.me.uk...
> �� Tiib <oot...@hot.ee> 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.

--
poncho


guinne...@gmail.com

unread,
Sep 24, 2013, 10:34:13 AM9/24/13
to
On Tuesday, 24 September 2013 15:11:54 UTC+1, Eric Sosman wrote:
>
> do while(A) while(B) while(C) while(D);
>
> This is unambiguous in C-as-it-stands,

This is uncompileable as it stands.

You need a statement after while(C) and before while(D).
An empty one will do:

Eric Sosman

unread,
Sep 24, 2013, 10:53:34 AM9/24/13
to
Oops! Right you are.

--
Eric Sosman
eso...@comcast-dot-net.invalid

Ben Bacarisse

unread,
Sep 24, 2013, 10:54:13 AM9/24/13
to
Eric Sosman <eso...@comcast-dot-net.invalid> writes:

> On 9/24/2013 6:55 AM, Ben Bacarisse wrote:
>> Öö Tiib <oot...@hot.ee> 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.

--
Ben.

Ben Bacarisse

unread,
Sep 24, 2013, 11:00:39 AM9/24/13
to
"Scott Fluhrer" <sflu...@ix.netcom.com> writes:

> "Ben Bacarisse" <ben.u...@bsb.me.uk> wrote in message
> news:0.f13c827a02b64b7a8d56.2013...@bsb.me.uk...
>> Öö Tiib <oot...@hot.ee> 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.

<snip>
--
Ben.

Scott Fluhrer

unread,
Sep 24, 2013, 11:04:10 AM9/24/13
to

"Ben Bacarisse" <ben.u...@bsb.me.uk> wrote in message
news:0.5f7e171d713b0a072b26.2013...@bsb.me.uk...
> "Scott Fluhrer" <sflu...@ix.netcom.com> writes:
>
>> "Ben Bacarisse" <ben.u...@bsb.me.uk> wrote in message
>> news:0.f13c827a02b64b7a8d56.2013...@bsb.me.uk...
>>> �� Tiib <oot...@hot.ee> 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.

Oops, sorry about that. You are correct.

--
poncho


BartC

unread,
Sep 24, 2013, 1:11:13 PM9/24/13
to


"Vinayakan Kuruvath" <ksvin...@gmail.com> wrote in message
news:66dcefdb-157c-438f...@googlegroups.com...

> do {
> i++;
> printf("Before continue --> i: %d\n", i);
> if ( i == 7) continue;
> printf("After continue --> i: %d\n", i);
> } while (i < 10);

> In the above code, if I remove the semi-colon after the 'while (i< 10)'
> the parser will complain.
>
> 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.

You don't need a semicolon following a closing brace such as }.

That's a simple way of explaining it.

--
Bartc

John Gordon

unread,
Sep 24, 2013, 1:23:28 PM9/24/13
to
In <aJj0u.112945$ei6....@fx08.am4> "BartC" <b...@freeuk.com> 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
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Ben Bacarisse

unread,
Sep 24, 2013, 1:33:27 PM9/24/13
to
John Gordon <gor...@panix.com> writes:

> In <aJj0u.112945$ei6....@fx08.am4> "BartC" <b...@freeuk.com> 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.

--
Ben.

glen herrmannsfeldt

unread,
Sep 24, 2013, 2:35:14 PM9/24/13
to
ᅵᅵ Tiib <oot...@hot.ee> 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.

-- glen

BartC

unread,
Sep 24, 2013, 4:24:52 PM9/24/13
to
"Ben Bacarisse" <ben.u...@bsb.me.uk> wrote in message
news:0.0cb6c6f968e9e593c826.2013...@bsb.me.uk...

>> In <aJj0u.112945$ei6....@fx08.am4> "BartC" <b...@freeuk.com> 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

Ben Bacarisse

unread,
Sep 24, 2013, 6:02:46 PM9/24/13
to
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.

--
Ben.

Nitin Tripathi

unread,
Sep 25, 2013, 12:06:29 PM9/25/13
to
best explanation.

Ben Bacarisse

unread,
Sep 25, 2013, 6:19:34 PM9/25/13
to
Nitin Tripathi <2nit...@gmail.com> writes:

> best explanation.

I presume you mean the message you replied to? If so, yes, it's
certainly very clear and easy to understand, but it suffers from the
small problem of being wrong!

--
Ben.
0 new messages