Computers are, at heart, very fancy calculators. In this lesson you'll use steav2 to do maths, work with text, and meet the two simplest answers of all: yes and no.
Doing maths
jongyeytha can print the result of a calculation, not just text:
bongSlanhOun "maths";
jongyeytha 2 + 3;
jongyeytha 10 - 4;
jongyeytha 6 * 7;
jongyeytha 7 / 2; 5
6
42
3.5 The four basic operators (symbols that do something to values) are:
| symbol | meaning |
|---|---|
+ |
add |
- |
subtract |
* |
multiply (there's no × key, so we use a star) |
/ |
divide |
Notice there are no quotes around the numbers. 2 + 3 is a calculation, and steav2 works out the answer before printing it.
Order of operations
Just like in school maths, multiplication and division happen before addition and subtraction. Use round brackets ( ) to choose the order yourself:
bongSlanhOun "order";
jongyeytha 2 + 3 * 4;
jongyeytha (2 + 3) * 4;
jongyeytha -5 + 1; 14
20
-4 A tiny surprise
Try this one:
bongSlanhOun "surprise";
jongyeytha 0.1 + 0.2; 0.30000000000000004 That's not a steav2 bug! Computers store decimal numbers in binary, and some decimals (like 0.1) can't be stored perfectly, so a tiny rounding error creeps in. Almost every programming language does the same. It's a fun fact to know, and it rarely matters.
Text
A piece of text is called a string (think of a string of letters, like beads on a necklace). In steav2, the type of text is called Ahsor, which means "letters". You write a string inside double quotes:
bongSlanhOun "text";
jongyeytha "I am a string";
jongyeytha "123";
jongyeytha 123; I am a string
123
123 The last two print the same thing, but they're different: "123" is text that happens to contain digits, while 123 is a real number you can do maths with.
Joining text together
The + sign also joins strings, one after the other:
bongSlanhOun "join";
jongyeytha "sour" + "dough";
jongyeytha "Hello, " + "world" + "!"; sourdough
Hello, world! Notice the space inside "Hello, ". steav2 never adds spaces for you, so if you want one, put it in the quotes.
Here's a very useful trick: if one side of a + is text, the other side is turned into text automatically. That means you can mix words and numbers:
bongSlanhOun "mix";
jongyeytha "I have " + 3 + " cats";
jongyeytha "Two times four is " + 2 * 4; I have 3 cats
Two times four is 8 Careful with this one, though. steav2 reads + from left to right, and once it has text, every + after it is joining, not adding:
bongSlanhOun "careful";
jongyeytha "total: " + 1 + 2;
jongyeytha "total: " + (1 + 2);
jongyeytha 1 + 2 + " is the total"; total: 12
total: 3
3 is the total In the first line, "total: " + 1 becomes the text "total: 1", and then + 2 sticks a 2 on the end. Brackets fix it, because the maths inside them happens first.
Yes and no
The last kind of value for today is the simplest: a value that is either true or false. These are called Booleans (after a mathematician called George Boole), and steav2 calls the type Boolean.
okmeans true (yes)ortmeans false (no)
You get Booleans when you ask steav2 a question, like "is 5 bigger than 3?":
bongSlanhOun "questions";
jongyeytha 5 > 3;
jongyeytha 2 > 10;
jongyeytha 4 == 4;
jongyeytha "is 1 less than 2? " + (1 < 2); ok
ort
ok
is 1 less than 2? ok Notice that "is equal to" is written with two equals signs, ==. We'll use these questions a lot when your programs start making decisions, a couple of lessons from now.
Your turn
- Work out how many minutes there are in a week (60 minutes, 24 hours, 7 days) and print it. Hint: multiply three numbers together.
- Print the sentence
My age in 10 years:followed by your age plus ten, as a proper sum. Hint: remember the brackets trick from "Joining text together". - Print whether
100 / 4is equal to25.
Solution for exercise 2:
bongSlanhOun "age";
jongyeytha "My age in 10 years: " + (12 + 10); My age in 10 years: 22