Page 1 of 1
Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 3:52 pm
by Marc56us
Hi Team,
As a follow-up to:
viewtopic.php?f=13&t=71741
PB
Select/Case use 'short-circuit' method, but sometime we like to test some/all cases
successively like in C or PHP
Doing something like (with Continue)
Code: Select all
A = 10
Select A
Case 1 To 10
Debug "> 1 and < 10"
Continue
Case 1 To 20
Debug "> 1 and < 20"
EndSelect
Display error "[COMPILER] Line 5: 'Continue' can not be used outside a loop." (this is normal)
C use Break is short-circuit is needed
Code: Select all
switch ( c )
{
case 'A':
capa++;
break;
case 'a':
lettera++;
break;
default:
nota++;
}
I don't think it would be possible to go back because all existing codes would have to be modified,

but would it be possible to add a keyword 'Continue' (or a new keyword, i.e
NextCase,
NoBreak or
ContinueCase) as an option ?

Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 4:30 pm
by Little John
-1 from me.
Marc56us wrote:PB Select/Case use 'short-circuit' method
The Switch statement in PHP uses so-called "
Fallthrough". As I have seen in other programming related discussion forums, this causes a lot of confusion especially in beginners, and is a likely cause of bugs. PureBasic's
Select without fallthrough is much cleaner and follows the principle of least surprise.
Marc56us wrote:but sometime we like to test some/all cases successively like in C or PHP
Fallthrough for
Select is not necessary at all, because we always can do everything with
If,
ElseIf, and
Else.
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 5:03 pm
by Marc56us
Little John wrote:
The Switch statement in PHP uses so-called "
Fallthrough". As I have seen in other programming related discussion forums, this causes a lot of confusion especially in beginners, and is a likely cause of bugs. PureBasic's
Select without fallthrough is much cleaner and follows the principle of least surprise.
This is why I ask for an option syntax
but would it be possible to add a keyword 'Continue' (or a new keyword, i.e NextCase, NoBreak or ContinueCase) as an option ?
Little John wrote:Fallthrough is not necessary at all, because we always can do everything with If, ElseIf, and Else.
It is then necessary to write much more code, because by default,
ElseIf allows you to test
several formulas, but always
exit after the one who match.
No short-circuit method like C allows to test a single formula but several
successive actions.
Code: Select all
int a = 20;
int Total;
switch(a)
{
case > 0:
Total++;
case > 5:
Total++;
case > 10:
Total++;
}
Total = 3
+1 +1
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 5:11 pm
by Little John
Marc56us wrote:This is why I ask for an option syntax
An unnecessary option that allows to write less readable code doesn't make sense to me.
Marc56us wrote:Little John wrote:Fallthrough is not necessary at all, because we always can do everything with If, ElseIf, and Else.
It is then necessary to write much more code, because by default,
ElseIf allows you to test
several formulas, but always
exit after the one who match.
No short-circuit method like C allows to test a single formula but several
successive actions.
You are not forced to use
ElseIf. You can use a sequence of
If statements.
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 5:17 pm
by Marc56us
Briefly, my previous code would give this:
Code: Select all
a = 20
If a > 0 : Total + 1 : EndIf
If a > 5 : Total + 1 : EndIf
If a > 10 : Total + 1 : EndIf
In my opinion, it's less readable, and I think it requires more instructions for the compiler.
'If' will recalculate at each line instead of simply reusing the result.
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 10:23 pm
by Little John
Marc56us wrote:I think it requires more instructions for the compiler.
'If' will recalculate at each line instead of simply reusing the result.
In both versions, there are 3 comparisons and 3 additions. in the end, I'm not interested in the number of instructions, but in the speed.
Select and
If cannot be compared directly in that way anyway, since there are general differences in the way they are implemented in PB. Actually, we can't know how many instructions or time it requires in PB since it is not implemented.
As I wrote, especially beginners have problems with fallthrough in
Select. We already have enough possibilities to introduce bugs into our programs. We do not need additional ones.
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 11:01 pm
by skywalk
I agree the C behavior of the switch statement is problematic and by far creates many many break lines.
There is a real possibility it will be amended in future C standards as fallthrough's represent barely 3% of many sampled code bases. This would introduce a Fallthrough Compiler option/command.
I prefer we stick with Select as is.
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 11:02 pm
by davido
-1
Re: Select / Case option option for no short-circuit method
Posted: Sat Nov 17, 2018 11:43 pm
by Demivec
My hack (macros need to be used in pairs and within the same Select/EndSelect block):
Code: Select all
;Author: Demivec
;Created: 11/17/2018
;Version: v1.00
;OS: All
;Compiler: PureBasic v5.70 LTS beta 2
;Description: The following two macros should always be paired, that is, used the the same number of times
; and within the same Select/EndSelect block..
;Placed at the end of a Select's 'case' to allow it to continue executiion in another 'case'
Macro S_Continue
Goto sc_flag#MacroExpandedCount#
EndMacro
;The point at which the immediately preceding use of the S_Continue macro will continue execution
Macro S_ContinuePoint
sc_flag#MacroExpandedCount#:
EndMacro
CompilerIf #PB_Compiler_IsMainFile
Define a, total, max
a = 20
total = 0
max = 99999999
Debug "before select execution a = " + a + ", total = " + total
Select a
Case 1 To max
total + 1
S_Continue
Case 5 To max
S_ContinuePoint
total + 1
S_Continue
Case 10 To max
S_ContinuePoint
total + 1
EndSelect
Debug "after select execution a = " + a + ", total = " + total
;a more obscure example of executing the same code after any succesfully matching 'case'
a = 20
total = 0
Debug "before select execution a = " + a + ", total = " + total
Select a
Case 1 To max
total + 1
S_Continue
Case 5 To max
total + 1
S_Continue
Case 10 To max
total + 1
S_Continue
Case -max ;an supposedly unreachable case ;meant to be executed only if one of the other cases matches
S_ContinuePoint
S_ContinuePoint
S_ContinuePoint
a + 5
EndSelect
Debug "after select execution a = " + a + ", total = " + total
CompilerEndIf
As you can see there are a couple of other problems that make things a bit strange. One being that all types of {expressions} aren't allowed in the 'case' statement, only equality is and a ranged version of equality where you have to specify the starting and ending point.
Note: the above hack can probably be misused further for some other situations.
