Beginner scripting tutorial #3 - Arithmetics

Arithmetics

Previous tutorial - Variables

Questions we will be answering:

  • What is it?
  • Why is it useful?
  • How do I use it?

Expected knowledge:


What is arithmetics?

Arithmetics, for what we will be looking at here at least, are the main “operators” in maths - addition, subraction, multiplication and division. We’ll look at how to perform these actions in our scripts with numbers and variables.

Why is arithmetic useful?

It’s usefull for, well, really anything that involves maths. Whether that’s a simple number + number or something more complex, arithmetics will be used. It’s another thing essential to know for programming, and luckily isn’t too difficult to understand.

How do I use arithmetics?

The way arithmetics is used in programming is the same way as you would use it in maths - write out the first number, then what operator you are using (+, -, *, /) and the second number. For example, if I wanted to add 5 and 3 together I would write out:

5 + 3

That line of code would be the same as just writing out 8, because 5 + 3 = 8. Considering that the code 5 + 3 is equal to the code 8, we can do things like printing out the value or storing the value in a variable:

print(5 + 3)

local sum = 5 + 3

What are the operators?

+ = Add
- = Subtract
* = Multiply
/ = Divide

Shortcuts

This isn’t absolutely necessary to know, though it is useful:

If you want to do something to an already existing number, i.e. add 3, you would want to do:

local number = 5
number = number + 3

Which would mean the variable number = the value of number plus 3 (equals 8). However, we can shorten this to just:

local number = 5
number += 3

It does the exact same thing, just a little shorter. This also works for any other operator.

Now that you know what arithmetics is, test your knowledge on these questions:

Question 1 (Easy)

How would I subtract 6 from 19 and print out the answer?

Hint: Put the operator in between the two numbers
Answer:

print(19 - 6)
Question 2 (Medium)

What is the shortest way of adding 4 to a variable with a value of 9?

Hint: Make sure you’ve read the “Shortcuts” part of this tutorial.
Answer:

local number = 9
number += 4
Question 3 (Hard)

How do I check if two numbers added together create 9, and if they do print: “number1 + number2 = 9!”

Hint:

Here, we have to use something called “if statements” - our next topic. The way they’re used is by saying:

if (something) then
    code here
end

Answer:

local number1 = 5
local number2 = 4

if number1 + number2 == 9 then
    print(number1.. " + " ..number2.. " = 9")
end

P.S. I’m going to try to upload these tutorials more!

11 Likes

Long time since the 2nd tutorial, but I’ll upload these more frequently until I’ve covered the basics

2 Likes

Can you combine them all into 1 post?

2 Likes

Just made it - post

1 Like

Noooooo, I meant stack them all up. (From first to last) And when you make an update for the post, comment that you made an update (It will show up again in the most recent resources)

What do you mean? Isn’t that what the post is?

No, I meant copy and paste every post you made about this tutorial and merge them into 1 very big post.
(From order)

Question 3- you shouldn’t ask people which they hadn’t learned yet that’s basically saying “what is pi” to 5 year olds.

1 Like

(Sorry for late reply, but:) that’s just how I’m formatting these tutorials; the rest of the tutorial is all about the one topic, then the 3rd question incorporates something from the next topic, to give people a bit of an idea on what’s to come. It’s only about 2 sentences within the whole topic, so it probably doesn’t make much of a difference