PureBasic Select vs PHP Switch

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

PureBasic Select vs PHP Switch

Post by Dude »

I was just reading this page about the PHP "Switch" statement:

https://www.w3schools.com/php/php_switch.asp

And it says to use "Break" after each "Case" to prevent the code from running into the next case automatically.

We don't have to do this in PureBasic, right? The manual doesn't say to, but after reading the above, I'm not sure. :?:

The PHP example shows "Break" being used after every "Case":

Code: Select all

 <?php
$favcolor = "red";
switch ($favcolor) {
    case "red":
        echo "Your favorite color is red!";
        break;
    case "blue":
        echo "Your favorite color is blue!";
        break;
    case "green":
        echo "Your favorite color is green!";
        break;
    default:
        echo "Your favorite color is neither red, blue, nor green!";
}
?> 
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: PureBasic Select vs PHP Switch

Post by srod »

Yes you do not need it in PB.
I may look like a mule, but I'm not a complete ass.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: PureBasic Select vs PHP Switch

Post by Dude »

Thanks for clarifying, srod. :)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: PureBasic Select vs PHP Switch

Post by Marc56us »

Yes, this is the so-called 'short circuit' method. At the first condition met, we leave the loop.

This is both good and bad.
+ Good, because it's fast.
- Bad, because you can't test several successive conditions (and the 'continue' function doesn't allow you to continue)

i.e, you can't do something like this in PB, but you can in C and PHP and some others languages

Code: Select all

A = 10

Select A
        
    Case 1 To 10
        Debug "> 1 and < 10"
        
    Case 1 To 20
        Debug "> 1 and < 20"
        
EndSelect
So I prefer the implementation of PHP (and the C language before it) even if we should always think about adding a keyword (break)
At least several successive conditions can be tested without adding ANDs

:idea: I don't think it would be possible to go back Because all existing codes would have to be modified, but it would be possible to add 'Continue' in this context :?:

:wink:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: PureBasic Select vs PHP Switch

Post by Josh »

Help wrote:... it will then execute the corresponding code and quit the Select structure.
The help is actually clear to me. Even if it was unclear to me, I would test it with a short program, because it's faster than posting here.


But be careful with a CompilerSelect, there is a bug in the compiler.

Code: Select all

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    Debug "Windows"
  CompilerCase #PB_OS_MacOS
    Debug "MacOS"
  CompilerCase #PB_OS_Linux
    Debug "Linux"
  CompilerCase #PB_Compiler_OS
    Debug "Second Case for OS"
CompilerEndSelect
Marc56us wrote:I don't think it would be possible to go back Because all existing codes would have to be modified, but it would be possible to add 'Continue' in this context
Good idea, you should post this in Feature Requests and Wishlists
sorry for my bad english
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: PureBasic Select vs PHP Switch

Post by Tenaja »

No, PB does not require it.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: PureBasic Select vs PHP Switch

Post by Dude »

Tenaja wrote:No, PB does not require it.
It's just a bot posting what I posted at the start of this thread.
DarkDragon
Addict
Addict
Posts: 2218
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: PureBasic Select vs PHP Switch

Post by DarkDragon »

Dude wrote:
Tenaja wrote:No, PB does not require it.
It's just a bot posting what I posted at the start of this thread.
With a slightly modified link!!!
bye,
Daniel
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Select vs PHP Switch

Post by TI-994A »

DarkDragon wrote:With a slightly modified link!!!
And code! :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: PureBasic Select vs PHP Switch

Post by Dude »

And the description: I wrote "read" and it wrote "perused", and I wrote "use" and it changed it to "utilize". :shock: Pretty smart actually.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Select vs PHP Switch

Post by TI-994A »

Dude wrote:And the description: I wrote "read" and it wrote "perused", and I wrote "use" and it changed it to "utilize". :shock: Pretty smart actually.
AI in action.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: PureBasic Select vs PHP Switch

Post by Tenaja »

Dude wrote:
Tenaja wrote:No, PB does not require it.
It's just a bot posting what I posted at the start of this thread.
I wondered, but I don't get as much time here as I'd like, so I was like "hey, a question I can actually answer!" Someday, I'll have more time for coding fun...
Post Reply