Skip to content

Expressions, Assignment, and Comments

標準~27 min5 terms9 past questions

Goals of this unit

  • Find the value of an expression by following operator precedence.
  • Trace the flow of branches and loops, using the fact that a conditional expression is always either true or false.
  • Tell apart the roles of the right side and the left side of an assignment, and follow how a variable's value changes step by step.
  • Explain why a comment has no effect on the processing result.
Terms at a glance (5)
TermIn one lineWhat it doesPseudo-language example
式Code that yields one value when evaluatedComputes a valuea + b × 2
演算子A symbol for an operation applied to valuesSets the operation: calculate, compare or combine+ × ≦ and
条件式An expression whose result is true or falseUsed to decide branches and loopsx ≧ 0 and x < 10
代入Storing the right side's value in the variable on the leftOverwrites a variable's values ← s + x
注釈Explanatory text that is never executedTells readers what the code is meant to do/* compute the total */

Terms

An expression (式) is a piece of code that yields a single value when it is evaluated (評価).

  1. (4 + 6) × 2
  2. 10 × 2
  3. 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.

条件式Conditional Expression

Open in glossary 条件式

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 PCondition QP and QP or Q
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse

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.

3 past questions on this term 条件式

Expression vs. Conditional Expression

AspectExpressionConditional Expression
Key differenceResult of evaluationA single value such as a numberEither true or false
Main operators usedArithmetic operatorsComparison and logical operators
Where it is usedMainly the right side of an assignment and calculationsMainly the tests in if and while

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 "←".

  1. n ← n × 2 (n is 5)
  2. Compute the right side: 5 × 2 = 10
  3. Store 10 in n on the left
  4. 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.

2 past questions on this term 代入

Conditional Expression vs. Assignment

AspectConditional ExpressionAssignment
Symbol in the pseudo-language= (checks equality)← (stores a value)
Key differenceVariable's valueUnchangedThe variable on the left is overwritten
ResultGives true or falseThe variable on the left gets a new 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.

Check questions

4 past questions from this unit

Start the check questions

All past questions in this topic