What Does This Program Do? - Branching
Tracing pseudocode programs that use IF-THEN-ELSE statements.
Overview
"What Does This Program Do?" is an ACSL category that presents a program written in pseudocode and asks you to determine what the program does or what value it outputs. This page focuses specifically on branching (IF-THEN-ELSE statements), which is covered in Junior Division Contest 1.
Key Concepts:
- Branching: Programs use IF-THEN-ELSE statements to make decisions based on conditions
- Boolean Expressions: Conditions use comparison operators (>, <, >=, <=, ==, !=) and logical operators (&&, ||, !)
- Trace Execution: Step through the program line by line, tracking variable values
- Conditional Logic: Only execute code blocks when their conditions are true
Key Concepts
ACSL Pseudocode - Branching Constructs
ACSL uses a standardized pseudocode format. Here are the key constructs for branching:
Decision Statements (Branching):
IF boolean expression THEN
Statement(s)
ELSE (optional)
Statement(s)
END IF
Comparison Operators:
>- greater than<- less than>=- greater than or equal to<=- less than or equal to==- equal to!=- not equal to
Logical Operators:
&&- AND (both conditions must be true)||- OR (at least one condition must be true)!- NOT (negates the condition)
How to Solve Branching Problems
Step-by-Step Strategy:
- Read the program carefully - Understand what variables are used and their initial values
- Identify input values - Note any INPUT statements and what values are provided
- Create a tracking table - Make a table with columns for each variable and a row for each step of execution
- Trace execution line by line - Follow the program flow, executing statements in order
- Evaluate conditions - When you encounter an IF statement, evaluate the boolean expression
- Follow the correct branch - Execute the THEN block if true, ELSE block if false (if present)
- Update your table - Record variable changes in the table after each operation
- Find the output - Determine what value is printed or what the final state is
Common Mistakes to Avoid
- Don't execute both THEN and ELSE: Only one branch executes, never both.
- Watch for nested IFs: Inner IF statements only execute if the outer condition is true.
- Sequential IFs are independent: Multiple IF statements in a row are evaluated separately - one being true doesn't affect the others.
- Track variable changes carefully: Update your table after each assignment, not before.
- Don't skip END IF: Each IF must have a matching END IF. Indentation helps identify which END IF matches which IF.
- Evaluate conditions correctly: Remember that
==checks equality, while=is assignment.
Quick Reference
Tips for Success:
- Always create a tracking table before you start
- Read the entire program first to understand the flow
- Work through one IF statement at a time
- Update your table immediately after each assignment
- Double-check your condition evaluations
- Verify your final answer makes logical sense
Examples
INPUT x
y = 0
IF x > 10 THEN
y = y + 5
END IF
IF x > 20 THEN
y = y + 10
ELSE
y = y + 2
END IF
OUTPUT y
Solution using a tracking table:
| Step | Statement | x | y |
|---|---|---|---|
| 1 | INPUT x | 15 | — |
| 2 | y = 0 | 15 | 0 |
| 3 | y = y + 5 (inside IF x > 10) | 15 | 5 |
| 4 | y = y + 2 (inside ELSE of IF x > 20) | 15 | 7 |
| 5 | OUTPUT y | 15 | 7 |
Final answer: 7
Practice Problems
Problem 1 Junior
What is printed when this program is run?
x = 10
y = 3
z = 5
w = 2
IF x > z * w THEN
x = x - z
END IF
IF (y + w) < (x - z) THEN
y = y + w
ELSE
z = z + w
END IF
IF x * y == z * w THEN
x = x * y / w
ELSE
y = y * z / x
END IF
IF z ↑ 2 >= (w + 1) ↑ 2 THEN
z = w + 1
ELSE
w = w + 1
END IF
OUTPUT (x + y) / w + (z + w) ↑ 2
Solution
Let's trace through the program using a tracking table:
| Step | Statement | x | y | z | w |
|---|---|---|---|---|---|
| 1 | Initial values | 10 | 3 | 5 | 2 |
| 2 | (IF x > z*w is FALSE, skip) | 10 | 3 | 5 | 2 |
| 3 | z = z + w (inside ELSE of IF (y+w) < (x-z)) | 10 | 3 | 7 | 2 |
| 4 | y = y * z / x (inside ELSE of IF x*y == z*w) | 10 | 2 | 7 | 2 |
| 5 | z = w + 1 (inside IF z↑2 >= (w+1)↑2) | 10 | 2 | 3 | 2 |
| 6 | OUTPUT (x+y)/w + (z+w)↑2 | 10 | 2 | 3 | 2 |
Initial values: x=10, y=3, z=5, w=2
Step 1: IF x > z*w THEN. Evaluate: 10 > 5*2 = 10 > 10, which is FALSE. Skip this IF.
Step 2: IF (y+w) < (x-z) THEN. Evaluate: (3+2) < (10-5) = 5 < 5, which is FALSE. Execute ELSE: z = z+w = 5+2 = 7
Current values: x=10, y=3, z=7, w=2
Step 3: IF x*y == z*w THEN. Evaluate: 10*3 == 7*2 = 30 == 14, which is FALSE. Execute ELSE: y = y*z/x = 3*7/10 = 21/10 = 2 (integer division)
Current values: x=10, y=2, z=7, w=2
Step 4: IF z↑2 >= (w+1)↑2 THEN. Evaluate: 7↑2 >= (2+1)↑2 = 49 >= 9, which is TRUE. Execute THEN: z = w+1 = 2+1 = 3
Final values: x=10, y=2, z=3, w=2
Output: (x+y)/w + (z+w)↑2 = (10+2)/2 + (3+2)↑2 = 12/2 + 5↑2 = 6 + 25 = 31
Answer: 31
Problem 2 Junior
What is printed when this program is run?
p = 8
q = 4
r = 6
s = 3
IF p > q * s THEN
p = p - q
END IF
IF (q + s) <= (p - r) THEN
q = q + s
ELSE
r = r + s
END IF
IF p * q != r * s THEN
p = p * q / r
ELSE
q = q * r / p
END IF
IF r ↑ 2 < (s + 2) ↑ 2 THEN
r = s + 2
ELSE
s = s + 2
END IF
IF p + q * r == r + s * q THEN
p = q * r
ELSE
r = s * q
END IF
OUTPUT (p + q) / s + (r - s) ↑ 2
Solution
Let's trace through the program using a tracking table:
| Step | Statement | p | q | r | s |
|---|---|---|---|---|---|
| 1 | Initial values | 8 | 4 | 6 | 3 |
| 2 | (IF p > q*s is FALSE, skip) | 8 | 4 | 6 | 3 |
| 3 | r = r + s (inside ELSE of IF (q+s) <= (p-r)) | 8 | 4 | 9 | 3 |
| 4 | p = p * q / r (inside IF p*q != r*s) | 3 | 4 | 9 | 3 |
| 5 | s = s + 2 (inside ELSE of IF r↑2 < (s+2)↑2) | 3 | 4 | 9 | 5 |
| 6 | r = s * q (inside ELSE of IF p+q*r == r+s*q) | 3 | 4 | 20 | 5 |
| 7 | OUTPUT (p+q)/s + (r-s)↑2 | 3 | 4 | 20 | 5 |
Initial values: p=8, q=4, r=6, s=3
Step 1: IF p > q*s THEN. Evaluate: 8 > 4*3 = 8 > 12, which is FALSE. Skip this IF.
Step 2: IF (q+s) <= (p-r) THEN. Evaluate: (4+3) <= (8-6) = 7 <= 2, which is FALSE. Execute ELSE: r = r+s = 6+3 = 9
Current values: p=8, q=4, r=9, s=3
Step 3: IF p*q != r*s THEN. Evaluate: 8*4 != 9*3 = 32 != 27, which is TRUE. Execute THEN: p = p*q/r = 8*4/9 = 32/9 = 3 (integer division)
Current values: p=3, q=4, r=9, s=3
Step 4: IF r↑2 < (s+2)↑2 THEN. Evaluate: 9↑2 < (3+2)↑2 = 81 < 25, which is FALSE. Execute ELSE: s = s+2 = 3+2 = 5
Current values: p=3, q=4, r=9, s=5
Step 5: IF p+q*r == r+s*q THEN. Evaluate: 3+4*9 == 9+5*4 = 39 == 29, which is FALSE. Execute ELSE: r = s*q = 5*4 = 20
Final values: p=3, q=4, r=20, s=5
Output: (p+q)/s + (r-s)↑2 = (3+4)/5 + (20-5)↑2 = 7/5 + 15↑2 = 1 + 225 = 226
Answer: 226