Boolean variables and statements

HotShoe

Well-Known Member
Licensed User
A boolean variable can be True or False, and so a boolean statement must resolve to a value of True or False. There are several ways to do both that you may see when reading other peoples source code, so lets go through a few variations of how to use booleans in an efficient manner.

An If statement ALWAYS resolves to a boolean value or it raises and error, but If statements can look differently depending on how they are written. Consider this line:

B4X:
If CustNum > 10 Then
... execution code here
End If
That is a straight forward If statement. If custnum is greater that 10 the statement is true and the execution code is run, otherwise the rest of the statement is skipped until an Else or End If is encountered.

Now lets look at other ways to test for the condition of custnum being greater than 10 that may be done from another sub routine, or another module or activity.

B4X:
Sub CheckCust as boolean

    return custnum > 10

end Sub
Then we can write our If statement like this:

B4X:
If checkcust then
... execution code here
end if
Notice that no where does the code above directly check the VALUE of checkcust like:
B4X:
if checkcust = true then
...

That's because it is not needed. This is a short circuit comparison and it applies to all booleans. You do not need to specify the value that you want to see, you can simply let the underlying language logic do it for you.

B4X:
If Not(CheckCust) Then
Will be True if CheckCust is False, while:

B4X:
If CheckCust Then
Will only be true if CheckCust is True. In the CheckCust subroutine above, the boolean value is determined if custnum is greater than 10 like this:

B4X:
return custnum > 10
That is a boolean evaluation, it is asking b4a, b4j, b4i, or any other language to return a boolean (True or False) value for the statement:

custnum > 10

If it is true, the sub returns True, if it is not then the sub returns False. You can reverse the result of any boolean with the Not() keyword, and sometime this is not only handy, but necessary.

This lesson is only to show you different ways of writing the same boolean statement and is not meant to be a better or worse way to handle these. The best way to write code is the way that is most comfortable for you.

--- Jem
 
Last edited:

Brian Robinson

Active Member
Licensed User
Another interesting one for boolean is when you need to swap from true to false, if it is not depending on other conditions.

A good example is a check box. You click on it and it goes from true to false (or on to off), or similarly every time you enter a function you may need to turn it on or off, depending on what it was last time.

To swap you can write the length fashion:
B4X:
If myBool = True then
  myBool = False
else
  myBool = True
end if
You can write it more efficiently as:
B4X:
myBool = not(myBool)
or
B4X:
 myBool = myBool <> True
which may be faster i believe, as not is i think a function call not an operator???

Cheers
Brian
 
Last edited:
Top