[solved] 2 bugs with switch/case

Things you’d like to miss in the future...
Post Reply
PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

[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.
Win11 Pro 223H2 Gerrman

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: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

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: )
Win11 Pro 223H2 Gerrman

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @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: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

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:
Win11 Pro 223H2 Gerrman

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: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: [solved] 2 bugs with switch/case

Post by admin »

Thanks, right in time! :tup:

Post Reply