What Does This Program Do? - Branching
In Contest 1, WDTPD problems use only IF/THEN/ELSE statements - no loops yet. You’re given pseudocode and input values, and you need to determine the output. The skill is careful line-by-line tracing.
ACSL pseudocode syntax
ACSL uses its own pseudocode that looks like a mix of BASIC and Python. Here’s what you need to know:
INPUT: a, b
IF a > b THEN
a = a - b
ELSE
b = b - a
END IF
OUTPUT: a, b Key syntax rules:
- Variables are single letters or short names (no underscores)
IF ... THEN ... ELSE ... END IF- the END IF is always explicit=is assignment (not comparison)- Comparison operators:
>,<,>=,<=,==,!= - Logical operators:
AND,OR,NOT INPUT:reads values,OUTPUT:prints values- Integer division uses
INT()- e.g., MODgives the remainder - e.g.,
The variable table method
The fastest and most reliable way to trace branching programs is to make a table of variables and update it line by line.
Example:
INPUT: x = 15, y = 4
a = x MOD y
b = INT(x / y)
IF a > b THEN
c = a + b
ELSE
c = a * b
END IF
OUTPUT: c Build your table:
| Step | x | y | a | b | c |
|---|---|---|---|---|---|
| Input | |||||
| ? ? NO, take ELSE | |||||
Answer: 9
Why this works: You never have to hold values in your head. Every variable has a column, and you can verify each step.
Try it: trace this program
Step through line by line. Watch how the variables change at each IF/ELSE branch.
1INPUT: a, b2a = 103b = 254IF a > b THEN5 x = a - b6ELSE7 x = b - a8END IF9OUTPUT: x| Name | Value |
|---|---|
| a | 10 |
| b | 25 |
Nested IF statements
ACSL problems often nest conditions inside other conditions. Trace them outside-in.
Example:
INPUT: a = 7, b = 3, c = 5
IF a > c THEN
IF b > c THEN
x = a + b + c
ELSE
x = a - b + c
END IF
ELSE
IF b > c THEN
x = a + b - c
ELSE
x = a - b - c
END IF
END IF
OUTPUT: x Trace:
- Is ? ? YES, enter first block
- Is ? ? NO, enter ELSE
Answer: 9
Speed tip: Before computing anything, determine which path through the IFs you take. Mark the path first, then compute only the relevant line.
Chained IF (no ELSE)
Watch out for multiple IFs in sequence (not nested). Each one is checked independently.
Example:
INPUT: a = 7, b = 3
IF a > b THEN
a = a - b
END IF
IF a > b THEN
b = b + a
END IF
OUTPUT: a, b Trace:
- Is ? ? YES, so
- Is ? ? YES, so
Answer: 4 7
The trap: Many students think the second IF uses the original value of (). It doesn’t - it uses the updated value (). In chained IFs, earlier statements affect later ones.
Operator precedence
ACSL follows standard math precedence:
- Parentheses (innermost first)
- NOT
*,/,INT(),MOD+,->,<,>=,<=,==,!=- AND
- OR
Examples:
- (not )
- (MOD before addition)
- evaluates as , which is , which is
Common ACSL tricks in branching
- Swapping with a temp variable:
temp = a
a = b
b = temp This swaps a and b. Recognize the pattern instantly.
- Min/max selection:
IF a > b THEN
max = a
ELSE
max = b
END IF - Absolute value:
IF x < 0 THEN
x = -x
END IF - Divisibility check:
IF x MOD 3 == 0 THEN
OUTPUT: "divisible"
END IF Common mistakes
- Not updating variables between chained IFs. Always use the current value, not the original.
- Confusing = (assignment) and == (comparison).
a = 5sets to .a == 5checks if is . - Misreading nested IFs. When IFs are deeply nested, indent your trace to match the nesting level.
- Misunderstanding INT(). ACSL defines as the greatest integer (the floor function). So , and (not , because ).
Contest strategy
- Read the entire program first to understand the structure
- Build a variable table before executing any lines
- Mark which branch you take at each IF before computing
- After finding your answer, quickly re-trace to verify
- Typical time: 60-90 seconds per problem