Use of Switch and SC end

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Use of Switch and SC end

Post by klownboy »

Hi, I have a question concerning the use of "switch". It seems in my testing that if an SC end (1) is encountered in a switch case after a SC break, even though the switch case is NOT true, the entire script ends. Why is that? See example below. Shouldn't we be able to in certain situations when the case is true, not only break but end or exit the script such that following code after the switch is not executed, yet, not have the 'SC end' exit the script when a false situation is encountered. Is it a possible bug or is that the way it is? It seems there would be a number of cases which could arise where the scripter would want to end the script in a true case. Edited: had it backwards.

Also, is it possible to use a case like '>3' or eval (> '3') in the above example to compare to the switch '4' '?

Code: Select all

// switch
 step;
  $favNumber = "3";  //first try with '3' then try it with '4'
  switch ($favNumber)  {
   case "2":
       echo "Your favorite number is 2.";
       break; end(1);
   case "3":
       echo "Your favorite number is 3.";
       break;
   case ">";
       echo "Your favorite number is greater than 3.";
       break;
  }
  echo "Switch done!";
Thanks,
Ken
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Use of Switch and SC end

Post by LittleBiG »

I know only this way for the second topic:

Code: Select all

// switch
 step;
  $favNumber = "3";  //first try with '3' then try it with '4'
  switch (true)  {
   case $favNumber == 2:
       echo "Your favorite number is 2.";
       break; end(1);
   case $favNumber == 3:
       echo "Your favorite number is 3.";
       break;
   case $favNumber > 3;
       echo "Your favorite number is greater than 3.";
       break;
  }
  echo "Switch done!";

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Use of Switch and SC end

Post by klownboy »

Thanks LittleBig for your help on the second part of my question. I should have tried what Don calls in the help, "this somehow perverted use of a switch". I wonder if there's some other method or syntax which could be used to obtain the correct result without using switch (true)...the perverted use. :)

Edit 1: I suppose in some cases I could use the default case to cover any numbers that weren't covered by the other cases. So default would cover $favNumber > 4 and still use the normal method (non perverted).

Edit2: I did discover that if you position the SC end(1) before the SC break, the script ends when the case is true and is not executed if it's false. So you could use the 'end' when you have nothing else to do in the script for that case. If you want to continue in the script after the switch, don't use the end.

What are your thoughts on the first part? Do you think the SC end(1) statement should be ignored instead of executed after the break when the case is FALSE?

Thanks again.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Use of Switch and SC end

Post by LittleBiG »

klownboy wrote:What are your thoughts on the first part? Do you think the SC end(1) statement should be ignored instead of executed after the break when the case is FALSE?
There is an important comment in the help: "Each case should be closed by a break statement. " So it is logical to continue the process on the next command after the "break". It is better than simply ignoring it. Consider it as a feature: if a case condition is false, you can run some code before the evaluation of the next case. Like in this meaningless code fraction:

Code: Select all

  switch (true)  {
   case $favNumber == 1:
       $result = $result . "1";
       break;
       $result = $result . "0";
   case $favNumber == 2:
If the $favNumber is not 1, a zero will be added to the $result, then the next case condition will be checked.

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Use of Switch and SC end

Post by klownboy »

Yes, experimenting I now see what you mean. The part of the story I wasn't clear on was the code after the break will execute even if the case is false which is exactly why the SC end (1) exited the script in a false case in my first post. :wink: I was initially thinking, when the case ended in the break, the next case was immediately started, but no, not until it finished executing code after the break. I'm not sure if that a good thing or bad thing, but as you said, it can be used to our advantage. Thanks. :beer:

Edit: Help states,
Usually each case should be closed by a break statement. Else processing continues with the next case (sometimes though this can be desired).
That makes sense and Don provides a good example. It certainly seems inconsistent though that when a case is false, code after the break within that case is executed before the next case is looked examined. Whereas, if the case is true, the code after the break is not executed. I would think Don might want to clarify that in the help file if that's what he intended.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Use of Switch and SC end

Post by klownboy »

Hi Don, you may not have seen this question initially since you were away. It concerns the use of switch and break. I'll summarize so you don't have to bother looking at the above posts. Please run the example script below first with a $favNumber of '3' and then with '2'.

Code: Select all

  step; 
  $favNumber = "3";   //next run try a $favNumber as '2'
  switch ($favNumber)  {
   case "2":
       echo "Message for Case 2 - BEFORE the break."; //end(1);
       break;
       echo Message for Case 2 - AFTER the break; //end(1);
   case "3":
       echo "Your favorite number is 3."; //end(1);
       break;
       end(1);
   case '4';
       echo "Case 4 - Your favorite number is greater than 3.";
       break;
  }
  echo "Switch test done!";
In the help file for 'switch' you mention what happens if there is no break - execution continues to the next case, but you don't talk about the fact that code, inserted after a break before the next case, is executed when the case is false. Whereas, if the case is true any code after the break is not executed. Is this flow path intentional or is it a possible bug? If it is indeed the way you'd expect, it seems to be rather important to the use of switch and break. You may want to you consider mentioning it in the help file.

I just happened to stumble on it because I had an SC end after a break for false case before the next case.
Thanks,
Ken
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply