I hope that this enhancements gets implemented.
SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.
I hope that this enhancements gets implemented.
-
Little John
- Addict

- Posts: 4812
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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?
I'd think it'd be the exact same as Default, when I learned VB I was taught that Case Else is the same as Default in C/PB.
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
@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.
To be honest, i looked under this URL (Visual Basic)
https://learn.microsoft.com/en-us/dotne ... -statement
and dont think further about CASE ELSE
It seems that Quin is right: in the example it seems to be the same as DEFAULT in PB.
But after a little bit of thinking it makes sense for me in PB if we implement it in the following way :
Example:
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
Under default we can not react on different cases. In this example of course we could make the debugs all under the DEFAULT, right, thats true. But i think this could eventuelly be in rare cases an enhancement for the SELECT CASE command.
This allows the possibility to call procedures ect. if it is not a match even if we dont reach DEFAULT because we have a case match in the SELECT CASE Cycle.
Example :
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
By the way: is there a programming language that has something like that implemented ? If not, this could be something special for our beloved PUREBASIC
Or am i totally wrong ?
Last edited by PeWa on Tue Apr 29, 2025 7:07 pm, edited 1 time in total.
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
As a note : my original intent was the relational operators, i believe they a useful.
I know, if someone wants to make a joke, he could say: "Use IF...THEN...ELSE"
I know, if someone wants to make a joke, he could say: "Use IF...THEN...ELSE"
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
I just thought of another use case where CASE ELSE would actually make sense if DEFAULT is reached: namely, when, for a number like in the example, a procedure or something else must be executed BEFORE the next CASE query if there is no match.
But enough about that now; perhaps others can think of some other cases that make sense. That's if I haven't made another mistake. It happens occasionally
Who else finds this is a useful extension ?
But enough about that now; perhaps others can think of some other cases that make sense. That's if I haven't made another mistake. It happens occasionally
Who else finds this is a useful extension ?
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.
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
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
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
While improvements of this nature can certainly be beneficial, Fred has expressed an opinion on Select last year :
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
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
@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
EndIfHowever, 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.
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
i was avoiding SELECT - CASE most the time , use IF-THEN -ELSE make more fun
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.
However, since you asked, to give a comparison with others — some languages provide the complete argument and conditional evaluation within each Case clause, thus making their logic independent of each other. That approach shifts the scope of Select beyond logic conditions relating to a single value argument, potentially better representing business logic or rules which aren't concerned merely with one value. But as I say, PureBasic — indeed like many languages — already takes a different intrinsic approach and perhaps it's difficult therefore to integrate something like the below, without resorting to adding a new language statement altogether :
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 caseI'm not advocating doing this, as I think PureBasic's scope in this regard is okay and it's important to be sympathetic towards the developers' resources. Still good to chat about it though
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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
just replace the 'case' with 'if' , and 'elseif' that's dose it all , see this
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
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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?
However, since you asked, to give a comparison with others — some languages provide the complete argument and conditional evaluation within each Case clause, thus making their logic independent of each other. That approach shifts the scope of Select beyond logic conditions relating to a single value argument, potentially better representing business logic or rules which aren't concerned merely with one value. But as I say, PureBasic — indeed like many languages — already takes a different intrinsic approach and perhaps it's difficult therefore to integrate something like the below, without resorting to adding a new language statement altogether :
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 caseI'm not advocating doing this, as I think PureBasic's scope in this regard is okay and it's important to be sympathetic towards the developers' resources. Still good to chat about it though
[/quote]
Your example case (pun intended) can be handled quite easily:
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)As I said, it may not result in a speed improvement over a series of If/EndIf statements but it does give some clarity in showing the precedence of the conditions being tested for.
-
Little John
- Addict

- Posts: 4812
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: SELECT CASE with relational operators and ELSE CASE like Visual Basic and others
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.

