🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1445

Re: while() and do/while()

#1445
From: bartc
Date: Tue, 10 Nov 2026 20:33
44 lines
3387 bytes
On 10/11/2026 19:47, Tim Rentsch wrote:
> bartc <bc@freeuk.com> writes:
> 
>> Anyway, how does C get away with having 'while' both starting a
>> loop statement, and ending one?!)
> 
> What is it that you think C is getting away with by having both
> while() and do/while() loop forms?


Mine was a light-hearted remark. Still, it /does/ get away with using 
the same keyword for two different purposes.

So you, or a compiler, encounters 'while': is that starting a new 
while-loop, or terminating a do-while loop? A fragment like this:

  ...
  while (*s++ = *t++);

could be either unless you know what '...' is.

You need to delve into the grammar to see how it works:

    do stmt while();         // 'while' terminates 'do'

    do while() ...           // 'while' is a while-loop inside do-while

    ... while()              // independent while-loop provided this is
                             // not that first case

So the 'while' on the first line will get consumed when parsing 'do stmt 
while' before it can be interpreted as a stand-alone statement.

The cost is needing to always have exactly 1 statement for the body of 
do-while. 2 or more need a compound statement, and 0 statements is not 
allowed.

(I can't have repeat-while in my own projects because it would be 
ambiguous. But there I can have 0, 1 or more statements within the body, 
and any can legally be nested while-statements.

I have to use repeat-until which has the opposite logic.)

Technical Details
Message-ID: <11cvv36$2g0f7$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11c7f1j$20b9h$1@dont-email.me> <11ccu0t$3s54f$1@dont-email.me> <11ccv4n$3sjn5$1@dont-email.me> <11cd05l$3t1er$1@dont-email.me> <11cd0q3$3ta28$1@dont-email.me> <11cd1vs$3tpfp$1@dont-email.me> <11cnve1$3ofei$1@dont-email.me> <11co8d4$3rpvm$1@dont-email.me> <14be9ea5-918f-5871-1bba-11e29e6e25d5@gmail.com> <11coqot$1236$1@dont-email.me> <11cplsf$8nc4$1@dont-email.me> <11cpnbb$97kp$1@dont-email.me> <11cpo21$9fji$1@dont-email.me> <11cpov5$9q2h$1@dont-email.me> <11cq3hb$dnf9$1@dont-email.me> <11ctijh$1mb1u$1@dont-email.me> <11ctr0o$1or07$1@dont-email.me> <11cukc4$1vnj7$1@dont-email.me> <11cv1ft$24amr$1@dont-email.me> <11cv3vb$257i8$1@dont-email.me> <11cv7lo$26k3l$1@dont-email.me> <86tslompsu.fsf_-_@linuxsc.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: bartc <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: while() and do/while()
Date: Tue, 10 Nov 2026 20:33:42 +0000
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <11cvv36$2g0f7$1@dont-email.me>
References: <11c7f1j$20b9h$1@dont-email.me> <11ccu0t$3s54f$1@dont-email.me>
 <11ccv4n$3sjn5$1@dont-email.me> <11cd05l$3t1er$1@dont-email.me>
 <11cd0q3$3ta28$1@dont-email.me> <11cd1vs$3tpfp$1@dont-email.me>
 <11cnve1$3ofei$1@dont-email.me> <11co8d4$3rpvm$1@dont-email.me>
 <14be9ea5-918f-5871-1bba-11e29e6e25d5@gmail.com>
 <11coqot$1236$1@dont-email.me> <11cplsf$8nc4$1@dont-email.me>
 <11cpnbb$97kp$1@dont-email.me> <11cpo21$9fji$1@dont-email.me>
 <11cpov5$9q2h$1@dont-email.me> <11cq3hb$dnf9$1@dont-email.me>
 <11ctijh$1mb1u$1@dont-email.me> <11ctr0o$1or07$1@dont-email.me>
 <11cukc4$1vnj7$1@dont-email.me> <11cv1ft$24amr$1@dont-email.me>
 <11cv3vb$257i8$1@dont-email.me> <11cv7lo$26k3l$1@dont-email.me>
 <86tslompsu.fsf_-_@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 10 Nov 2026 20:33:43 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2621927"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18EnVHgy8tNDFU3SD9OmSRZ"; posting-host="4695183c8b99665fc19e8d6c8d0d67a8"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:VXBu0jTUQhWrF3vLhCXYn9txKCA= sha256:6z6JA+Pl9dyA/z8S+CIPefv7ElZMxD/VnZ/18s9n2LM=
	sha1:uhJ8YP7g+pH2dFIZKdNP+JVE3K8= sha256:ox2jt7/n6beorFI+iC9+3xvPO9WOX+JUR2jqboUvHnk=
In-Reply-To: <86tslompsu.fsf_-_@linuxsc.com>
Content-Language: en-GB