Operators


      Throughout our programs, we're going to be mixing commands + functions, variables, literals and operators. We can think of most operators as symbolic short cuts. So we use these symbols to represent some action, rather than a word. Such actions might be to make assignments, perform math operations (like Addition, Subtraction, Division, Multiplication ) and even compare items together.

      Bellow you'll learn about the various supported operators and hopefully gain some insight into how to use them.



I N D E X:








Assignment Operators


      Assignments are the most common and basic form of operator we'll be using within our programs. They are used to copy the contents of the Right hand side (be it a Value / string (text) or result of some calculation), into the left hand side.


= equals operator. Makes the left hand side (the destination side) a copy of the right hand side.


Samples,
  
  A  = 10  ; Assign the variable A the value of 10
  B  = 20  ; Assign the variable B the value of 20
  A  = B   ; Assign the variable A the value of Variable B
  
  Print A
  Print B
  Sync
  WaitKey
  


     The example above contains three assignment operators. In the first line, the code assigns the integer variable A an integer literal value of 10. So when this line of code is executed, PlayBASIC will copy the value (10 in this case) into the destination varibale of A. The next line is doing much the same thing, except it copies the integer value 20 into a different integer variable called B. The third line copies the contents of the variable B and stores it in variable A. This process overwrites what was previously stored 10 that was in A, with whatever is currently in the B variable.

      If we think back to line two and remember that after the second line of code has been executed by PlayBASIC, B would contain 20. So if we copied the contents of B into A, now A will also contain the value 20.

      When we run this fragment of code, it'll output the following.

  
  20
  20
  



      Note: During assignments between Integer and Floating Point values, PlayBASIC will automatically round for you. You can override this behavior by using either the Floor() or Int() or functions. Floor is the recommended solution.

  
  A=  0.75
  Print A    ;  A will equal 1
  A=  Floor(0.75)
  Print A    ;  A will equal 0
  Sync
  WaitKey
  



Top







Math Operators


      The math operators take the items located directly on the left and right hand side and perform this math operation upon them. The result is then piped back in place of the operator and the two terms, so the more operations you have, the results from each operation are carried forward until the final answer is calculated.

      In PlayBASIC we have following math operators.


+ Addition.
- Subtraction
* Multiplcation.
/ Division.
^ Power Of.


  
  Print 100 + 10
  Print 100 - 10
  Print 100 * 10
  Print 100 / 10
  Print 2 ^ 8
  Sync
  WaitKey
  



      In the above example, the expressions only have a single math operation in them for simplicity, but you can actually use as may operators in a expression as you need. So lets have a quick look at a more complicated sequence of operators. Bellow we've got an expression containing three separate math operations.


  
  Print 100 + 10 + 90 -50
  Sync
  WaitKey
  


      You can probably imagine what happens, but we'll go through just in case. PlayBASIC solves all expressions scanning from the LEFT to RIGHT, so in the case of expression after the PRINT statement, it'll first see the 100+10 addition math operator and solve it. So conceptually what's happening, is it's adding the 100+10 together and substituting the result (the 110 value) back into the expression in place of the operator. So after solving the first addition we end up with something like this Print 110+90-50. Now since the expression still has more math operators in it, the compiler repeats the process until they've all been solved. So next search it'd find the addition operator between previous result (the 110) and the 90, so the result of that operation is substituted back (print 200-50) and the process repeats again, finally ending up with Print 150. At this point all operators have been solved so outputs code to perform for the Print operation with it's result.





Order Of Math Operations / Precedence


      Compilers use a combined order of operations. Your probably familiar with these already, generally (Here in Australia) BODMAS is typically the way we're taught to evaluate equations. I was taught that BODMAS stood for.


B = BRACKETS
O = OPERATIONS
D = DIVISION
M = MULTIPLICATION
A = ADDITION
S = SUBTRACTION


To brush up on it, visit here. http://www.abacustraining.biz/bodmasExercises.htm


      Programming languages tend to evaluate in this order though (which is what PlayBASIC uses).

B = BRACKETS
DM = DIVISION and MULTIPLICATION
AS = ADDITION and SUBTRACTION
O = OPERATIONS


      Now it's important to note that DIVISIONS and MULTIPLICATION are of equal precedence, as are ADDITION and SUBTRACTION. That is to say, the DIVISIONS and ADDITIONS are NOT done before MULTIPLY or SUBTRACTION. They are evaluated equally LEFT to RIGHT.

      I.e Using these rules we should get

