dunbarx wrote: ↑Mon Mar 22, 2021 12:12 am
Some here, all rather expert in LC,
That certainly lets me out haha.
@Bogs You assert that if/then can be substituted for switch in *any* instance. I don't understand that at all,(1.) since switch obviates entirely the need for nesting. Nesting can get rather unwieldy in complex constructions, so (2.) all this talk about one-line statements seems unfair to the discussion; (3.) it is complex constructs that benefit from using "switch" instead of "if/then", not simple ones.
1.) it is my opinion (and practice) that nesting in an if / then statement means you haven't thought out the goal completely, but nesting does serve a purpose in very quickly setting up a test while thinking the end result out.
1a.) if you think there are not examples of badly nested switch statements, I don't know what to tell you.
2..) The talk about 1 line if then statements came about based on the examples you yourself gave, so I'd say they are fair, however, even written out fully, taking 2 statements and applying them in <switch case> or <if then> will always come out shorter and certainly more english like in an <if then> construct.
In my entire life, I've never heard even once anyone come up to me and say "switch the apple, case the pear" outside of a room of people coding. On the other hand, I can't imagine anyone never having heard "If the door is open, then close it".
3.) All of the examples given here are simple, but, regardless I think you have that wrong. The main advantage to <switch case> over <if / then> isn't code blocking, realistically speaking, the main advantage is <switch case> doesn't evaluate every single permutation.
It goes to the one case that matches and then bails <at break>, where as <if then> continues until you are done testing conditions.
So if you want <if then> comparisons to <switch case> that are neatly segmented or, alternatively, you think it is somehow impossible to craft an <if then> as neatly as a <switch case> substitute one for the other after you have written it out. You will I think find that except for the words "switch, case, break, end switch" there really is no difference using "if, then, end if" other than break is missing.
<pssssssstttttttt> you can add a break statement to an <if then> construct if you *really* want to, I'm pretty sure the galaxy will survive {but I have not tested this hee hee}.
**Disclaimer - I have in the early stages of my life written entire programs that were nothing but
if / then statements just for fun. No animals were gassed out to extinction in the creating of this post. If water is entering your home, you might want to either make use of it, or stop it from being problematic.