Sitonce
Country: US
Show exams for United States Hong Kong
Sign in

Programming Control Flow: Praxis Computer Science 5652

Updated 7 min read
Key takeaway

To trace program control flow, record initialization, conditions, executed statements, updates and termination.

  • Follow the language or pseudocode rules in the prompt, especially for loop boundaries and comparisons.
  • Use the exact semantics specified, including boundary behavior.
  • Check every option against the prompt before you finalize the response.
On this page20 sections
  1. Control flow is the order of execution
  2. Trace a conditional completely
  3. Loops need initialization, condition and update
  4. Off-by-one errors are boundary errors
  5. Functions and parameters
  6. Boolean logic and short-circuiting
  7. Testing boundary paths
  8. Quick reference
  9. Recursion needs a base case
  10. Trace output separately from state
  11. Track nested conditionals in execution order
  12. Mutation differs from reassignment
  13. Use invariants to explain a loop
  14. Separate syntax errors from logic errors
  15. Understand function return paths
  16. Trace nested repetition carefully
  17. Separate loop exit from loop completion
  18. Make branch conditions explicit
  19. Test loop invariants and termination
  20. Debug with the smallest failing input

Control flow is the order of execution

A program normally proceeds through statements in sequence, but conditionals choose among paths and loops repeat a block. To understand the result, track the program counter conceptually and record variables that affect later decisions.

Use the language or pseudocode notation stated. Assignment, equality comparison, integer division and loop boundaries differ among languages and notations.

Trace a conditional completely

A conditional evaluates a Boolean expression and selects a branch. Work out the expression using the current variable values before executing the chosen statements. A branch that is skipped does not update its variables.

Example: if points are at least 10, add a badge; otherwise add a reminder. At exactly 10, the first condition is true if the operator is greater than or equal. Changing it to greater-than changes the boundary case.

Loops need initialization, condition and update

A loop's state changes across iterations. Identify the initial values, condition, body, update and exit state. If the condition never changes from true, the loop can fail to terminate. If the update skips a value, the result can be wrong without an obvious syntax error.

For total = 0 and i = 1, while i <= 4 add i to total, then increment i. The totals are 1, 3, 6 and 10. After the final update i becomes 5 and the condition fails.

Off-by-one errors are boundary errors

Inclusive and exclusive bounds determine how many times a loop runs. A loop from i = 0 while i < n executes for indices 0 through n-1, which is n iterations. A loop that uses i <= n may attempt an extra position in a zero-based array.

Trace the smallest and largest cases. For an empty collection, does the body run? At the last valid index, does the condition stop before reading past the end?

Functions and parameters

A function groups a task, can receive parameters and may return a value. When tracing a call, substitute argument values, track local variables and identify the return path. Scope determines which variable a statement changes.

Do not assume that changing a local parameter changes the caller's variable in every language or for every data type. Use the language semantics and object model stated in the prompt.

Boolean logic and short-circuiting

AND, OR and NOT combine conditions. De Morgan's laws can help rewrite a negated condition, but parentheses and evaluation order matter. Some languages short-circuit after the result is known; others may evaluate expressions differently.

If the question uses Python, its Boolean operators and truth testing follow Python rules. If it uses generic pseudocode, follow the notation defined by ETS rather than importing a language-specific behavior.

Testing boundary paths

A useful test set includes ordinary inputs, boundary values, empty or single-element collections and values that trigger each branch. Testing can reveal defects but does not prove that a program has no defects.

For a loop over four entries, test 0, 1, 4 and 5 entries if the surrounding function permits them. Identify the expected behavior before running the trace.

Quick reference

Trace stepQuestion to ask
InitializeWhat are starting values?
ConditionWhat Boolean result controls execution?
BodyWhich statements actually run?
UpdateWhat state changes next?
ExitWhy does repetition stop?

Recursion needs a base case

A recursive function calls itself on a smaller or otherwise changed problem. A base case stops the chain. Without progress toward that case, recursion can continue until a resource limit is reached.

