Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using _Andalso with Select Case
#1
I have used the logical operator _Andalso in IF...Then statements however I'm not sure if this logical operator works in Select Case. 
Is this because Select Case: Case is .. will only accept =,<>,<,>
the logical operators like And, Or, Xor, _Andalso do not work with Case is..?
Reply
#2
select case... end select wasn't designed for the application you described.  what is the fear of using ordinary if... then... else... end if block?

select case was created in a time.  where it looked like basic would never offer short-circuit evaluation.  while it's offered in qb64pe.  it has to be only in if... then... else... end if block.  otherwise it would be confusing.  i don't think other programming languages are different about it.  although i've seen c code that is simply mind-boggling.

could do something like this:

Code: (Select All)
a = 4
input b
if a = 4 _andalso b = 5 then
    'do this
else
    do
        select case a
            case 5, 10, 15
                print "It's divisible by five!"
                exit do
        end select
        select everycase b
            case 1
                print "The other value is too low."
            case 5
                print "b was expected to be this value."
            case 1 to 5
                print "It was part of the stupid test!"
        end select
    loop until 1
end if
put select case... end select inside an if... then... else... end if block.  use the compound operators in the "if" statement.  note how the do... loop is being used, it executes only once.  this is only to be able to use exit do to ignore other statements after it.  in case the programmer doesn't want them executed for a specific case.  this could be looked up in the qb64 wiki.
Reply
#3
Never used  _ANDALSO but I find it useful when I must test 2 conditions and the second can be false and it triggers a runtime error like for example out of range, so before the existence of _AndAlso I must write a first IF for the field of values good so that the second condition in the second IF doesn't trigger a runtime error.
Code: (Select All)
Dim a(1 To 7)
For c = 0 To 100
    If c = 0 _AndAlso c Mod 24 = 0 Then Locate 1, 1 ' this will be executed one time
    If c Mod 25 = 0 Then Sleep 1
    Print c
    row = CsrLin: column = Pos(0)
    Locate 25, 1: Print " This doesn't disappear!";
    Locate row, column
    If c > 0 _AndAlso c < 8 Then a(c) = row ' this will be executed seven times
Next
Sleep 3
Cls
For c = 0 To 100
    Select Case c
        Case 0
            Locate 1, 1
        Case 1 To 7
            a(c) = row
        Case 25, 50, 75, 100
            Sleep 1
    End Select
    Print c
    row = CsrLin: column = Pos(0)
    Locate 25, 1: Print " This doesn't disappear!";
    Locate row, column
Next c
Sleep
System
But as Wiki explains it is useful for avoiding so many actions required in the following AND conditions when the first is false and the final result of conditions is false.

Select case let you set the values or the range of values to perform some actions. In this kind of decisions about one variable:  the values that activate the same action have been typed in the same CASE as a range or a list of values/conditions Select Case wiki page so you can put the _andalso conditions into the same CASE block without using _andalso keyword.
Reply
#4
Thanks very much for your suggestions on a workaround for        Case is > A AndAlso A < C .

I find I prefer a Select Case than an IF..THEN decision structure. I was also thinking I may be able to create a workaround if there was a Select Case True, but doesn't appear that that is in the Select Case arsenals. 

The ability to short circuit a decision with multiple conditions has been a great addition to the Logical Operators and I use it a lot with IF...THEN. It just occurred to me that I haven't tried more than one other condition, I wonder how _Andalso works with many multiple conditions? For example:

If A>B _Andalso B>C _Andalso C>D _Andalso D=>A THEN Print "This will never work"

Thanks again for your suggestions and comments.
Reply




Users browsing this thread: 2 Guest(s)