Boolean variables and statements

Discussion in 'Teaching Programming with B4X' started by HotShoe, Sep 1, 2015.

  1. HotShoe

    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:

    Code:
    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.

    Code:
    Sub CheckCust as boolean

        
    return custnum > 10

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

    Code:
    If checkcust then
    ... execution code here
    end if
    Notice that no where does the code above directly check the VALUE of checkcust like:
    Code:
    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.

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

    Code:
    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:

    Code:
    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: Sep 2, 2015
    wonder, Peter Simpson, BPak and 9 others like this.
  2. Brian Robinson

    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:
    Code:
    If myBool = True then
      myBool = 
    False
    else
      myBool = 
    True
    end if
    You can write it more efficiently as:
    Code:
    myBool = not(myBool)
    or
    Code:
    myBool = myBool <> True
    which may be faster i believe, as not is i think a function call not an operator???

    Cheers
    Brian
     
    Last edited: Feb 27, 2016
    Peter Simpson, wonder and HotShoe like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice