In the previous chapter, we initialised our variable with our own value. What if we want the user to input her own value? What if we want to do something with it?
Inputs and outputsEdit
There are numerous ways to receive output. The most common one is displaying it on the screen. Other ways include using the speakers, the printer, etc.
In our pseudocode, we simply prompt the user to input the age, get the age and display our message. This is very simple and our input/output method is not stated explicitly.
In our Pascal code, we first declared 'age', an integer. Then we output the text 'How old are you?' onto the console. This is our prompt. Next, we get the user's input with the readln function, and place the value inside our 'age' variable. Finally, we output the result. The IntToStr function simply converts age from an integer into a string.
(Note: The plus signs on the output does not indicate addition but text concatenation. We will discuss this in our later operators section.)
x ← 60 x := 60 x = 60 Put 60 into x
x := 60
x = 60
We can also use assign a variable to another variable. In this example, y will become 60:
x ← 60 y ← x
x := 60 y := x
x = 60 y = x
Sometimes, we don't know the original value of a variable, but need to add a certain number to it. In this case, we can use the following form:
y ← 60 y ← y + 3
y := 60 y := y + 3
y = 60 y = y + 3
y = 60 y += 3
It may be confusing why it works, but this is related to operator precedence, which we'll discuss later.
Remember that the expression on the left is always the variable you want to change, while the expression on the right is always the value to be put into the variable. Assignment statements like 2 ← 3, y + 3 ← y + 4 or myConstant ← y + 3 will result in errors as they don't make any sense! (How can you, for instance, assign 3 to 2?)
An operator can be a unary operator or a binary operator. A unary operator has only one operand. The most common unary operator is the unary minus, which reverses the sign of a number. For example -2 is negative two and -(-2) makes 2.
Binary operators are far more common. Binary operators have operands on both sides. For example, 1 + 1 returns 2. The assignment operator, which we've just met, is also a binary operator.
Operators related to arithmetic are arithmetic operators. The most common common binary arithmetic operators are shown below.
In all likelihood, you have no problems with the four basic operations, but Mod and Div may be a tad problematic. In fact, they're less intimidating than they might sound! In a division operation, Mod returns the remainder, while Div returns the quotient. Our usual division operator returns a fraction when the remainder is 0.
Relational operators, also known as comparison operators, compare two variables and return a Boolean variable. For example, 1 < 2 returns TRUE and 1 > 2 returns FALSE. Here are some common relational operators.
|Equal to||= or ==||=||==|
|Not equal to||<>||!=|
|Larger than or equal to||>=|
|Smaller than or equal to||<=|
These operators can always be used with numbers. In some languages, you can compare characters, strings, Boolean values and even other types like spaceships and dinosaurs with them. However, a detailed discussion of them is outside the scope of this book, especially those parts requiring object-oriented languages.
Relational operators compare two values and turn them into Boolean values, while logical operators manipulated the Boolean values themselves. The most common logical operators are AND, OR and NOT. NOR, XOR and NAND are other logical operators. We will only discuss AND, OR, NOT and XOR in this book.
- NOT simply flips a Boolean value around. NOT TRUE returns FALSE and NOT FALSE returns TRUE. If a is TRUE, then NOT a is FALSE; if b is FALSE, then NOT b is TRUE.
- AND returns TRUE if both operands are true; otherwise, it returns FALSE. TRUE AND FALSE returns FALSE, while TRUE AND TRUE returns TRUE.
- OR returns FALSE if both operands are false; otherwise, it returns TRUE. TRUE OR FALSE returns TRUE, while FALSE OR FALSE returns FALSE.
- XOR returns TRUE if one and only one of the operands is TRUE. TRUE XOR FALSE returns TRUE, while TRUE XOR TRUE returns FALSE.
All this can be very confusing! A truth table shows all the outcomes of these logical expressions. Let's say we have two Boolean variables, p and q. Here are the truth tables for NOT p, p AND q, and p OR q:
|p||q||p AND q|
|p||q||p OR q|
With this in mind, we can easily create more complicated truth tables (the round brackets are called the grouping operator and indicate that the parts inside are to be performed first):
|p||q||p OR q||NOT(p OR q)|
Relational operators are often used with comparison operators. For example, NOT((1 = 2) OR (2 < 3)) gives TRUE. We will see how useful they are in the coming chapters.
Order of operationsEdit
Remember the order of operations (PEMDAS or BOMDAS/BIMDAS) you learnt in Maths class? ActionScript also applies different operators in a fixed order. In programming, there are two things we need to consider when determining the order of operations: operator precedence and operator associativity.
What is operator precedence?Edit
If we want a certain operation to have higher precedence, we should use the grouping operator, which is a pair of round brackets:
We must be very careful with our operator precedence in programming, or errors will result. When in doubt, it is advisable to use the grouping operator.
What is operator associativity?Edit
Consider the statement
my_number ← 3 * 2 + 5 * 6. 3 * 2 and 5 * 6 have the same precedence, so which one should be performed first? This can be answered with operator associativity. Associativity also varies, but the associativity of the assignment operator is nearly always right to left. For this reason, x ← x + 3 will add 3 to x as the addition operation is performed first.
A subprogram is a smaller program that performs a certain task. To work, the parent program should give, or 'pass', some data to the subprogram. The subprogram has a number of parameters that pass the arguments to the subprogram. Some subprograms have a return value; others don't. In some languages, including Pascal, procedures are subprograms that do not return a value, and functions are subprograms that do return a value. In others, functions and subprograms are the same.
As an example, if you have experience with spreadsheets, you must have used the function SUM(<cell range>). SUM is a subprogram with one parameter. The cell range we pass to the subprogram is an argument. This function returns a value, which is the sum of the cells. (Note: All spreadsheet functions return values.)
To illustrate, we will first make a subprogram without a return value, then a subprogram without.