An expression (式) is a piece of code that yields a single value when it is evaluated (評価).
(4 + 6) × 2
10 × 2
20
Doing the remaining multiplication gives the expression's single value, 20.
3 / 3
Evaluation means calculating an expression to get its value. A variable (変数) is a named container that holds a value, and inside an expression it stands for whatever value it holds at that moment. The typical form joins values or variables with operators, but a single number or a single variable on its own is also an expression.
AnalogyIt is like a calculation keyed into a calculator: press the equals key and exactly one answer appears.
How to rememberIn English it is an expression: something that "expresses" the one value the calculation produces.
Exam points
ExamThe value of an expression (式) depends on the values held in its variables at the moment it is evaluated. The same expression gives a different result once a variable's value changes.
Mix-upAn expression (式) only computes a value and leaves the variables unchanged. What rewrites a variable's contents is assignment (代入).
An operator (演算子) is a symbol for an operation such as adding, comparing or combining, and it joins with values to form an expression.
Main operators
Arithmetic operators (e.g. + − × ÷)
Calculate with numbers
Comparison operators (e.g. = ≠ < ≦)
Compare two values and return true or false
Logical operators (e.g. and or not)
Combine or invert true and false
The value an operator acts on is called an operand (被演算子). For example, arithmetic operators calculate with numbers, comparison operators check which of two values is larger or whether they are equal, and logical operators combine or invert true and false. When one expression has several arithmetic operators, precedence (優先順位) applies: parentheses first, then multiplication and division, then addition and subtraction.
AnalogyIt plays the part of verbs like "chop", "mix" and "bake" in a recipe, deciding what to do with the ingredients.
How to rememberThe Japanese word 演算子 ends in 子, the same character as in 原子 (atom) and 因子 (factor), so read it as a small part that carries out an operation.
Exam points
ExamAn operation that gives the remainder (余り) of a division tells you whether one number divides another evenly. A remainder of 0 means it divides evenly.
ExamBecause of precedence (優先順位), b × 2 is calculated first in "a + b × 2". To do the addition first, put that part in parentheses.
A conditional expression (条件式) is an expression that evaluates to either true (真) or false (偽), and it decides which way a branch goes and whether a loop continues.
Condition P
Condition Q
P and Q
P or Q
True
True
True
True
True
False
False
True
False
True
False
True
False
False
False
False
The basic form compares two values with a comparison operator; for example, "a ≧ 18" is true when a is 18 or more. Several conditions are joined with logical operators: and is true only when all of them are true, and or is true when at least one is true. not swaps true and false.
AnalogyIt is like the height marker at the entrance to a theme park ride: you either meet the mark or you do not, and that decides whether you get on.
How to rememberWrite true as 1 and false as 0: and matches the product, and or matches whether the sum is 1 or more.
Exam points
ExamA check that requires every condition to hold uses a conditional expression (条件式) that joins those conditions with and.
ExamWhen a branch contains another branch, the outer conditional expression (条件式) decides which path to take by being true or false, and the inner one is evaluated inside that path.
Mix-upIn a conditional expression (条件式), "=" is a comparison operator (演算子) that checks whether both sides are equal, and its result is true or false. It works differently from assignment (代入) (←), which puts a value into a variable.
Assignment (代入) evaluates the expression on the right and stores the resulting value in the variable (変数) on the left; in the pseudo-language it is written with "←".
n ← n × 2 (n is 5)
Compute the right side: 5 × 2 = 10
Store 10 in n on the left
n is now 10
The contents of n are replaced with 10, and the old 5 is gone.
4 / 4
After an assignment, the variable's old value is gone and the new value takes its place. Even when the same variable appears on both sides, as in "n ← n × 2", the right side is computed first with the current value and the result is then stored back in n. As assignments run one after another, the variable's value changes in the order they are executed.
AnalogyIt is like erasing the number written in a box on a whiteboard and writing a new one in its place.
How to rememberThe arrow "←" points the way the value travels: it flows from right to left.
Exam points
ExamTo swap the values of two variables, first move the value of the variable about to be overwritten into a temporary variable. Otherwise the first assignment (代入) overwrites one of the values and it is lost.
ExamAn assignment (代入) inside a loop updates the variable on every pass, and the next pass starts calculating from the updated value.
A comment (注釈) is an explanation written into a program for people to read, and it is never executed.
Writing down what a process is for or what a variable means makes the program easier to follow for whoever reads or fixes it later. IPA's pseudo-language uses forms such as enclosing the text in "/* … */" or writing it after "//". Adding or deleting comments does not change what the program produces.
AnalogyIt is like a note on a map saying "steep hill here": the road stays the same, but the note helps whoever reads the map.
How to rememberThe Japanese word combines "note" and "explain", so read it as an explanation added beside the main text.
Key points
An expression (式) evaluates to one value, and with several arithmetic operators the order is parentheses, then multiplication and division, then addition and subtraction.
A conditional expression (条件式) is true or false: and is true when every condition is true, and or is true when any one of them is true.
Assignment (代入) evaluates the right side before storing the result in the variable on the left, so a variable that appears on both sides is updated from its current value.
A comment (注釈) is an explanation for people to read, and adding or deleting one leaves the processing result unchanged.