That doesn't "solve" the results that @Craig expects as if you change the line "case 2=2" to "case 2=3" then even though that test is negative, because the case 1=1 has not been terminated with break, the case 2=3 statement is ignored and the second addition is also made.
Code: Select all
on mouseUp
switch
case 1= 1
add 1 to a
case 2 = 3 -- you would think this prevents the next addition, but becase the statement block from the true result in the first case is not terminiated, it also performs that statement (and any others that might be there prior to the "break")
add 1 to a
break
case 1 = 5000
add 1 to a
break
end switch
answer a
end mouseUp
It it usually argued that a
switch structure can reduce the cognitive load in digesting a complex (especially nested) set of if/then/[else] tests, making the code clearer and more elegant.
In the case Craig is arguing for, I think it *could* be possible to make some
switch structures that work in that way, but probably the cognitive load would be increased. Certainly I don't see how you could reduce it to "just" case comparisons in an elegant
switch structure without some if/then comparisons. So overall, I imagine it would be impossible to make it "more elegant" than the series of one-liner if/then statements.
But I haven't really put any effort in to imagining how a
switch structure would work - it got too hard to imagine, so my brain took the easy way and refused to go beyond the one-liner if/then series.