Deutsch   English   Français   Italiano  
<vtmgj8$g81k$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Loops (was Re: do { quit; } else { })
Date: Tue, 15 Apr 2025 21:46:32 +0100
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <vtmgj8$g81k$1@dont-email.me>
References: <vspbjh$8dvd$1@dont-email.me> <vtc7mp$2q5hr$1@dont-email.me>
 <vtcqf6$3j95s$1@dont-email.me> <vtdh4q$b3kt$1@dont-email.me>
 <vtf7fe$1qtpg$1@dont-email.me> <vtgfuf$31ug1$1@dont-email.me>
 <20250413072027.219@kylheku.com> <vtgpce$39229$1@dont-email.me>
 <vti2ki$g23v$1@dont-email.me> <vtin99$vu24$1@dont-email.me>
 <vtiuf0$18au8$1@dont-email.me> <vtj97r$1i3v3$1@dont-email.me>
 <vtl166$36p6b$1@dont-email.me> <vtlcg0$3f46a$2@dont-email.me>
 <20250415053852.166@kylheku.com> <vtm4ae$6d5j$1@dont-email.me>
 <H7yLP.2056536$OrR5.1414451@fx18.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 15 Apr 2025 22:46:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cec4580467d8bef69918f71b92c931ef";
	logging-data="532532"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/hhOTmtdi1ud7K6T30RGEH"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UT24iXXgFgUdaU91AA17+D9I5cc=
Content-Language: en-GB
In-Reply-To: <H7yLP.2056536$OrR5.1414451@fx18.iad>
Bytes: 3646

On 15/04/2025 20:07, Scott Lurndal wrote:
> bart <bc@freeuk.com> writes:
>> On 15/04/2025 14:19, Kaz Kylheku wrote:
> 
>> Thats's fine. But it means a real 'for' loop doesn't exist in C; you
>> have to emulate it using that 3-way construct, which is naff, and also
>> error prone.
> 
> Real for loops _are_ a three-way construct.
> 
> 135 FOR I=1 TO 10 STEP 2                              [BASIC]
> 
>      for(i = 1; i < 11; i += 2)                        [C/C++]
> 
>      do 1 = 1, 10, 2                                   [FORTRAN]

Any step other than 1 is unusual. So Basic and Fortran would typically be:

     for i = 1 to 10     # 6 tokens; Basic
     do  i = 1, 10       # 6 tokens; Fortran
     for i = 1, 10       # 6 tokens; Lua
     for i to 10 do      # 5 tokens; Mine (using default start)
     to 10 do            # 3 tokens; Mine (when index is not needed)

Let's look at that C again:

     for (int i = 1; i < 11; i += 1)  # 15 tokens; C

* I've added 'int' for the C, as most of the above don't need a
   declaration for the loop index. But it will still be 14 without

* I use += for the increment because you did. ++ saves one token,
   but it's still at 13/14 tokens

* All the above can directly use 10 as the upper bound; C has to use
   11 when written idiomatically

Even using a step of two, that would only add 2 tokens to the above; C 
will still need 14/15 tokens.

Note that C's for-loop is dumb; it merely take three expressions A B C 
that can be completely unrelated, and arranges them into a loop:

   A; while (B) {...; C}


> SPRITE also had:
> 
>     UNTIL done, notfound
>     DO
>         ...
>         IF token = "this"
>         THEN LOOP_EXIT done
>         FI;
>         ...
>         IF i = upb(table)
>         THEN LOOP_EXIT notfound
>         FI;
>         ...
>     OD
>     CASE
>     IS done:     write("i=", i);
>     OR notfound  write("not found", upb(table));

That's pretty weird, but whatever it does, it's nothing to do with 
iterating a variable over a range.