Sequence
Statements run one after another, top to bottom. Order matters: swapping two lines can change the result.
price = 40
price = price * 0.9 # 10% off -> 36.0
price = price + 5 # delivery -> 41.0Doing the delivery first would give 40.5 instead.
Selection
if / elif / else picks one branch. Conditions are checked in order and only the first true branch runs, so put the most specific test first.
def grade(score):
if score >= 80:
return "A"
elif score >= 65:
return "B"
elif score >= 50:
return "C"
else:
return "U"Combine conditions with and, or, not. A range check: if 1 <= n <= 10: in Python, if (n >= 1 && n <= 10) in Java.
Iteration
- Count-controlled (
for): the number of repetitions is known in advance. - Condition-controlled (
while): repeats while a condition holds; it may run zero times. - Post-condition (
do ... whilein Java): runs the body at least once, then tests. Ideal for input validation.
range(start, stop, step) stops before stop: range(1, 10, 4) gives 1, 5, 9.
Nested loop trace:
total = 0
for i in range(1, 4):
for j in range(i, 4):
total = total + 1
print(total)| i | inner j values | iterations | total |
|---|---|---|---|
| 1 | 1, 2, 3 | 3 | 3 |
| 2 | 2, 3 | 2 | 5 |
| 3 | 3 | 1 | 6 |
Output: 6.
Java input validation with a post-condition loop:
int age;
do {
System.out.print("Age (13-19): ");
age = input.nextInt();
} while (age < 13 || age > 19);
System.out.println("Accepted " + age);Functions and modular code
A function (method in Java) is a named block that can take parameters and return a value. Benefits: code is written once and reused, each part can be tested separately, programs are easier to read, and team members can work on different modules.
def count_between(values, low, high):
count = 0
for v in values:
if low <= v <= high:
count = count + 1
return count
print(count_between([4, 12, 7, 20, 9], 5, 10)) # 2public static int countBetween(int[] values, int low, int high) {
int count = 0;
for (int v : values) {
if (v >= low && v <= high) count++;
}
return count;
}A parameter is the name in the definition (values); an argument is the actual value passed in the call. return ends the function immediately; print only displays and gives nothing back to the caller.
Scope of variables
- A local variable is created inside a function and exists only while it runs.
- A global variable is defined at the top level and visible throughout the file.
level = 1
def level_up(level):
level = level + 1
return level
new = level_up(level)
print(level, new) # 1 2The parameter level is a new local variable that happens to share a name with the global one. Changing it does not touch the global. Locals reduce accidental side effects and make functions self-contained; overuse of globals makes bugs hard to trace.
Common mistakes
- Using
=(assignment) where==(comparison) is meant. - Off-by-one with
range:range(5)is 0 to 4. - Infinite
whileloops because the loop variable never changes. - Printing inside a function when the question asks it to return a value.
- Dividing by zero when a list or filtered set is empty; guard with an
if.
Exam tips
- Read "construct a function" carefully: match the name, parameters and return type exactly.
- Handle the edge case the question mentions (empty list, no matches).
- For scope questions, say explicitly which variable is local and which is global.
- Indent consistently; in Python, indentation is the structure.