Jump to content



Photo
- - - - -

Conditional Branch

CasioBasic switch/case

  • Please log in to reply
3 replies to this topic

#1 Krtyski

Krtyski

    Casio Freak

  • Members
  • PipPipPipPip
  • 132 posts
  • Gender:Male
  • Location:Tokyo, Japan
  • Interests:programming, smooth Jazz and 4-wheel driving.

  • Calculators:
    FX-502P, FX-602P, FX-603P,
    fx-4000P, fx-7000G,
    fx-4500P, fx-4800P
    fx-5800P,
    CFX-9850G,
    CFX-9850GC PLUS
    fx-9860G,
    fx-9860G AU,
    fx-9860G Slim
    fx-9860GII SD,
    fx-9860GII-2,
    fx-9860GII-2 SD,
    fx-CG20, fx-CG50,
    fx-CP400
    fx-9860GIII
    fx-9750GIII
    fx-7400GIII

Posted 21 February 2014 - 01:19 PM

There is no multiple conditional branch statement in CasioBasic. So I've used following way just like switch/case.

I usually use fx-5800P and there is no 'ElseIf' available, I think it's same with fx-9860GII or fx-cg10.


E=3=>Goto 3
E=2=>Goto 2
E=1=>Goto 1
E=0=>Goto 0
Goto Z

Lbl 3
[process 3]
(Goto Z)

Lbl 2
[process 2]
(Goto Z)

Lbl 1
[process 1]
(Goto Z)

Lbl 0
[process 0]
(Goto Z)

Lbl Z
[process Z]

(Goto Z) is not necessarily used just like 'break;' with switch() in C-Language, but Goto Z and Lbl Z must be used to avoid 'spaghetti'.

I usually use this for exception handling in CasioBasic. The variable E is an exception flag.

There are many arguments about goto, but I believe this usage in CasioBasic counts for high readability and minimal bug creation.


Is there some good idea for multiple conditional branch in CasioBasic?

Edited by Krtyski, 21 February 2014 - 01:36 PM.


#2 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 21 February 2014 - 05:49 PM

Why not this?

If E=3
Then [process 3]

Else If E=2
Then [process 2]
IfEnd

Else If E=1
Then [process 1]
IfEnd

IfEnd


#3 Krtyski

Krtyski

    Casio Freak

  • Members
  • PipPipPipPip
  • 132 posts
  • Gender:Male
  • Location:Tokyo, Japan
  • Interests:programming, smooth Jazz and 4-wheel driving.

  • Calculators:
    FX-502P, FX-602P, FX-603P,
    fx-4000P, fx-7000G,
    fx-4500P, fx-4800P
    fx-5800P,
    CFX-9850G,
    CFX-9850GC PLUS
    fx-9860G,
    fx-9860G AU,
    fx-9860G Slim
    fx-9860GII SD,
    fx-9860GII-2,
    fx-9860GII-2 SD,
    fx-CG20, fx-CG50,
    fx-CP400
    fx-9860GIII
    fx-9750GIII
    fx-7400GIII

Posted 22 February 2014 - 10:59 AM

Why not this?

If E=3
Then [process 3]

Else If E=2
Then [process 2]
IfEnd

Else If E=1
Then [process 1]
IfEnd

IfEnd



Oh!, I've not tired that way and only thought "If" is not allowed coming right after "Else" .....

I over looked 3 IfEnd.

Since I do not like to write annoying IfEnd more than 2, just want to write simply like...

switch (E) {
case 3:
[process 3]
case 2:
[process 2]
case 1:
[process 1]
}



Thanks

Edited by Krtyski, 22 February 2014 - 12:39 PM.


#4 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 22 February 2014 - 03:53 PM

It seems like the Goto/Lbl solution was more complex though, and it required more typing. Also, it uses up Lbl's that could be used on other things, not to mention that Goto is considered harmful! :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users