Lesson 5: Control Flow and Cases

Intermediate

Traditional languages use if/else, switch, and while. Phograph handles conditional logic differently: through multiple cases and control annotations.

Cases: the Phograph if/else

A method can have more than one case. Each case is a separate dataflow diagram. When the method is called:

  1. Case 1 executes first.
  2. If a node in case 1 has a nextCaseOnFailure annotation and it fails (produces false), execution jumps to case 2.
  3. Case 2 executes. If it also fails, case 3 is tried, and so on.
  4. The outputs of whichever case succeeds become the method’s outputs.

Think of cases like pattern-matching clauses or if/elif/else branches, but each branch is a complete visual graph.

Navigating cases

When a method has multiple cases, a bar at the top of the canvas shows “Case 1 of 3” with arrow buttons. You can also navigate with the Class Browser sidebar.

Control annotations

An annotation is a badge on a node that tells the engine how to handle success or failure. The most important annotation is nextCaseOnFailure: if this node outputs false, abandon this case and try the next one.

Common annotations:

AnnotationMeaning
nextCaseOnFailureIf this node fails, try the next case
continueOnFailureIf this node fails, skip it and keep going
terminateOnSuccessIf this node succeeds, stop (used for loop termination)
terminateOnFailureIf this node fails, stop the loop

Walkthrough: Fibonacci base case

Look at the Fibonacci example’s fib method, case 1:

Inputs (n) n n 1 <= nextCaseOnFailure Outputs (n)

The <= node compares n to 1. It has the nextCaseOnFailure annotation (shown as a badge on the node).

Case 2 is the recursive case: it computes fib(n-1) + fib(n-2).

How failure works

A node fails in Phograph when:

Without an annotation, a false output is just a normal Boolean value. The annotation is what gives it control-flow meaning.

No boolean blindness In text-based languages, an if statement tests a boolean but discards it. In Phograph, the match node both tests and passes data through. The “successful” path is the normal data flow; failure diverts to the next case. Data keeps flowing — no values are lost.

The if primitive

For simple conditionals within a single case, Phograph has an if primitive with 3 inputs: condition, then-value, else-value. It outputs whichever value corresponds to the condition.

true "A" "B" cond then else if outputs "A"

Exercise 5.1 — Absolute value

Create a method my-abs (1 input, 1 output) using two cases:

  1. Case 1: Check if n >= 0 with nextCaseOnFailure. If true, return n unchanged.
  2. Case 2: Compute 0 - n and return it.
  3. Call my-abs(-5) from main. Result should be 5.
  4. Also try my-abs(3). Should return 3.

Exercise 5.2 — Max of two numbers

  1. Create a method max2 (2 inputs, 1 output).
  2. Case 1: Compare input 0 >= input 1 with nextCaseOnFailure. If true, return input 0.
  3. Case 2: Return input 1 (this is the fallback).
  4. Test with max2(3, 7)7.
  5. Test with max2(10, 4)10.

Exercise 5.3 — Use the if primitive

  1. Build a single-case method that takes a number and returns "positive" or "negative" using the if primitive.
  2. Compare the input to 0 with >=.
  3. Feed the boolean, "positive", and "negative" into if.
  4. Test with several values.

Key Concepts