Jump to content

Select and Bool value


Terenz
 Share

Recommended Posts

Hello,

I have three variable can be True or False. I was build a Select...Case...EndSelect in this way:

Global $A = 0, $B = 0, $C = 0

Select
    Case $A = 0 And $B = 0 And $C = 0
        ; do a thing
    Case $A = 0 And $B = 0 And $C = 1
        ; etc
EndSelect

There is a more efficent way to write the code or i just need to set all the eight possible combination? And in what order? 

Since some operation are in common ( like when $A is True in 4 different Case ) i was thinking there is a better way to write this. Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

With 8 combinations the way you're going about it seems most likely.  I would start, personally, with looking at whether I could shorten the code any; do any of them cause me to call the same function, for example. If you were running the same function or call if any of them equal 1, you could do something like this:

Local $A = 0, $B = 0, $C = 1
Local $x = ($A + $B + $C)

Switch $x
    Case 0
        ConsoleWrite("X = 0" & @CRLF)
    Case 1
        ConsoleWrite("X >= 1" & @CRLF)
EndSwitch

But that doesn't seem to fit in with your end goal.

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Unfortunately no, since is not for any of them but depends. If is A is a thing, B another one and C another still. The function i'll do when A = True don't change if also B or C are True or False for this reason i'd like to avoid all that Cases or most of the code is equal.

Global $A = 1, $B = 0, $C = 0

Select
    Case $A = 1 And $B = 0 And $C = 0
        _MyFuncForA(1) ; example
        ; do a thing
    Case $A = 1 And $B = 1 And $C = 0
        _MyFuncForA(1) ; example
        _MyFuncForB(1) ; example
        ; etc
EndSelect

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

So then, what about something short and sweet?

Global $A = 1, $B = 0, $C = 0

If $A Then _MyFuncForA($A)
If $B Then _MyFuncForB($B)
If $C Then _MyFuncForC($C)

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Link to comment
Share on other sites

New idea. Here you can prioritize the most likely events:

Switch $A+$B+$C
  Case 3
    ConsoleWrite("1" & @CRLF)
  Case 2
    Switch True
      Case $A And $B
        ConsoleWrite("2" & @CRLF)
      Case $A And $C
        ConsoleWrite("3" & @CRLF)
      Case $B And $C
        ConsoleWrite("4" & @CRLF)
    EndSwitch
  Case 1
    Switch True
      Case $A
        ConsoleWrite("5" & @CRLF)
      Case $B
        ConsoleWrite("6" & @CRLF)
      Case $C
        ConsoleWrite("7" & @CRLF)
    EndSwitch
  Case 0
    ConsoleWrite("8" & @CRLF)
EndSwitch

 

Link to comment
Share on other sites

Does it have to be boolean?  Would a binary approach help?

Global Enum Step *2 $iA, $iB, $iC
;Global $iA = 1, $iB = 2, $iC = 4

$X = $iA + $iC

Select
    Case $X = 1     ;$iA

    Case $X = 2     ;$iB

    Case $X = 3     ;$iA + $iB

    Case $X = 4     ;$iC

    Case $X = 5     ;$iA + $iC

    Case $X = 6     ;$iB + $iC

    Case $X = 7     ;$iA + $iB + $iC
EndSelect



 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...