10/2*5= 5*5 = 25
5*10/2= 50/2 = 25
100+200-100 = 300-100 = 200
10-1+5 = 9+5 = 14
100/2+25 = 50+25 = 75
(100+10/2)*2 = (100+5)*2 = 105*2 = 210


      Or you can try in PlayBASIC yourself.

  
  Print 10/2*5
  Print 5*10/2
  Print 100+200-100
  Print 10-1+5
  Print 100/2+25
  Print (100+10/2)*2
  
  Sync
  WaitKey
  



Top







Math Short Cut Operators


      These operators allow us to shorten some common long math expressions. They can be used on most data types and structures within PlayBASIC. Including Integers, Floats, Strings, Arrays, Pointer writes (limited support) and even User Defined Types.


++ ; Increase by one. This is the equivalent of INC operator. Except you can use these on arrays/ type fields

-- ; decrease by one. This is the equivalent of DEC operator. Except you can use these on arrays/ type fields

+= ; Add right value to left. Eg Score += 100 , is the same as Score = Score +100

-= ; Subtract right value from left. Eg Score -= 100 , is the same as Score = Score -100

*= ; Multiply right value by left. Eg Score *= 100 , is the same as Score = Score *100

/= ; Divide right value by left. Eg Score /= 100 , is the same as Score = Score /100



Samples,
  
  A++     ;  Increase the A Variable by one   Same as A=A+1 , or  Inc A
  A--     ;  Decrease the A Variable by one   Same as A=A-1,  or Dec A
  A+=10   ;  Increase the A Variable by 10    Same as A=A+10 , or  Inc A,10
  A-=10   ;  Decrease the A Variable by 10    Same as A=A-10 , or  Dec A,10
  A*=10   ;  Multiply the A Variable by 10    Same as A=A*10
  A/=10   ;  Divide the A Variable by 10    Same as A=A/10
  


Note: These operators can only be used within an assignment, they're currently not supported with an expression (within a calculation)


Top







Comparison Operators


     The following list of the operators are used when we wish to compare two items together. These are often used in conjunction with the IF/THEN and IF/ENDIF comparison statements. You can also used them in conditional loops, such as Repeat/Until, While/EndWhile.


= equals.

<> not equal too
>< not equal too (inverted)
!= not equal too (C styled operator)

< less than
> greater than

<= less than or equal too
=< less than or equal too

=> greater than or equal too
>= greater than or equal too


Note: PlayBASIC accepts both forms of not equal too, less than or equal and greater than or equal symbols in user programs. You can also use the C sytled not equal operator also.

  
  If Variable1  =  Variable2 Then Print "These variables are equal!"
  If Variable1  <>  Variable2 Then Print "These variables are not equal!"
  If Variable1  >  Variable2 Then Print "Variable 1 is larger than variable 2"
  If Variable1  <  Variable2 Then Print "Variable 1 is smaller than variable 2"
  If Variable1  >=  Variable2 Then Print "Variable 1 is larger than or equal to variable 2"
  If Variable1  <=  Variable2 Then Print "Variable 1 is lower than or equal to variable 2"
  



      So how do comparisons really work ? - Well, when we use a compare operator in PlayBASIC, what happens is PlayBASIC runs the comparison operation on the terms, which will be the items on the left and right hand sides of the operator. The operation then returns a numeric value representing the equally of these terms for this operator, where 1 ='s TRUE and 0 equals FALSE.

      For example, if we have an expression with the following comparison 50>10, then when PB executes the Greater than operation, it's comparing if 50 is larger than 10. Since it is, PB will return a value of 1 (TRUE) back into the expression, so comparisons are in fact another form math operation.

  
  Print  50>10
  Print  50<10
  Print  50>=10
  Print  50<=10
  Print  50=10
  Print  50<>10
  Sync
  WaitKey
  


      If we run the above code in PlayBASIC, we get the following output on screen.

  
  1
  0
  1
  0
  0
  1
  





      Experienced programmers will often use comparison within expressions to avoid the need for a decision statements like IF/Then blocks for example. One such situation that comes to mind would setting a Flag variable based upon a certain criteria.

      For example something like this,

  
  Flag = false
  If Something=SomeOtherThing
     Flag=true
  EndIf
  


      Can be expressed simply as,

  
  Flag = Something=SomeOtherThing
  





Top






Logical Operators



& bit wise AND
| bit wise OR
~ bit wise XOR

