Decisions and loops
A program becomes useful when it can choose an action and repeat work.
A decision runs code only when a condition is true. A loop runs a group of instructions more than once.
Set age to 20.
If age is at least 18.
Show "You can continue.".
Else.
Show "You cannot continue yet.".
End.Read an If block like a normal sentence: if this condition is true, do this work.
Make a decision
Start a decision with If and close it with End.:
Set signed_in to true.
If signed_in equals true.
Show "Welcome back.".
End.The indented instruction runs only when signed_in equals true.
Indentation makes the block easier to read. End. gives the block its real boundary.
Choose between two actions
Use Else. when one action should run for a true condition and another should run for a false condition:
Set age to 16.
If age is at least 18.
Show "Adult".
Else.
Show "Minor".
End.Only one side runs:
- the first block runs when the condition is true
- the
Else.block runs when the condition is false
Otherwise. is also a readable form:
Set ready to false.
If ready equals true.
Show "Starting now.".
Otherwise.
Show "Not ready yet.".
End.Use one style consistently inside a project.
Compare equal values
Use equals when two values should be the same:
Set command to "start".
If command equals "start".
Show "The program is starting.".
End.Use does not equal when the values should be different:
Set status to "waiting".
If status does not equal "finished".
Show "Work is still running.".
End.Text comparisons should use the exact value your program expects.
Compare numbers
Creole provides readable number comparisons:
is greater thanis less thanis at leastis at mostequalsdoes not equal
Examples:
Set score to 82.
If score is greater than 80.
Show "High score".
End.
If score is at least 50.
Show "Passed".
End.
If score is at most 100.
Show "Valid score".
End.Choose the comparison that matches the boundary you need.
is greater than 18 does not include 18. is at least 18 includes 18.
Check text content
Use contains when you need to know whether text includes another piece of text:
Set message to "Welcome to Creole".
If message contains "Creole".
Show "Language name found.".
End.This is useful for simple input checks and text processing.
Use truth values directly
A variable can hold true or false:
Set has_access to true.
If has_access.
Show "Access allowed.".
Else.
Show "Access denied.".
End.This reads naturally because has_access already represents a condition.
Choose truth-value names that sound clear inside an If instruction:
is_ready
has_access
request_finished
file_foundCombine conditions
Use and when every condition must be true:
Set age to 20.
Set has_ticket to true.
If age is at least 18 and has_ticket equals true.
Show "Entry allowed.".
End.Use or when either condition can be true:
Set role to "editor".
If role equals "admin" or role equals "editor".
Show "Editing is allowed.".
End.Keep combined conditions short. If a condition becomes difficult to read, save smaller results under clear names first.
Make decisions in steps
Clear intermediate values can make a decision easier to understand:
Set age to 24.
Set account_active to true.
Set old_enough to age is at least 18.
If old_enough and account_active.
Show "The account can continue.".
Else.
Show "The account cannot continue.".
End.A readable program is usually better than one very long condition.
Put a decision inside a decision
A decision can contain another decision:
Set signed_in to true.
Set role to "admin".
If signed_in.
Show "Signed in.".
If role equals "admin".
Show "Admin tools are ready.".
Else.
Show "Standard tools are ready.".
End.
Else.
Show "Please sign in.".
End.Each opened block needs its own End.. Indentation helps you see which End. belongs to which block.
Do not add deep nesting unless it is needed. Small functions and clear variables are often easier to maintain.
Repeat an action
Use Repeat when work should run a known number of times:
Repeat 3 times.
Show "Hello from Creole".
End.The block runs three times and then the program continues after End..
Use the repeat count
Inside a Repeat block, count gives the current repetition number:
Repeat 3 times.
Show count.
End.The output is:
1
2
3Use count when each repetition needs a number:
Repeat 5 times.
Show "Running step " followed by count.
End.Calculate during a loop
A loop can update a saved value:
Set total to 0.
Repeat 4 times.
Set total to total plus count.
End.
Show total.The loop adds 1, 2, 3, and 4 to total.
Follow the value one repetition at a time when checking loop behavior.
Read every item
Use For each to process every value in a list:
Create an empty list called names.
Add "Asha" to names.
Add "Maya" to names.
Add "Noah" to names.
For each name in names.
Show name.
End.On each repetition, name contains the next value from names.
Use a singular name for one item and a plural name for the complete list:
For each file in files.
Show file.
End.This makes the instruction easy to read.
Lists and records are explained fully in the next chapter.
Make a decision for every item
A For each block can contain an If block:
Create an empty list called scores.
Add 80 to scores.
Add 42 to scores.
Add 91 to scores.
For each score in scores.
If score is at least 50.
Show "Passed".
Else.
Show "Try again".
End.
End.The inner End. closes the decision. The final End. closes the loop.
Repeat project work carefully
Loops can run file, request, or data instructions many times. Before using a large repeat number:
- test the block with a small number
- check which values change
- avoid sending unnecessary API requests
- confirm that files are not overwritten by mistake
- make sure the loop has a clear purpose
Start with:
Repeat 2 times.
Show count.
End.Do not begin with hundreds of repetitions until the work is tested.
Common decision problems
Using = instead of readable comparison words
Incorrect:
If age = 18.Correct:
If age equals 18.Creole uses readable comparison words instead of symbolic comparison syntax.
Forgetting End.
Incorrect:
If ready equals true.
Show "Ready".Correct:
If ready equals true.
Show "Ready".
End.The checker reports an open block when End. is missing.
Placing Else. after End.
Incorrect:
If ready equals true.
Show "Ready".
End.
Else.
Show "Waiting".
End.Correct:
If ready equals true.
Show "Ready".
Else.
Show "Waiting".
End.Else. belongs inside the decision, before its End..
Repeating one time too many
Use the exact number of repetitions you need and remember that count starts at 1.
Making conditions difficult to read
Do not place many unrelated checks in one line. Save smaller results under meaningful names and check those names.
Practice program
Create decisions.crlo:
Ask "What is your name? " and store the answer in name.
Set lessons_finished to 3.
Set learning_active to true.
If learning_active and lessons_finished is at least 3.
Show "Great progress, " followed by name.
Else.
Show "Keep learning, " followed by name.
End.
Repeat 3 times.
Show "Practice step " followed by count.
End.Check the file:
creole --check decisions.crloRun it:
creole decisions.crloChange lessons_finished and learning_active, then run the program again to see a different decision.
Learning checklist
- I can create an
Ifblock. - I can choose another action with
Else.. - I can compare text and numbers.
- I understand
is greater thanandis at least. - I can use a true or false variable as a condition.
- I can combine conditions with
andoror. - I can repeat work a known number of times.
- I can use
countinside a repeat block. - I can read every value with
For each. - I close every block with
End..
Next step
You can now make choices, compare values, repeat instructions, and process each item in a collection.
Continue to Lists and records to learn how Creole keeps multiple values and structured information together.