| Deutsch English Français Italiano |
|
<vu1bv0$2fp2j$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!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: Sun, 20 Apr 2025 00:34:57 +0100
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <vu1bv0$2fp2j$1@dont-email.me>
References: <vspbjh$8dvd$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> <vtnekn$1fogv$1@dont-email.me>
<vto2mb$20c4n$1@dont-email.me> <vtu4i5$3hteg$1@dont-email.me>
<vtujko$3uida$1@dont-email.me> <vtvfop$rf2p$1@dont-email.me>
<vtvto2$15otp$1@dont-email.me> <vu01k7$1bfv2$1@dont-email.me>
<vu0720$1dva7$1@dont-email.me> <8734e4nd08.fsf@nosuchdomain.example.com>
<vu0v75$22n7b$5@dont-email.me> <87h62jn9li.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 20 Apr 2025 01:34:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6c9533872368e3608000b70afe74a0c7";
logging-data="2614355"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uTaLkovWrGWjBQIIHkNLR"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/H66+S6wUWY2mOwlZKdE8DOP20E=
In-Reply-To: <87h62jn9li.fsf@nosuchdomain.example.com>
Content-Language: en-GB
Bytes: 4260
On 19/04/2025 22:07, Keith Thompson wrote:
> bart <bc@freeuk.com> writes:
>> On 19/04/2025 20:54, Keith Thompson wrote:
>>> bart <bc@freeuk.com> writes:
>>> [...]
>>>> I don't understand why you're happy using a crippled looping
>>>> mechanism, that is a PITA to use for the majority of iterating loops,
>>>> just so you have that flexibility for the whacky loops that are in the
>>>> minority, most of which can be trivially expressed with 'while'.
>>> [...]
>>> I agree. You don't understand. What confuses me is your complete
>>> lack
>>> of interest in trying to understand.
>>> Many of us are happy using C's for loop because we don't consider
>>> it to be crippled. It is not a PITA to use it for the majority of
>>> iterating loops; we just use a very common coding pattern like
>>> for (int i = 0; i < N; i ++) {
>>> // ...
>>> }
>>> We can write "i --" if we want the loop to run in reverse,
>>
>> That's not enough for it to run in reverse.
>
> No, it's not. I didn't mean to imply that it was.
>
>> I'd be interested in a
>> complete example.
>
> Really? It's such a simple example that I'm surprised you think you
> need my help. But ok :
>
> for (int i = N-1; i >=0; i --) {
> // ...
> }
>
> I wrote that off the top of my head. I then wrote and compiled a
> program using it, and it worked.
I only recently noticed that the pattern was rather different from an
upwards loop. For example, for a range of 0..N-1 inclusive the upper
limit is usually written as N, with N-1 being something that a 1-based
language is more likely to use.
I just wondered if you used a different approach. So it looks like 3
things need to be changed, apart from reversing range limits:
Start val Comp End Val Increment
Upwards 0 < N ++
Downwards N-1 >= 0 --
It's also just struck me that it would be more consistent if Upwards
loops used this:
Upward 0 <= N-1 ++
I doubt this would be popular though with two extra characters to type
(and possibly to omit or get wrong).
>
> A similar loop with an iteration variable of an unsigned type would
> fail because `i >= 0` is always true. I have some thoughts about
> how to deal with that, but I'll leave that aside.
Just insist on signed loop variables. Or if ints are 32 bits and start
values are between 2**31 and 2**32, require an i64 loop variable.