Getting Started
Sample Programs
Code Snippets

Web Sites
More Tutorials
User Groups
Talk Shows

Progress Bar

GBIC >> PowerBASIC >> Tutorials >> Operators

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • Operators
    PowerBASIC supports a pretty much standard set of operators. Some languages have operators specific to string or number types, but PowerBASIC operators work on both types. Here's a compact listing of the operators PowerBASIC supports. This tutorial breaks these into common groups for discussion.

    Note that operators can be made of letters (such as MOD) and do not always consist of special characters.

         +       +=       <       AND     AND=
         -       -=       >       OR      OR=
         *       *=       <>      NOT     NOT=
         /       /=       ><      XOR     XOR=
         \       \=       <=      EQV     EQV=
         MOD     MOD=     =<      IMP     IMP=
         ^                >=
         ISTRUE           =>

    Basic Arithmetic
    The following, industry-standard operators are used to perform basic arithmetic operations.

        =     x = 2       # assignment
        +     x = x + 1   # add
        -     x = x - 1   # subtract
        *     x = x * 2   # multiply
        /     x = x / 2   # divide
        \     x = x \ 2   # integer division
        ^     x = x^2     # raises x to the power of 2
        MOD   x MOD 5     # modulus (whole number remainder of division)

    MOD is an example of an operator that is defined using letters rather than special characters.

    Relational Operators
    These operators compare two values or expressions. They return a Boolean result of TRUE (-1) or FALSE (0). Since they return a numeric result, they can be use in numerical expressions to return a -1 or 0. However, relational operators are most notably used in flow control structures, such as IF, DO, WHILE, etc., which use the TRUE/FALSE value of an expression to determine which source code statements to execute.

        <                 # less than
        >                 # greater than
        <>  or  ><        # not equal to
        <=  or  =<        # less than or equal to
        >=  or  =>        # great than or equal to

    When used with strings, the ASCII values of each character in the string form the basis for the comparison. For example, "a" is less than "A" because their respective ASCII codes are 39 and 65.

    String Concatenation
    The PowerBASIC uses two operators (+ and &) to join strings.

         b$ = "dog"      'assign "dog" to string variable a$
         c$ = "cat"      'assign "cat" to string variable c$
         a$ = b$ + c$    'a$ = "catdog"
         a$ = b$ & c$    'a$ = "catdog"

    This is a comparative slow process and programmers are advised to use the special BUILD$ function which concatenates strings with much greater speed, particularly with larger numbers of strings to concatenate.

    Boolean Values and PowerBASIC Functions
    Before getting into PowerBASIC Boolean operators, here are some points to remember when dealing with Boolean values and PowerBASIC functions.

    • In PowerBASIC, numeric zero or an empty string are treated as FALSE. All other values are considered TRUE.

    • PowerBASIC functions which return a Boolean result return -1 to indicate TRUE and 0 to indicate FALSE.

    • Some PowerBASIC operators evaluate expressions in terms of their TRUE/FALSE value and some act on the numeric/string value of the expression.

    Boolean Operators
    These PowerBASIC operators can be used in two ways - to compare the TRUE/FALSE value of two expressions, or to manipulate the content of a variable at the bit level. All but the NOT operator work on two expressions.

        and    # TRUE if both expressions TRUE
        or     # TRUE if either expression TRUE
        xor    # TRUE if arguments have different Boolean values
        not    # returns bitwise one's-complement of a single argument
        eqv    # TRUE if arguments have same Boolean values
        imp    # FALSE only if 1st argument is T and 2nd argument is F

    From a Boolean viewpoint, when dealing with values 0 and -1, NOT works as expected. But when NOT is used on other values, it can provide unexpected results. This is because NOT works at the bit level, not on a Boolean level. In cases where the expression to be evaluated is other than 0 or -1, the ISFALSE and ISTRUE operators should be used to deal with values on a Boolean level.

    Bit Manipulations by Boolean Operators
    All of the Boolean operators above operate at the bit level. The section above shows how the operators can successfully be used to perform Boolean tests. Here is a description of the bit-level effect of the operators. You can see how the AND/OR and NOT/XOR pairs provide effects opposite to one another.

        AND   sets selected bits to 0 without affecting the other bits
        OR    sets selected bits to 1 without affecting the other bits
        NOT   reverses all bits
        XOR   reverses selected bits

    The EQV and IMP also perform bit-level manipulation but the results are seldom used for that purpose. The Boolean descriptions of the prior section describe the effects most useful to programmers.

    Compound Operators
    To simplify coding, PowerBASIC also offer the ability to combine the following operator pairs for common operations. The operator, its use, and the equivalent expressions are given in the following table.

         +=     x+=5        x = x+5
         -=     x-=5        x = x-5
         *=     x*=5        x = x*5
         /=     x/=5        x = x/5
         \=     x/=5        x = x\5
         MOD=   x%=5        x = x%5
         AND=   x AND= 6    x = x AND 6
         OR=    x OR= 5     x = x OR 5
         XOR=   x XOR= 3    x = x XOR 3
         EVQ=   x EQV= 6    x = x EQV 6
         IMP=   x IMP= 2    x = x IMP 2
         +=     x += "dog"  x = x + "dog"
         &=     x &= "dog"  x = x & "dog"

    All of these compound operators are described as replacing a variable with its value modified (according to the operator type) by a second value.

    Quote Operators
    Other languages, such as Perl, provide the ability to insert variables into quotes and at execution, substitute the variable value for the variable name. The method is a shortcut for concatenating strings and variable values.

    This is called interpolation and is not supported by PowerBASIC.

    If you have any suggestions or corrections, please let me know.