|
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.
Samples,
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.
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.
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.
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.
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.
Programming languages tend to evaluate in this order though (which is what PlayBASIC uses).
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
Or you can try in PlayBASIC yourself.
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.
Samples,
Note: These operators can only be used within an assignment, they're currently not supported with an expression (within a calculation) 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.
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.
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.
If we run the above code in PlayBASIC, we get the following output on screen.
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,
Can be expressed simply as,
Logical Operators
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.
Logical Bit Wise Short Cut Operators These are some short cuts for bitwise operators. So you shorten these types of expressions.
Samples,
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.
Samples,
The results of the previous code snippet.
|
Related Info: | ArrayBasics | Comparisons | Dec | Functions&Psub | If | Inc | Literals | Loops | Variables : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |