Question Bit of Logic

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Hello.
I have to execute some operations according to some (4) booleans.
Let's suppose the normal estate of the booleans is False, and that only one of the four booleans can be True at one time (which is not necesarily true, but the program cannot go ahead with two True values).

Should I use nested Select Case or nested If-Else-If? I feel a bit clumsy with the nested Select Case but I suppose it should be sth similar to this:

VB.NET:
Select Case Boolean1
  Case True
  Case Else
     Select Case Boolean2
         Case True
         Case Else
            Select Case Boolean3
                Case True
                Case Else
                    Select Case Boolean4
                       Case True
                       Case Else
                    End Select
             End Select
      End Select
End Select

Thank you for your suggestions.
In the case I had two True and had to Exit I suppose I could code sth similar to the following:

VB.NET:
[COLOR=#0000ff]If[/COLOR] Boolean1 [COLOR=#0000ff]and[/COLOR] Boolean2 [COLOR=#0000ff]or[/COLOR] Boolean1 [COLOR=#0000ff]and[/COLOR] Boolean3 [COLOR=#0000ff]or[/COLOR] Boolean1 [COLOR=#0000ff]and[/COLOR] Boolean4 [COLOR=#0000ff]or[/COLOR] Boolean2 [COLOR=#0000ff]and[/COLOR] Boolean3 [COLOR=#0000ff]or[/COLOR] Boolean2 [COLOR=#0000ff]and[/COLOR] Boolean4 [COLOR=#0000ff]or[/COLOR] Boolean3 [COLOR=#0000ff]and[/COLOR] Boolean4 [COLOR=#0000ff]Then[/COLOR] [COLOR=#40e0d0]Application[/COLOR].Exit()
 
You certainly wouldn't use Select Case when testing a Boolean value. You'd always use If...Else in preference to Select Case for that.

That said, there's a clever way to test all four Booleans at the same time in a situation like yours, e.g.
Dim flags = Convert.ToInt32(bool1) * 1 +
            Convert.ToInt32(bool2) * 2 +
            Convert.ToInt32(bool3) * 4 +
            Convert.ToInt32(bool4) * 8

Select Case flags
    Case 0
        'None of the flags are set.
    Case 1
        'Only the first flag is set.
    Case 2
        'Only the second flag is set.
    Case 4
        'Only the third flag is set.
    Case 8
        'Only the fourth flag is set.
    Case Else
        'Two or more flags are set.
End Select
This makes use of the fact that you can treat a 32-bit number as a group of 32 Boolean values, where each bit in the binary form is 1 for True and 0 for False. In the above example, if `bool2` and `bool4` are both True then `flags` will end up being 10 and that is the only combination that will produce that result. This means that each possible value of `flags` represents no more than one possible combination of Boolean values, thus you can use a single Select Case to test for all possibilities.
 
By the way, while the result would be the same, it would actually be more correct to use `Or` operators rather than `+` in my previous code snippet, given that it is performing a bitwise operation and not a mathematical one.
 
In this case, I think you are adding numbers, not conditions. So let it as follows:
VB.NET:
Dim flags = Convert.ToInt32(Pilar1) * 1 + Convert.ToInt32(Pilar2) * 2 + Convert.ToInt32(Pilar3) * 4 + Convert.ToInt32(Pilar4) * 8

Thank you for your very useful hint.
 
In this case, I think you are adding numbers, not conditions. So let it as follows:
VB.NET:
Dim flags = Convert.ToInt32(Pilar1) * 1 + Convert.ToInt32(Pilar2) * 2 + Convert.ToInt32(Pilar3) * 4 + Convert.ToInt32(Pilar4) * 8

Thank you for your very useful hint.

I should have spent a bit more time thinking about this. The "proper" way to do this would be to use the left bit shift operator `<<` instead of multipleication and the bitwise `Or` operator instead of addition. Again, the result would be the same but, logically, mathematical operators should not be used because this is not a mathematical operation but rather a logical one:
Dim flags = Convert.ToInt32(bool1) << 0 Or
            Convert.ToInt32(bool2) << 1 Or
            Convert.ToInt32(bool3) << 2 Or
            Convert.ToInt32(bool4) << 3
 
Select Case flags
    Case 0
        'None of the flags are set.
    Case 1
        'Only the first flag is set.
    Case 2
        'Only the second flag is set.
    Case 4
        'Only the third flag is set.
    Case 8
        'Only the fourth flag is set.
    Case Else
        'Two or more flags are set.
End Select
Again, the result will be the same but those operators more accurately represent what you're actually doing.
 
Back
Top