Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143168
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c, comp.lang.c++ |
| Subject | Re: for ... else blocks |
| Date | 2019-04-17 14:05 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <q974qd$a1$1@dont-email.me> (permalink) |
| References | (5 earlier) <q95ltu$c8r$1@dont-email.me> <87y3494hru.fsf@bsb.me.uk> <q963vs$ck8$1@dont-email.me> <q96kqg$l34$1@dont-email.me> <kmDtE.71610$da5.23289@fx13.am4> |
Cross-posted to 2 groups.
On 17/04/2019 12:38, Bart wrote:
> On 17/04/2019 08:32, David Brown wrote:
>
>>> Thanks. That seems to indicate that there are at least two about equally
>>> convincing but exactly opposite notions of what `else` should mean if it
>>> were introduced as an extension to `for`...
>
> Its use in Python is not hard to understand.
That is debatable. But I agree it is easier (and more useful) than
Rick's suggestion.
>
> Its use as described in the OP is harder (RCH's ideas for features
> always are).
>
>> And that would indicate that neither is a good idea, and "for else"
>> should be avoided. Both cases can be easily and clearly handled by a
>> bool flag, which a good compiler can often eliminate entirely (and it's
>> a cheap solution even if the compiler can't remove it).
>
> The idea is to avoid the extra logic. Here's a typical use in one of my
> languages (real example):
>
> for sw to optionnames.len do
> if eqstring(name,optionnames[sw]) then
> do_option(sw,value)
> exit
> fi
> else
> println "Unknown option:",name
> stop 1
> od
>
bool found = false;
int sw;
for (sw = 0; sw < len; sw++) {
if (strcmp(name, options[sw]) == 0) {
found = true;
break;
}
}
if (found) {
do_option(sw, value);
} else {
printf("Unknown option: %s\r\n", name);
stop(1);
}
The "for else" construct is not necessary, and the bool flag solution
will be just as efficient. Structuring the code as "try to find the
match, then handle the result" is arguably neater (and by "arguably", I
don't mean it is /definitely/ neater). It also makes it easier to
refactor the code if you have a different way of searching, perhaps as a
separate function.
> This for a language at the level of C (I'm posting from that group). A
> higher level might just say 'sw in optionnames', but since many won't
> have eliminated for-loops completely, there is likely still to be a use
> for for-else.
>
> The alternative as you say is to use a flag:
>
> optionno:=0
> for sw ...
> ...
> optionno:=sw
> ...
> od
> if optionno then
> do_option ...
> else
> println ...
> fi
>
> I must have written a thousand such loops; the for-else feature provides
> a welcome respite, as well as a self-contained solution in one statement.
A smaller and more compact solution could be nice - but that would, as
you say, require a higher level language.
>
> The flag version introduces a new variable that demands an extra, wider
> scope, something at odds with your preference for narrower block scopes.
I have a preference for narrower scopes, all else being equal. But if
the data is needed over a wider range, then a wider scope is the answer.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
for ... else blocks "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-16 14:57 -0400
Re: for ... else blocks "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-16 15:06 -0400
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-16 20:35 +0100
Re: for ... else blocks "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-16 17:02 -0400
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-16 23:36 +0200
Re: for ... else blocks "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-04-17 00:45 +0200
Re: for ... else blocks Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-17 01:06 +0100
Re: for ... else blocks "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-04-17 04:45 +0200
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-17 09:32 +0200
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 11:38 +0100
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-17 14:05 +0200
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 13:57 +0100
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-17 15:36 +0200
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 15:16 +0100
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-17 17:51 +0200
Re: for ... else blocks "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-04-17 16:15 +0200
Re: for ... else blocks "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-04-17 16:24 +0200
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-17 17:55 +0200
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 17:44 +0100
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 17:48 +0100
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 10:18 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 10:32 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 10:38 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 12:12 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 12:25 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 12:39 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 15:52 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 16:50 -0700
Re: for ... else blocks fir <profesor.fir@gmail.com> - 2019-04-17 13:58 -0700
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-18 12:21 +0200
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-18 18:54 +0100
Re: for ... else blocks Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-18 20:36 +0100
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-18 21:15 +0100
Re: for ... else blocks Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-18 21:46 +0100
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-17 15:05 +0100
Re: for ... else blocks "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-17 10:55 -0400
Re: for ... else blocks Ike Naar <ike@iceland.freeshell.org> - 2019-04-18 05:57 +0000
Re: for ... else blocks Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-17 20:43 +0100
Re: for ... else blocks Chad <cdalten@gmail.com> - 2019-04-16 12:15 -0700
Re: for ... else blocks Bart <bc@freeuk.com> - 2019-04-16 20:27 +0100
Re: for ... else blocks Christian Gollwitzer <auriocus@gmx.de> - 2019-04-16 21:34 +0200
Re: for ... else blocks "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-16 16:54 -0400
Re: for ... else blocks David Brown <david.brown@hesbynett.no> - 2019-04-16 23:32 +0200
csiph-web