Variables, Expressions, Boolean Logic and Selection

AP Computer Science Principles · Unit 3 — Algorithms and Programming

Assignment is an action, not an equation

A variable is an abstraction inside a program that holds one value at a time; a well-chosen name tells the reader what the value means. In the exam pseudocode the assignment statement is written with an arrow:

a ← expression

It means: evaluate the expression on the right using the current values, then store the result in a, replacing whatever was there. The arrow is directional, so a ← b copies the value of b into a at that moment; changing b later does not change a. To exchange two values you need a temporary variable, or an arithmetic trick such as a ← a + b, b ← a - b, a ← a - b.

Arithmetic uses +, -, *, / and MOD. a MOD b is the remainder when a is divided by b (for $a \ge 0$, $b > 0$): 17 MOD 5 is 2 and 4 MOD 9 is 4. MOD has the same precedence as * and /, so 2 + 7 MOD 3 is $2+1=3$. MOD is how programs test divisibility (n MOD 2 = 0 means even), extract the last digit (n MOD 10) and wrap around a cycle ((day + 3) MOD 7).

The order of statements is part of the algorithm. Sequencing means statements run one after another in the order given, and swapping two lines usually changes the result.

Boolean expressions and selection

A Boolean value is either true or false. Relational operators =, ≠, >, <, ≥, ≤ compare values and produce a Boolean. Logical operators combine Booleans: NOT condition reverses it; condition1 AND condition2 is true only if both are true; condition1 OR condition2 is true if at least one is true.

Negating a compound condition follows De Morgan's laws: NOT (A AND B) is equivalent to (NOT A) OR (NOT B), and NOT (A OR B) is equivalent to (NOT A) AND (NOT B). Negating a comparison flips it completely: the opposite of x > 5 is x ≤ 5, not x < 5.

Selection chooses which statements run based on a Boolean condition:

IF(condition)
{
   <block of statements>
}
ELSE
{
   <second block of statements>
}

Exactly one of the two blocks runs. Selection statements can be nested inside one another; the inner condition is checked only when the outer branch containing it is taken. A chain of separate IF statements behaves differently from an IF … ELSE: each separate IF is checked using the values left by the previous one, so a value can be changed more than once. When two code segments look alike, find an input that makes them take different paths.

cost ← 5weight > 2 ? cost ← cost + 3 × (weight − 2) express ?cost > 10 ? RETURN(cost)cost ← cost × 2cost ← cost + 10 correctsaisai correctcorrectsai
Rẽ nhánh lồng nhau. Điều kiện cost > 10 chỉ được xét khi express đúng. Muốn truy vết, đi theo mũi tên với giá trị hiện tại của cost — giá trị này đã có thể đổi ở nhánh weight > 2 phía trên.

Worked example: Đổi chỗ hai biến và phủ định điều kiện

(1) Trace the segment and give the final values of a, b and c.

a ← 3
b ← 7
a ← a + b
b ← a - b
a ← a - b
c ← (a * 2 + b) MOD 4

(2) A ticket is free when (age < 6) OR (age ≥ 65). Write the condition for “the ticket is not free” without using NOT.

Solution

(1) a ← 3 + 7 = 10. b ← 10 − 7 = 3. a ← 10 − 3 = 7. Ba dòng giữa đã đổi chỗ hai giá trị mà không cần biến tạm: a = 7, b = 3. Cuối cùng $c = (7 \times 2 + 3)$ MOD 4 = 17 MOD 4 = 1. Kết quả a = 7, b = 3, c = 1.

(2) “Không miễn phí” là NOT((age < 6) OR (age ≥ 65)). Theo De Morgan, NOT của OR thành AND của hai NOT: (NOT(age < 6)) AND (NOT(age ≥ 65)). Lật từng phép so sánh: (age ≥ 6) AND (age < 65). Kiểm nhanh: 6 tuổi — không miễn phí, biểu thức đúng; 65 tuổi — miễn phí, biểu thức sai vì 65 < 65 sai.

Common mistake

Bẫy đọc mũi tên như dấu bằng toán học. Sau a ← b rồi b ← 5, biến a vẫn giữ giá trị cũ của b. Bẫy thứ hai: phủ định x > 5 AND y ≤ 2 thành x < 5 AND y > 2 — sai hai lần: phải đổi AND thành OR, và ngược của > là ≤. Bẫy thứ ba: coi hai lệnh IF rời nhau như IF … ELSE, bỏ qua việc lệnh IF thứ hai đọc giá trị vừa bị lệnh thứ nhất sửa.

Key takeaway

Chốt: gán = tính vế phải bằng giá trị hiện tại rồi đè vào biến; thứ tự lệnh là thuật toán. MOD là số dư, cùng ưu tiên với * và /. De Morgan: NOT(A AND B) = NOT A OR NOT B; NOT(A OR B) = NOT A AND NOT B; ngược của > là ≤. IF … ELSE chạy đúng một khối; IF rời nhau có thể sửa một biến nhiều lần.

← Extracting Information from Data · All AP Computer Science Principles lessons · Practice AP Computer Science Principles questions (free, instant scoring) · Iteration and Lists →