Chapter 4.1 - The Sinclair ZX81 as a calculator |
Turn the computer on. You can now use it as a calculator, along the lines of chapter 2: type PRINT, then whatever it is that you want working out, & then NEWLINE. (We shan't usually bother to tell you to type NEWLINE.) |
We formalize this by giving each operation a priority, a number between 1 & 16. The operations with highest priority are evaluated first, & operations with equal priority are evaluated in order from left to right. ** has priority 10 * and / have priority 8 + & - have priority 6 When - is used to negate something, as when you write -1, then it has priority 9. (This is unary minus, as opposed to the binary minus in 3-1: a unary operation has one operand, while a binary operation has two. Note that on the ZX81 you cannot use + as a unary operation.) This order is absolutely rigid, but you can circumvent it by using brackets: anything in brackets is evaluated first & then treated as a single number, so that PRINT 3*2+2 gives the answer 6+2 = 6, but PRINT 3*(2+2) gives the answer 3*4 = 12. A combination like this is called an expression - in this case, an arithmetic or numeric expression because the answer is a number. In general, whenever the computer is expecting a number from you, you can give it an expression instead and it will work out the answer. You can write numbers with decimal points (use the full stop), & you can also use scientific notation - as is quite common on pocket calculators. In this, after an ordinary number (with or without a decimal point), you can write an exponent part consisting of the letter E, then maybe + or -, & then a number without a decimal point. The E here means '*10**' ('times ten to the power of'), so that 2.34E0 = 2.34 * 10**0 = 2.34 2.34E3 = 2.34 * 10**3 = 2340 2.34E-2 = 2.34 * 10**-2 = 0.0234 & so on. (Try printing these out on the ZX81.) The easiest way of thinking of this is to imagine the exponent part shifting the decimal point along to the right (for a positive exponent) or to the left (for a negative exponent). You can also print more than one thing at once, separating them either with commas (,) or semicolons (; or shifted X). If you use a comma, then the next number will be displayed starting either at the left hand margin, or in the middle of the line in the 16th column. If you use a semicolon, then the next number will be displayed immediately following the last one. Try PRINT 1;2;3;4;5;6;7;8;9;10 & PRINT 1,2,3,4,5,6,7,8,9,10 to see the differences. You can mix commas & semicolons within a single PRINT statement if you want. Summary Statements: PRINT, with commas & semicolons Operations: +,-,*,/,** Expressions, scientific notation |