AND logical conjunction
OR logical disjunction
XOR logical disjunction
NOT logical negation


     These are most commonly used within If comparison expressions. Where we might want your program to take a certain action, when a combination of comparisons are True or False.

      Examples.

  
; define some variables and assign them some values
  A=40
  B=60
  C=100
  
; compare A and B for matches
  If A=40 And B=60
     Print "Both Conditions are true"
  EndIf
  
; compare A and B for explicit matches
; This will fail as the A compare is FALSE
; while the B side is TRUE.
; so when
  If A=400 And B=60
     Print "Both Conditions are true"
  EndIf
  
  
; compare A and B, OR the states so only one
; of the compares needs to be true for the
; if statement to be run.
  If A=40 Or B=12345
     Print "At least one comparison was true"
  EndIf
  
; Here we're ANDing the results of the A and B
; compares together,  then ORing the result
; of the C compare.  So the only way for
; the If statement to be true is that C=100's
; or both A=40 and B=60
  
  If (A=40 And B=60Or (C=100)
     Print "At least one comparison was true"
  EndIf
  
  Sync
  WaitKey
  



Top







Logical Bit Wise Short Cut Operators


These are some short cuts for bitwise operators. So you shorten these types of expressions.


&= bit wise AND left side with the right side value
|= bit wise OR left side with the right side value
~= bit wise XOR left side with the right side value



Samples,
  
  A&= 255   ;   Same as A=A & 255
  A|= 255   ;   Same as A=A | 255
  A~= 255   ;   Same as A=A ~ 255
  


Top







Bit Shift Operators


      C styled Bit shift operators. These operators are not to be confused as comparison operators and they're really just short cuts for the LSR32 and LSL32 functions. While the << and >> operators are functionality the same, they're just easier for the programmer to write than function call.



<< is a bitwise function that performs a left bit shift upon a 32 bit value
>> is a bitwise function that performs a right bit shift upon a 32 bit value



Samples,
  
  For lp=0 To 31
     
     // left shift 1 by 0 to 31 bits
     s$ =Bin$(1 << lp)
     s$+="   "
     s$+=Bin$($80000000 >> lp)
     
     Print s$
     
  Next
  Print ""
  
  Sync
  WaitKey
  


     The results of the previous code snippet.
  
  %00000000000000000000000000000001   %10000000000000000000000000000000
  %00000000000000000000000000000010   %01000000000000000000000000000000
  %00000000000000000000000000000100   %00100000000000000000000000000000
  %00000000000000000000000000001000   %00010000000000000000000000000000
  %00000000000000000000000000010000   %00001000000000000000000000000000
  %00000000000000000000000000100000   %00000100000000000000000000000000
  %00000000000000000000000001000000   %00000010000000000000000000000000
  %00000000000000000000000010000000   %00000001000000000000000000000000
  %00000000000000000000000100000000   %00000000100000000000000000000000
  %00000000000000000000001000000000   %00000000010000000000000000000000
  %00000000000000000000010000000000   %00000000001000000000000000000000
  %00000000000000000000100000000000   %00000000000100000000000000000000
  %00000000000000000001000000000000   %00000000000010000000000000000000
  %00000000000000000010000000000000   %00000000000001000000000000000000
  %00000000000000000100000000000000   %00000000000000100000000000000000
  %00000000000000001000000000000000   %00000000000000010000000000000000
  %00000000000000010000000000000000   %00000000000000001000000000000000
  %00000000000000100000000000000000   %00000000000000000100000000000000
  %00000000000001000000000000000000   %00000000000000000010000000000000
  %00000000000010000000000000000000   %00000000000000000001000000000000
  %00000000000100000000000000000000   %00000000000000000000100000000000
  %00000000001000000000000000000000   %00000000000000000000010000000000
  %00000000010000000000000000000000   %00000000000000000000001000000000
  %00000000100000000000000000000000   %00000000000000000000000100000000
  %00000001000000000000000000000000   %00000000000000000000000010000000
  %00000010000000000000000000000000   %00000000000000000000000001000000
  %00000100000000000000000000000000   %00000000000000000000000000100000
  %00001000000000000000000000000000   %00000000000000000000000000010000
  %00010000000000000000000000000000   %00000000000000000000000000001000
  %00100000000000000000000000000000   %00000000000000000000000000000100
  %01000000000000000000000000000000   %00000000000000000000000000000010
  %10000000000000000000000000000000   %00000000000000000000000000000001
  


Top




 
Related Info: ArrayBasics | Comparisons | Dec | Functions&Psub | If | Inc | Literals | Loops | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com