

I hope that this enhancements gets implemented.
What effect should Case Else have in your opinion? Can you please give an example?PeWa wrote: Tue Apr 29, 2025 4:15 pm an enhancement of SELECT CASE was requested In old posts (2003,2004 ...), but relational operators are until today not implemented. In Visual Basic i see that relational operators like <,>, ... are implemented with the command IS, and there are also the ELSE CASE (or CASE ELSE ?) command. I find that very useful in extension to the DEFAULT option of Purebasic, and i think relational operators wants everyone
I hope that this enhancements gets implemented.
Little John wrote: Tue Apr 29, 2025 5:20 pm What effect should Case Else have in your opinion? Can you please give an example?
@Little John and QuinLittle John wrote: Tue Apr 29, 2025 5:20 pmWhat effect should Case Else have in your opinion? Can you please give an example?PeWa wrote: Tue Apr 29, 2025 4:15 pm an enhancement of SELECT CASE was requested In old posts (2003,2004 ...), but relational operators are until today not implemented. In Visual Basic i see that relational operators like <,>, ... are implemented with the command IS, and there are also the ELSE CASE (or CASE ELSE ?) command. I find that very useful in extension to the DEFAULT option of Purebasic, and i think relational operators wants everyone
I hope that this enhancements gets implemented.
Code: Select all
; Lets asume, somebody enters a number and we want to print some answers about it
Number=123
Select Number
Case 1 : Debug "The number is one"
Case Else Debug "The number is not one"
Case 2 : Debug "The number is two"
Case Else Debug "The number is not two"
Case 3 : Debug "The number is three"
Case Else Debug "The number is not three"
Case 4 : Debug "The number is four"
Case Else Debug "The number is not four"
Case 5 : Debug "The number is five"
Case Else Debug "The number is not five"
Default Debug "The number is above 5 (or null ??? :-)))
EndSelect
Code: Select all
; Same as before, but now we dont reach DEFAULT
Number=5
Select Number
Case 1 : Debug "The number is one"
Case Else Debug "The number is not one"
Case 2 : Debug "The number is two"
Case Else Debug "The number is not two"
Case 3 : Debug "The number is three"
Case Else Debug "The number is not three"
Case 4 : Debug "The number is four"
Case Else Debug "The number is not four"
Case 5 : Debug "The number is five"
Case Else Debug "The number is not five"
Default Debug "The number is above 5 (or null ??? :-)))
EndSelect
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
Select OverTheSea
Case 1
Debug "The Paddles are stolen.What know ? You cant take the raft or boat"
PaddlesStolen=1
Case Else ; No match, so the paddles are available and with the CASE ELSE we can set the Variable BEFORE we come
;to Case 3 or 4
Debug "The Paddles are available"
PaddlesStolen=0
Case 2
Debug "He takes the way around the sea"
Case 3 ; He takes a boat, and the variable ist set.
If PaddlesStolen=2
Debug "The Paddles are faulty,will you risk to try to come over the see with the boat ?"
If PaddlesStolen=0
Debug "He take the boat, the paddles and try to reach the other side of the sea"
BoatOverSeaAnimation=1
; Call Procedure for some animation of the boat over the sea
BoatAnimation ()
EndIf
Case 4
; same as case 3 if he takes the raft with raft animation
Default
Debug "He dont know how to conquer the sea"
EndSelect
I come from a language background where case statements can support multiple variables and conditional tests, which I miss, but we have If/ElseIf.PB supports multicase and the 'To' keyword which allow ranged tests, which is enough for case IMHO. If you need complex compare, If/ElseIf is the way. https://www.purebasic.fr/english/viewto ... b1#p617690
@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: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 errorsThat should be enough for now.
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
You can do multiple variables and conditional tests in a Select Case. I'm not sure if it is as efficient or speedy but you might be in the group that considers it to have more clarity. Do you have an example of what you would like to do but can't?PBJim wrote: Tue Apr 29, 2025 9:55 pm I come from a language background where case statements can support multiple variables and conditional tests, which I miss, but we have If/ElseIf.
To be honest, I'm fairly happy with PureBasic's case statement as it is Demivec. It follows a very common form of specifying the argument once, within the Select statement, so there's a practical constraint in terms of how that might become more elaborate or extended further.
Code: Select all
begin case
case acctype = "C" and amount > cutoff
.. do something
case delflag = "D" or acctype = "D"
.. do something
case amount > limit
.. do something
case 1
.. do the default
end case
PBJim wrote: Wed Apr 30, 2025 10:56 amCode: Select all
begin case case acctype = "C" and amount > cutoff .. do something case delflag = "D" or acctype = "D" .. do something case amount > limit .. do something case 1 .. do the default end case
Code: Select all
begin if
if acctype = "C" and amount > cutoff
.. do something
elseif delflag = "D" or acctype = "D"
.. do something
elseif amount > limit
.. do something
elseif 1
.. do the default
end if
To be honest, I'm fairly happy with PureBasic's case statement as it is Demivec. It follows a very common form of specifying the argument once, within the Select statement, so there's a practical constraint in terms of how that might become more elaborate or extended further.PBJim wrote: Wed Apr 30, 2025 10:56 am Do you have an example of what you would like to do but can't?
Code: Select all
begin case
case acctype = "C" and amount > cutoff
.. do something
case delflag = "D" or acctype = "D"
.. do something
case amount > limit
.. do something
case 1
.. do the default
end case
Code: Select all
Procedure decide(acctype.s, delflag.s, amount.i, limit.i, cutoff.i)
Select #True
Case Bool((acctype = "C") And (amount > cutoff))
Debug "acctype('" + acctype + "') = 'C' And amount(" + amount + ") > cutoff(" + cutoff + ")"
;.. do something
Case Bool((delflag = "D") Or (acctype = "D"))
Debug "delflag('" + delflag + "') = 'D' Or acctype('" + acctype + "') = 'D'"
;.. do something
Case Bool(amount > limit)
Debug "amount(" + amount + ") > limit(" + limit + ")"
;.. do something
Default
Debug "Default, acctype('" + acctype + "'); delflag('" + delflag + "'), amount(" + amount + "), limit(" + limit + "), cutoff(" + cutoff + ")"
;.. do the default
EndSelect
Debug LSet("", 40, "-")
EndProcedure
decide("C", "D", 30000, 8000, 29800)
decide("C", "D", 2550, 8000, 29800
decide("D", "A", 2550, 8000, 29800)
decide("A", "N", 2, 8000, 29800)
decide("A", "N", 8001, 8000, 29800)
decide("B", "D", 10000, 8000, 29800)
decide("C", "A", 75, 8000, 29800)
My thoughts exactly.Demivec wrote: Tue Apr 29, 2025 10:59 pm 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.