[solved] 2 bugs with switch/case

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

[solved] 2 bugs with switch/case

Post by PeterH »

In the Switch-stmt I see 2 bugs:

- the commands "Case" *must* be written lower case
- if multiple case-groups, followed by a default:, and no case matches the select-value, the last case is executed, the default: is ignored. Test:

Code: Select all

   $x = 'a';
   Switch ($x) {
      case 'b':
         Echo 'b!';
         Break;
      case 'c':
         Echo 'c!';
         Break;
      case "d":
         Echo 'd!';
         Break;
      default:
         Echo 'Other!';
   }
(If you delete the "d"-group, the 'c'-Group will be executed.)


By the way: I wouldn't call the "operands" of the cases "labels" - I'd name them values. This would fit much more the comparison to IF/ELSEIF/ELSE: they are just values, too.

So I'd write:

Code: Select all

   switch (value) {
      case value1;
         code to be executed if value=value1;
         break;
...
...
      default:
         executed if no case matches value
   }
Last edited by PeterH on 04 Mar 2017 12:58, edited 1 time in total.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: 2 bugs with switch/case

Post by TheQwerty »

RE #2: This appears to be related to Break being case-sensitive.

#3: Syntax checking shouldn't complain about this:

Code: Select all

function Test($v) {
  switch ($v) {
    case 'a': return 1;
    case 'b': return 2;
    default: return -1;
  }
}


"Test"
  Echo Test('a');
  Echo Test('b');
  Echo Test('c');
PeterH wrote:By the way: I wouldn't call the "operands" of the cases "labels" - I'd name them values. This would fit much more the comparison to IF/ELSEIF/ELSE: they are just values, too.
I believe 'label' is correct here. If XY's implementation didn't include break and could only execute a single case branch then it might be less accurate. Instead XY's acts more like a goto and jumps to the matching case label - which is consistent with many other languages. See also Wikipedia.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: 2 bugs with switch/case

Post by PeterH »

Hm - Break is not case sensitive.
BUT: if the last break in front of default: is written as Break, it's (prepending!) case instruction is interpreted as true!
So your statement is also a bit correct :shock:

To your example: with syntax check active, for 'c' it shows an error: "Dubious syntax: return", then after "Continue" it's being followed by "'default:' is not a valid script command.". Then "" is returned.

As a hint: the stepping-window shows the line "default: return -1;" aligned below "return 2;", not as expected below "case 'b';". So there seems to be some misinterpretation.

To label/value:
- here the same happens as by If/Elseif/else for "values", so I would compare
- for (modern) script languages I wouldn't want to talk about "jumping". I think this is a word from a long time ago, isn't it?
(I must confess: I did bad bad branching in Assembler :mrgreen: )

admin
Site Admin
Posts: 67271
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: 2 bugs with switch/case

Post by admin »

Thanks and fixed. :tup: :cup:

Code: Select all

    ! Scripting: The keywords "break", "case", "default", and "return" were not 
      case-insensitive in all contexts. Fixed.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: [solved] 2 bugs with switch/case

Post by PeterH »

Tested: works as expected.
(Even with caps in Default: - I didn't test that before.)

Thanks, again! :om: :cup:

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: 2 bugs with switch/case

Post by TheQwerty »

TheQwerty wrote:#3: Syntax checking shouldn't complain about this:

Code: Select all

function Test($v) {
  switch ($v) {
    case 'a': return 1;
    case 'b': return 2;
    default: return -1;
  }
}


"Test"
  Echo Test('a');
  Echo Test('b');
  Echo Test('c');
Don, this issue remains in v17.60.0112.

admin
Site Admin
Posts: 67271
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: [solved] 2 bugs with switch/case

Post by admin »

Thanks, right in time! :tup:

Post Reply