Lesson 07 of 55
8 mins readPython Conditional Branching: if, elsif & else
In Plain English
In Perl, `if`, `elsif`, and `else` blocks require curly braces `{}` around every branch, even for single statements.
Deep Dive: How It Works
Block Requirement: Unlike C or Java, braces `{}` are mandatory around all `if` bodies.
Spelling: The keyword is `elsif`, not `else if` or `elif`.
Boolean Context: Evaluates condition using Perl truth rules (0, "0", "", undef are false).
Core Rules to Remember

Mandatory Braces: Always enclose conditional body code in `{ ... }`.

Spelling is elsif: Use `elsif` for subsequent branches.
Live Interactive Example
Hit Run Code to see it liveGrading Logic with if-elsif-else
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: 85 satisfies the condition ($score >= 80).
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Check Threshold Condition
Set `my $cpu_load = 92;`
If `$cpu_load > 90`, print `"Warning: High Load\n"`. Otherwise, print `"Load Normal\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.