35
Type:
10 PRINT 2^10
When you use all the operations together, it can get a bit complicated. After a winning
baseball game, your 7-person team is awarded 3 cans of pop for the game plus another
11 cans because you finished first in the league too. How many cans does each player
get?
Type this:
10 PRINT 3+11/7
Is it really evaluated from left to right? Yes, but was it the answer you were expecting?
If the answer came out to 4.57143, the computer first divided 11 by 7 getting 1.57143 and
then it added 3 to the result giving 4.57143. You probably were expecting the answer to
be 2 because 3+11 is 14, then divided by 7 is 2.
You can see that expressions are not simply evaluated from left to right. It is necessary
to have an exact set of rules to follow.
Here they are:
Functions
Order in which they are
performed
()
things is parentheses(brackets)
1st
^
raising to a power
2nd
* /
multiplication and division
3rd
+ -
addition and subtraction
4th
The computer will always look at an expression in parentheses first and do the things you
put there. It could be an expression or more sets of brackets. Among the expression,
raising to a power is done first, followed by multiplication and division from left to right,
followed by addition and subtraction from left to right. If you’re not sure how the computer
will evaluate the expression, simply put brackets around the expression you want done first.
Remember the brackets must always be in pairs, one right bracket for each left one. If
you don’t, then you will get a "
? SYNTAX ERROR
" message.
Summary of Contents for PreComputer Prestige
Page 1: ...COURSE BOOK...