Keikaku provides expressive control structures for decision-making and iteration, using intuitive keywords like foresee, cycle, and otherwise.
foresee - ifalternate - else ifotherwise - elsecycle while - while loopcycle through - for eachcycle from X to Y - rangeUse foresee for if-statements. Chain with alternate for else-if branches, and otherwise for the default case.
1# Simple if2temperature := 253foresee temperature > 30:4 declare("It's hot!")56# If-else7age := 208foresee age >= 18:9 declare("Adult")10otherwise:11 declare("Minor")1213# If-else if-else chain14score := 8515foresee 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: B1a := 102b := 2034# Equality5foresee a == 10:6 declare("Equal")78# Inequality9foresee a != b:10 declare("Not equal")1112# Greater/Less13foresee a < b:14 declare("a is smaller")1516foresee b >= 20:17 declare("b is at least 20")1819# Logical operators20foresee a > 5 and b < 30:21 declare("Both conditions true")2223foresee a > 15 or b > 15:24 declare("At least one true")2526# Negation27foresee not (a == b):28 declare("They differ")1user_role := "admin"2user_active := true34foresee 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")1# Basic while loop2count := 03cycle while count < 5:4 declare("Count:", count)5 count = count + 16# Output: 0, 1, 2, 3, 478# While with break condition9attempts := 010cycle while attempts < 100:11 attempts = attempts + 112 foresee random(1, 10) == 7:13 declare("Lucky 7 found after", attempts, "attempts!")14 terminate1516# Infinite loop (use with caution)17cycle:18 input := inquire("Enter 'quit' to exit: ")19 foresee input == "quit":20 terminate21 declare("You entered:", input)1# Iterate over a list2fruits := ["apple", "banana", "cherry"]3cycle through fruits as fruit:4 declare("I like", fruit)56# With index using enumerate pattern7items := ["first", "second", "third"]8index := 09cycle through items as item:10 declare(index, ":", item)11 index = index + 11213# Nested iteration14matrix := [[1, 2], [3, 4], [5, 6]]15cycle through matrix as row:16 cycle through row as cell:17 declare(cell)1819# Iterating over dictionary keys20config := {"host": "localhost", "port": 8080}21# (Dictionary iteration depends on implementation)1# Basic range (exclusive end)2cycle from 1 to 5 as i:3 declare(i)4# Output: 1, 2, 3, 456# Range for counting7cycle from 0 to 10 as n:8 declare(n * n)9# Output: 0, 1, 4, 9, 16, 25, 36, 49, 64, 811011# Combining range with conditions12cycle 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)1# Early termination with terminate2cycle from 1 to 100 as i:3 foresee i == 5:4 declare("Stopping at 5")5 terminate6 declare(i)7# Output: 1, 2, 3, 4, Stopping at 589# Skip iterations (simulated)10cycle from 1 to 10 as i:11 foresee i % 2 == 0:12 # Skip even numbers by doing nothing13 pass14 otherwise:15 declare("Odd:", i)16# Output: Odd: 1, Odd: 3, Odd: 5, Odd: 7, Odd: 9Define a block of code that doesn't execute immediately, then trigger it later.
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)67# Do other setup...8declare("Preparing environment...")9prepare_resources()1011# Now execute the scheme12execute1314# Output:15# Preparing environment...16# Executing the master plan...17# Plan complete: ...Use absolute to assert conditions that MUST be true. Failure causes a runtime error.
1# Assert a condition2value := 423absolute value > 0 # Passes silently45# Failed assertion6balance := -1007absolute balance >= 0 # ERROR: Assertion failed89# Use in validation10protocol process_order(quantity):11 absolute quantity > 0 # Guard condition12 absolute quantity <= 1000 # Upper limit13 14 declare("Processing", quantity, "items")15 yield quantity * 101# Override forces a value in global scope2protocol set_config(key, value):3 override config_key = key4 override config_value = value56set_config("mode", "production")7declare(config_mode) # "production"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)1011cycle: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 terminate26 otherwise:27 declare("Invalid option, try again")1# Process a list with multiple transformations2data := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]3results := []45cycle through data as item:6 # Skip odd numbers7 foresee item % 2 != 0:8 # continue (skip)9 otherwise:10 # Transform: square even numbers11 squared := item * item12 13 # Filter: only keep if > 1014 foresee squared > 10:15 push(results, squared)1617declare("Processed results:", results)18# Output: [16, 36, 64, 100]