Demivec wrote: Tue Apr 29, 2025 10:59 pm
PeWa wrote: Tue Apr 29, 2025 9:27 pm
To find an example for my previous post, I quickly threw together a code sample. It didn't turn out particularly well, but it should illustrate what I mean:
the possibility of using CASE ELSE to set a variable or execute a procedure before another CASE occurs, because the subsequent one depends on the one before it. As I said, the example isn't good; I couldn't think of anything better.
Please ignore any logical errors

That should be enough for now.
@PeWa: I don't think your example on how a CASE ELSE might be used provides any backing as to why it might be useful. AFor fun, I rewrote your example to show why a CASE ELSE wouldn't be needed:
Code: Select all
; Lets asume, we wrote an adventure. Someone must come over a sea or something.
; several options, and we must be sure that under some circumstances a variable is set
; before a following CASE is a match.
; in this example: raft and paddles
; PaddlesStolen=0 -> Paddles are available, Paddles=1 ->paddles stolen
; OverTheSea 1: Paddles stolen, we must set a variable to excecute some animations
; OverTheSea 2: take a path
; OverTheSea 3: take a boat
; OverTheSea 4: take a raft
OverTheSea=3
PaddlesStolen=2 ; The Paddles are faulty
; handle stolen paddles
If OverTheSea = 1
Debug "The Paddles are stolen.What know ? You cant take the raft or boat"
PaddlesStolen=1
Else
Debug "The Paddles are available"
PaddlesStolen = 0
EndIf
If PaddlesStolen <> 1
Select OverTheSea
Case 1 ; {paddles stolen already handled}
Case 2 ; takes a path
Debug "He takes the path around the sea"
Case 3, 4 ; takes a boat or raft
If OverTheSea = 3
transport$ = "boat"
Else
transport$ = "raft"
EndIf
If PaddlesStolen=2
Debug "The Paddles are faulty, will you risk to try to come over the see with the " + transport$ + "?"
Choice = AskYN() ;return #yes or #no
If Choice = #no
Debug "He remains on the shore. Better to be safe then sorry."
Else
Debug "He take the " + transport$ + ", the paddles and try to reach the other side of the sea"
OverSeaAnimation = 0.0; ranges from 0.0 to 1.0
;Do animation of the travelling over the sea
SeaTravelAnimation(transport$, OverSeaAnimation)
EndIf
Default
Debug "He dont know how to conquer the sea"
EndSelect
EndIf
I don't think the addition of a CASE ELSE adds anything of substance.
However, I wouldn't mind some additional functionality but I'm not certain that would actually improve anything, especially with respect to speed and clarity. My understanding is that speed and clarity are two main reasons to use Select/Case instead of If/Else/EndIf. Trying to make Select/Case an all encompassing statement I think will just make it less effective at doing any one of them well. I think it be interesting to test out any ideas to see if they hold water.
Hi Demivec, that is of course a way to progam it. I know, my program was not really good, but i want to show one of the two reasons for an CASE ElSE function that i could find. But if we program it like you, we must use IF. As i stated above, it is always possible to use IF instead of SELECT,or in conjuction with select. i want to show a possible use of CASE ELSE.
I used for fun an example with a fictional adventure, let me show what i mean with an other example, a Procedure Inventory() in an adventure, if the user picks an item, then SELECT ENDSELECT sets a variable to "Visible" in inventory.
In this example the items have a simple number as ID and we have only two items. This example is in this form not usable,
only an example:
Code: Select all
Item=2 ; the user takes a lantern, 1 is a candle
Procedure VisibleItemsInventory()
Select item
CASE 1
candlevisible=1
CASE ELSE candlevisible=0
CASE 2
lanternvisible=1
CASE ELSE lanternvisible=0
Endselect
EndProcedure
It is only an example why a CASE ELSE could be useful. Can we use IF instead ? Sure. Can we program it another way ? Sure. But we can react if a CASE is not a match. imagin we have in that fictional adventure hundrets of items. I belief the main reason to use SELECT is the clarity, if we have a big bunch of cases. In your post you say that is also a reason for you.
If we use IF, ELSE, ELSEIF ELSEIF ELSEIF ... in that case that clarity is gone. Must we then not ask, why we not only implement in a programming langueage IF/ELSE and forget other commands like SELECT ? Of course, this is a matter we can discuss endless

The advantages of IF or SELECT are clear i think.
An the second case where CASE ELSE might be usefull is my bad try with the paddle/Raft-boat example. I want to show that it could make sense to set with CASE ELSE a variable or call a function that is important for a following CASE that is a match. Eventually that wasnt really clear. If someone have a better example, i am curious.
An implementation of >,< ... is that what i find really useful.