Select / Case option option for no short-circuit method

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Select / Case option option for no short-circuit method

Post by Marc56us »

Hi Team, :D

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,
:idea: but would it be possible to add a keyword 'Continue' (or a new keyword, i.e NextCase, NoBreak or ContinueCase) as an option ?

:wink:
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Select / Case option option for no short-circuit method

Post 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.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Select / Case option option for no short-circuit method

Post 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
:wink:

+1 +1
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Select / Case option option for no short-circuit method

Post 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.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Select / Case option option for no short-circuit method

Post 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.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Select / Case option option for no short-circuit method

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Select / Case option option for no short-circuit method

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Select / Case option option for no short-circuit method

Post by davido »

-1
DE AA EB
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Select / Case option option for no short-circuit method

Post 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. :mrgreen:
Post Reply