Control Flow

Keikaku provides expressive control structures for decision-making and iteration, using intuitive keywords like foresee, cycle, and otherwise.

Control Flow Keywords

Conditionals

  • foresee - if
  • alternate - else if
  • otherwise - else

Loops

  • cycle while - while loop
  • cycle through - for each
  • cycle from X to Y - range

Conditionals (foresee)

Use foresee for if-statements. Chain with alternate for else-if branches, and otherwise for the default case.

Basic Conditionals

KEIKAKU Protocol
1# Simple if
2temperature := 25
3foresee temperature > 30:
4 declare("It's hot!")
5
6# If-else
7age := 20
8foresee age >= 18:
9 declare("Adult")
10otherwise:
11 declare("Minor")
12
13# If-else if-else chain
14score := 85
15foresee score >= 90:
16 declare("Grade: A")
17alternate score >= 80:
18 declare("Grade: B")
19alternate score >= 70:
20 declare("Grade: C")
21alternate score >= 60:
22 declare("Grade: D")
23otherwise:
24 declare("Grade: F")
25# Output: Grade: B

Comparison Operators

KEIKAKU Protocol
1a := 10
2b := 20
3
4# Equality
5foresee a == 10:
6 declare("Equal")
7
8# Inequality
9foresee a != b:
10 declare("Not equal")
11
12# Greater/Less
13foresee a < b:
14 declare("a is smaller")
15
16foresee b >= 20:
17 declare("b is at least 20")
18
19# Logical operators
20foresee a > 5 and b < 30:
21 declare("Both conditions true")
22
23foresee a > 15 or b > 15:
24 declare("At least one true")
25
26# Negation
27foresee not (a == b):
28 declare("They differ")

Nested Conditionals

KEIKAKU Protocol
1user_role := "admin"
2user_active := true
3
4foresee user_active:
5 foresee user_role == "admin":
6 declare("Welcome, Administrator")
7 alternate user_role == "moderator":
8 declare("Welcome, Moderator")
9 otherwise:
10 declare("Welcome, User")
11otherwise:
12 declare("Account disabled")

Loops (cycle)

While Loops

KEIKAKU Protocol
1# Basic while loop
2count := 0
3cycle while count < 5:
4 declare("Count:", count)
5 count = count + 1
6# Output: 0, 1, 2, 3, 4
7
8# While with break condition
9attempts := 0
10cycle while attempts < 100:
11 attempts = attempts + 1
12 foresee random(1, 10) == 7:
13 declare("Lucky 7 found after", attempts, "attempts!")
14 terminate
15
16# Infinite loop (use with caution)
17cycle:
18 input := inquire("Enter 'quit' to exit: ")
19 foresee input == "quit":
20 terminate
21 declare("You entered:", input)

For-Each Loops (cycle through)

KEIKAKU Protocol
1# Iterate over a list
2fruits := ["apple", "banana", "cherry"]
3cycle through fruits as fruit:
4 declare("I like", fruit)
5
6# With index using enumerate pattern
7items := ["first", "second", "third"]
8index := 0
9cycle through items as item:
10 declare(index, ":", item)
11 index = index + 1
12
13# Nested iteration
14matrix := [[1, 2], [3, 4], [5, 6]]
15cycle through matrix as row:
16 cycle through row as cell:
17 declare(cell)
18
19# Iterating over dictionary keys
20config := {"host": "localhost", "port": 8080}
21# (Dictionary iteration depends on implementation)

Range Loops (cycle from...to)

KEIKAKU Protocol
1# Basic range (exclusive end)
2cycle from 1 to 5 as i:
3 declare(i)
4# Output: 1, 2, 3, 4
5
6# Range for counting
7cycle from 0 to 10 as n:
8 declare(n * n)
9# Output: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
10
11# Combining range with conditions
12cycle from 1 to 101 as n:
13 foresee n % 15 == 0:
14 declare("FizzBuzz")
15 alternate n % 3 == 0:
16 declare("Fizz")
17 alternate n % 5 == 0:
18 declare("Buzz")
19 otherwise:
20 declare(n)

Loop Control

KEIKAKU Protocol
1# Early termination with terminate
2cycle from 1 to 100 as i:
3 foresee i == 5:
4 declare("Stopping at 5")
5 terminate
6 declare(i)
7# Output: 1, 2, 3, 4, Stopping at 5
8
9# Skip iterations (simulated)
10cycle from 1 to 10 as i:
11 foresee i % 2 == 0:
12 # Skip even numbers by doing nothing
13 pass
14 otherwise:
15 declare("Odd:", i)
16# Output: Odd: 1, Odd: 3, Odd: 5, Odd: 7, Odd: 9

Special Constructs

Scheme Blocks (Deferred Execution)

Define a block of code that doesn't execute immediately, then trigger it later.

KEIKAKU Protocol
1# Define a scheme (doesn't run yet)
2scheme:
3 declare("Executing the master plan...")
4 result := compute_complex_operation()
5 declare("Plan complete:", result)
6
7# Do other setup...
8declare("Preparing environment...")
9prepare_resources()
10
11# Now execute the scheme
12execute
13
14# Output:
15# Preparing environment...
16# Executing the master plan...
17# Plan complete: ...

Absolute Assertions

Use absolute to assert conditions that MUST be true. Failure causes a runtime error.

KEIKAKU Protocol
1# Assert a condition
2value := 42
3absolute value > 0 # Passes silently
4
5# Failed assertion
6balance := -100
7absolute balance >= 0 # ERROR: Assertion failed
8
9# Use in validation
10protocol process_order(quantity):
11 absolute quantity > 0 # Guard condition
12 absolute quantity <= 1000 # Upper limit
13
14 declare("Processing", quantity, "items")
15 yield quantity * 10

Override (Force Set)

KEIKAKU Protocol
1# Override forces a value in global scope
2protocol set_config(key, value):
3 override config_key = key
4 override config_value = value
5
6set_config("mode", "production")
7declare(config_mode) # "production"

Practical Examples

Menu System

KEIKAKU Protocol
1protocol show_menu():
2 declare("\n=== Main Menu ===")
3 declare("1. New Game")
4 declare("2. Load Game")
5 declare("3. Settings")
6 declare("4. Exit")
7
8 choice := inquire("Select option: ")
9 yield number(choice)
10
11cycle:
12 option := show_menu()
13
14 foresee option == 1:
15 declare("Starting new game...")
16 # start_new_game()
17 alternate option == 2:
18 declare("Loading saved game...")
19 # load_game()
20 alternate option == 3:
21 declare("Opening settings...")
22 # show_settings()
23 alternate option == 4:
24 declare("Goodbye!")
25 terminate
26 otherwise:
27 declare("Invalid option, try again")

Data Processing Pipeline

KEIKAKU Protocol
1# Process a list with multiple transformations
2data := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3results := []
4
5cycle through data as item:
6 # Skip odd numbers
7 foresee item % 2 != 0:
8 # continue (skip)
9 otherwise:
10 # Transform: square even numbers
11 squared := item * item
12
13 # Filter: only keep if > 10
14 foresee squared > 10:
15 push(results, squared)
16
17declare("Processed results:", results)
18# Output: [16, 36, 64, 100]

Best Practices

  • 1. Prefer early returns - Check error conditions first, then happy path
  • 2. Avoid deep nesting - Extract nested conditions into protocols
  • 3. Use 'cycle through' over indices - More readable and less error-prone
  • 4. Add comments for complex conditions - Explain the "why"
  • 5. Use 'absolute' for invariants - Catch bugs early