Math and Logic Operations
The assignment operator (the equals sign "=") assigns a value to the variable found on the left hand side of the equals sign based on the value of what is calculated on the right hand side of the equals sign.
Example:
X = 4
Y = 5
Z = X + Y
Z = Z * 2FirstName = "Bart"
X stores the number 4
Y stores the number 5
Z stores the number 9 (= 4 + 5)
Z stores the number 18 (=9 * 2)FirstName stores the string "Bart"
The = (equals sign) here refers
to assignment, not "equals" in the mathematical sense. So if x is 10 and y is
15, x = x + y is NOT a valid mathematical expression, but this
statement works in QBasic. It makes x the value of what is already found in x plus what is
found in y (in this case x is now equal to 25).
We can do any math operations with numbers (integers or decimal numbers) using the equals sign but math CAN NOT be done on STRING variables. Remember that in QBasic, math operation follow the BEDMAS rules.
The Math Operators:
Symbol
Meaning
+
plus
-
minus
*
times (multiplication)
/
divide (division)
^
power of (exponent)
(
opening bracket
)
closing bracket
Here are some useful examples:
REM this will calculate the percent ( where mark and total are variables that store numbers)
mark = 45
total = 70
grade= mark / total * 100
PRINT "The mark "; mark; " out of "; total; " is "; grade; " per cent"REM this will calculate the average
age1 = 5
age2 = 15
age3 = 45
averageAge = (age1 + age2 + age3 ) / 3
PRINT "The average age is "; averageAge ; " years old"