Control Structures
Control structures are the decision‑making parts of a program. They let your code ask questions, choose between different paths, and decide what to do next. Think of them like traffic signals or signposts that guide your program’s flow.
“If I finish my homework, then I can play. Otherwise, I must study.”
Why Are They Important?
Without control structures, a program would simply run every line in order, with no ability to adapt or react. In real‑world tasks we often need to:
- Make decisions: “If the user is logged in, show the dashboard; otherwise, show the login screen.”
- Handle multiple choices: “Choose a difficulty level from Easy, Medium, or Hard.”
- Build complex logic: “If both A and B are true, do X; if only one is true, do Y.”
Control structures turn a static list of commands into a dynamic, intelligent process.
Control Flow Diagram
ASCII Diagram – IF Decision
+-------------------+
| Start |
+-------------------+
|
v
[Check Condition]
/ \
True False
| |
Action 1 Action 2
| |
+------+------+
|
End
1. If Statement
Use an if
statement to run a block of code only when a condition is true.
How it works
- Evaluate the condition.
- If the condition is true, execute the code inside the
if
block. - If the condition is false, skip the block and continue.
Syntax
if condition then
// code to run when condition is true
end if
Examples
Without parentheses
set temperature to 30
if temperature > 25 then
show("It's hot today")
end if
With parentheses
set temperature to 20
if (temperature <= 25) then
show("It's cool today")
end if
Note: You can use
set
without a priordeclare
.
2. If‑Else
An if-else
statement lets you choose between two blocks:
- The if block runs when the condition is true.
- The else block runs when the condition is false.
Syntax
if condition then
// code if true
else
// code if false
end if
Example
set age to 16
if age >= 18 then
show("Adult")
else
show("Minor")
end if
3. Else If
Use else if
when you have multiple conditions to check in order:
- Check the first
if
. - If that is false, check each
else if
in sequence. - If none match, an optional
else
block can handle all other cases.
Syntax
if condition1 then
// code for condition1
else if condition2 then
// code for condition2
else
// fallback code
end if
Example
set score to 70
if score >= 90 then
show("Excellent")
else if score >= 80 then
show("Very Good")
else if score >= 70 then
show("Good")
else
show("Needs Improvement")
end if
4. Nested If
A nested if places one if
statement inside another. Use this when you need to make a second decision only if the first condition is true.
Syntax
if conditionA then
if conditionB then
// code when both A and B are true
else
// code when A is true but B is false
end if
else
// code when A is false
end if
Example
set userType to "admin"
set status to "active"
if userType == "admin" then
if status == "active" then
show("Admin access granted")
else
show("Admin account inactive")
end if
else
show("Standard user access")
end if
5. Choose / When / Otherwise
choose
is a multi‑branch structure (similar to switch‑case) for matching one variable against many possible values. Use when
for each case and otherwise
for the default. Place a colon (:
) after each when
or otherwise
.
Syntax
choose variable
when value1:
// code for value1
when value2:
// code for value2
otherwise:
// code if no cases match
end choose
Example
set color to "green"
choose color
when "red":
show("Stop")
when "yellow":
show("Caution")
when "green":
show("Go")
otherwise:
show("Unknown signal")
end choose
6. Comparison Operators
Comparison operators test relationships between values and return true
or false
.
- Equality:
==
oris
- Inequality:
!=
oris not
- Greater / Less:
>
,<
,>=
,<=
- Membership:
is in
Example
set role to "admin"
if role == "admin" then
show("Welcome, Admin")
end if
if role is not "guest" then
show("Access granted")
end if
Summary Table
Structure | Syntax Example | Purpose |
---|---|---|
if | if cond then … end if | Run block if condition is true |
if with () | if (cond) then … end if | Alternative, explicit style |
if-else | if … else … end if | Two‑way decision |
else if | if … else if … else … end if | Multi‑way decision |
Nested if | if … then if … end if end if | Decision inside another decision |
choose | choose var when val: … otherwise: … end | Clean multi‑case selection |
== / is | if x == y or if x is y | Equality comparison |
!= / is not | if x != y or if x is not y | Inequality comparison |
> / < / >= / <= | if x > y , etc. | Relational comparisons |
is in | "a" is in list | Membership test |
Control structures are the foundation for making your programs dynamic, responsive, and intelligent. Master these tools to write code that can think and adapt to any situation.