Control flow is how your program makes decisions. It allows code to run only when certain conditions are met, or to repeat tasks.
The valid syntax is if <condition>, followed by an indented block of code.
score = 85
if score > 50
say "You passed!"
elif score == 50
say "Just barely made it."
else
say "Please try again."| Keyword | Description |
|---|---|
if |
Execute block if condition is true |
elif |
Execute if previous conditions were false and this is true |
else |
Execute if all previous conditions were false |
then |
Optional keyword after condition (for readability) |
do |
Optional keyword after condition (for readability) |
You can use English words instead of math symbols if you prefer.
| Symbol | English Phrase |
|---|---|
== |
is, is exactly, equals |
!= |
is not |
>= |
is at least, is more than or equal to |
<= |
is at most, is less than or equal to |
> |
is more than, is greater than |
< |
is less than |
Example:
if the age is at least 18
say "Adult"
if score is more than 100
say "High score!"
if name is "Alice"
say "Welcome, Alice!"unless is the opposite of if. It runs only if the condition is no (false).
tired = no
unless tired
say "Let's keep coding!"This is equivalent to:
if not tired
say "Let's keep coding!"If you have a long chain of if/else, when is cleaner. It looks like a "switch" statement in other languages.
day = "Monday"
when day
is "Saturday"
say "Weekend!"
is "Sunday"
say "Rest day."
is "Monday"
say "Back to work."
otherwise
say "Just another day."The otherwise clause is optional and acts as a default case when no other conditions match.
Loops let you repeat code.
The simplest loop. Runs a block a specific number of times.
repeat 3 times
say "Hip Hip Hooray!"You can also use it without times:
repeat 5
say "Hello"Runs while a condition is true.
count = 5
while count > 0
say count
count = count - 1Natural language alternative:
doing as long as count > 0
say count
count -= 1Runs until a condition becomes true (opposite of while).
battery = 0
until battery == 100
say "Charging..."
battery += 10Iterates over a list or a range of numbers.
Range:
for i in range 1 5
say i
# Prints: 1, 2, 3, 4, 5Range with step:
for i in range 0 10 2
say i
# Prints: 0, 2, 4, 6, 8, 10Lists:
colors = ["red", "blue", "green"]
for color in colors
say colorDictionaries:
user = {"name": "Alice", "age": 30}
for key in user
say key + ": " + str(user[key])Alternative natural syntax:
for each item in items
say itemSometimes you want a program to run indefinitely, for example, a server or a background watcher.
forever
say "I will run until stopped manually."
wait 1 secondUse stop or break to exit a forever loop when needed.
| Keyword | Alias | Description |
|---|---|---|
stop |
break |
Exits the loop immediately |
skip |
continue |
Skips the rest of the current iteration |
repeat 10 times
if something_bad_happened
stop # Exit the loop
for i in range 1 10
if i == 5
skip # Skip printing 5
say iReal world programs encounter errors. You can handle them gracefully using try, catch, and always (or finally).
try
# Risky code
result = 10 / 0
catch error
# This runs if there is an error
say in red "Oops! Something went wrong: " + error
always
# This runs no matter what (success or error)
say "Cleanup complete."| Keyword | Alias | Description |
|---|---|---|
try |
Begin a block that might throw an error | |
catch |
Handle errors if they occur | |
always |
finally |
Always execute, regardless of error |
try
try
risky_operation()
catch inner_error
say "Inner error handled"
catch outer_error
say "Outer error caught"To stop your program completely at any point, use exit.
if critical_system_failure
say in red "Shutting down!"
exitSometimes you need a placeholder for code you haven't written yet. While ShellLite doesn't have a formal pass keyword, you can use an empty block or a comment:
if condition
# TODO: implement this laterShellLite supports inline conditional expressions:
status = "adult" if age >= 18 else "minor"| Statement | Purpose |
|---|---|
if/elif/else |
Conditional branching |
unless |
Negated conditional |
when/is/otherwise |
Pattern matching |
repeat N times |
Fixed iteration count |
while |
Loop while condition is true |
until |
Loop until condition becomes true |
for X in Y |
Iterate over collection |
forever |
Infinite loop |
stop/break |
Exit loop |
skip/continue |
Skip to next iteration |
try/catch/always |
Error handling |
exit |
Terminate program |