Page 1 of 1
PureBasic Select vs PHP Switch
Posted: Sat Nov 17, 2018 1:55 pm
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!";
}
?>
Re: PureBasic Select vs PHP Switch
Posted: Sat Nov 17, 2018 1:58 pm
by srod
Yes you do not need it in PB.
Re: PureBasic Select vs PHP Switch
Posted: Sat Nov 17, 2018 2:12 pm
by Dude
Thanks for clarifying, srod.

Re: PureBasic Select vs PHP Switch
Posted: Sat Nov 17, 2018 3:00 pm
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

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

Re: PureBasic Select vs PHP Switch
Posted: Sat Nov 17, 2018 3:09 pm
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
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 8:13 am
by Tenaja
No, PB does not require it.
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 8:33 am
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.
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 10:07 am
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!!!
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 10:14 am
by TI-994A
DarkDragon wrote:With a slightly modified link!!!
And code! 
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 10:59 am
by Dude
And the description: I wrote "read" and it wrote "perused", and I wrote "use" and it changed it to "utilize".

Pretty smart actually.
Re: PureBasic Select vs PHP Switch
Posted: Sat Mar 30, 2019 11:23 am
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".

Pretty smart actually.
AI in action.
Re: PureBasic Select vs PHP Switch
Posted: Sun Mar 31, 2019 3:09 am
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...