Trace each call's arguments and return value. For a sum of the first n positive integers, a recursive call might reduce n by one until zero, but the exact definition controls the result.

Trace output separately from state

A program can change a variable without printing it, or print a value before the next update. Track output events in order as well as final variable state.

If asked for console output, reproduce only what is emitted and preserve line breaks or separators described by the code.

Track nested conditionals in execution order

For nested if statements, evaluate the outer condition first. Only the selected branch executes, and its statements can change values used later.

Indentation can define blocks in some languages, while braces or keywords define them in others. Read the syntax and scope correctly.

Mutation differs from reassignment

Changing an element inside a mutable collection can affect other references to that same object, while rebinding a local variable may only change the local name. The exact semantics depend on the language.

For a named language, verify its object and parameter rules in documentation. For pseudocode, use the specified abstraction.

Use invariants to explain a loop

A loop invariant is a condition that remains true at a particular point in each iteration. It can help explain why the loop maintains a correct partial result and why termination produces the intended answer.

For a summation loop, an invariant can state that total equals the sum of all processed values. Check it at initialization, after the body and at exit.

Separate syntax errors from logic errors

A syntax error prevents a program from being parsed; a logic error lets it run but produces the wrong result. A runtime error occurs during execution.

Testing and debugging approaches differ: syntax requires fixing code structure, while logic requires tracing assumptions and expected behavior.

Understand function return paths

A function may return from several branches. Follow the first executed return and do not continue evaluating statements that cannot run afterward.

If a function's branch has no return, the language may provide a default value or error behavior. Use the stated language rules.

Trace nested repetition carefully

Consider an outer loop that runs for i equal to 1 and 2, and an inner loop that runs j from 1 through 3 on each outer iteration. The inner body executes six times, not five: three runs for each of two outer values. If the body adds i to a total, the result is 3 times 1 plus 3 times 2, or 9.

For a nested loop where the inner bound depends on i, write a small table with one row per outer iteration and count the inner runs separately. This reveals triangular patterns that are easy to misread as a rectangular product.

Separate loop exit from loop completion

A break statement exits the nearest loop in languages that define it that way; continue skips the rest of the current iteration and proceeds to the next. A return statement can exit a function entirely. These actions change which later statements execute.

When analyzing a code question, mark the point of each control transfer and resume at the correct location. Do not treat a skipped iteration as if the loop had ended or assume a nested break exits every enclosing loop.

Make branch conditions explicit

A branch chooses a path based on a Boolean expression. For conditions using AND, both parts must be true; for OR, at least one must be true. Parentheses can change how compound tests group, and short-circuit behavior depends on language semantics.

Test a condition with a truth table or a few boundary examples. For a range check such as age >= 13 && age <= 19, verify values below, at and above both endpoints. This reveals inclusive-boundary mistakes before they are hidden inside a larger program.

Test loop invariants and termination

A loop invariant is a condition that should remain true before and after each iteration. For a summation loop, an invariant might say that the accumulator equals the sum of all values already processed. Checking initialization, preservation and the final condition can reveal whether the program includes every intended element.

Termination requires progress toward a stopping condition. If a counter is supposed to increase but a branch skips the update, the loop may not end. Trace both the normal path and any conditional path that changes the update.

Debug with the smallest failing input

When a trace gives an unexpected result, reduce the input while preserving the failure. A one-element or two-element case often reveals whether a loop starts too late, ends too soon or mishandles an empty case. Then inspect state immediately before the incorrect decision.

This is more effective than rereading the entire program. State a specific hypothesis, such as “the update occurs before the body,” and test it against the actual sequence. Small examples make execution order visible.

Common questions

What should I record in a loop trace?

Starting state, condition, executed body, update and exit state.

Are assignment and comparison the same?

No. Exact syntax and meaning depend on the language or pseudocode stated.

Does testing prove a program is correct?

Testing finds defects but does not prove that no defects exist.