Switch statement without break is not logical

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Switch statement without break is not logical

Post by mrcoollion » Fri Nov 05, 2021 11:58 am

Hello all,

Not that I cannot do it differently with an If-Then but I think that the Switch statement workings are in my opinion not logical.
Of course, when using a break in a case the switch statement only performs the first case that is true. However, I would expect that without the break statement it would assess the next case and only executes the statements belonging to that case if the case is true.
Instead in the below setup it also does Answer B.

Code: Select all

   // Test Switch Case statement
   put 1 into A
   put 2 into B
   Put 1 into C
   switch 
      case A=1
         answer "A = 1"
      case B=1
         answer "B = 1"
      case C=1
         answer "C = 1"
   end switch
   // Result: All cases are run also the B Case which should not run because the Case is not true.
Do you think this is correct?

Regards,

MrCoolLion (or Paul)

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: Switch statement without break is not logical

Post by LCMark » Fri Nov 05, 2021 12:44 pm

@mccoolliion: The switch statement structure is the standard form you find in other languages where `fallthrough` is a natural thing to have - and indeed useful in some specific circumstances.

Fallthrough means that there is no special handling for cases which have no code - allowing 'grouping' of cases:

Code: Select all

switch tOption
case "A"
case "B"
   answer "the case was A or B"
   break
default
   answer "the case was something else"
   break
end switch
In general 'fallthrough' is not needed all that much - but if you take it away then it removes a rather specific ability of switch - its ability to be used to model unstructured control flow (more specifically low-level code execution) - i.e. what you can do if you have goto and labels:

Code: Select all

repeat forever
    switch tLineNumber
    case 10
        put 0 into tX
    case 20
        if tX > 10 then
           put 50 into tLineNumber
           break
        end if
     case 30
        add 1 to tX
     case 40
         put 20 into tLineNumber
         break
     case 50
         exit repeat
     end switch
 end repeat
 
This gives the same behavior as a old-school BASIC program:

Code: Select all

10 X = 0
20 IF X > 10 THEN 50
30 X = X + 1
40 GOTO 20
50 STOP
This might seem a rather esoteric case - but if you do not have a switch statement which works like that then it makes it very difficult to represent such processes.

For a more modern example, the asm.js (not WASM!) version of emscripten would fallback to using a switch like that in cases where it could not convert C code to structured control flow (C has goto and labels, JS does not).

That being said, it would be better if you had to mark fallthrough explicitly - to prevent accidental 'missing break' errors :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Switch statement without break is not logical

Post by dunbarx » Fri Nov 05, 2021 2:52 pm

MrCoolion.

Check out this thread:

viewtopic.php?f=9&t=35611&hilit=switch

Craig

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Switch statement without break is not logical

Post by stam » Fri Nov 05, 2021 8:44 pm

I see the OP's point - that the case statement does not do a conditional check - but that's not the intended use for switch i don't think.

Really the OP is checking 3 different conditionals. I don't even think a single encompassing if-then structure would be helpful, but rather:

Code: Select all

    if A=1 then answer "A = 1"
    if B=1 then answer "B = 1"
    if C=1 then answer "C = 1"
switch is more to pick out het first correct statement (or if you want to fall through the cases, just omit the break and they'll all be executed until a break shows up or you reach a default or end of case list.

At least that's how i think of it...

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Switch statement without break is not logical

Post by mwieder » Sat Nov 06, 2021 4:45 pm

C has goto and labels
Oy. I admit to having the need to use those occasionally, but I've always felt dirty afterwards.

Post Reply