Lesson 07 of 55
9 mins readPython Conditional Expressions (if then else)
In Plain English
Unlike imperative statements, if-then-else in Haskell is a pure expression that evaluates to a value. Because every expression must return a value, the else branch is mandatory.
Deep Dive: How It Works
Expression Nature: if condition then val1 else val2 returns either val1 or val2. Both branches must have identical types.
Mandatory else: You cannot omit else because the entire if-then-else construct must evaluate to a valid value.
Inline Composability: Can be placed anywhere an expression is expected (e.g. 1 + (if True then 10 else 20)).
Core Rules to Remember

Mandatory else Branch: Because if is an expression returning a value, both then and else are required.

Type Uniformity: Both then and else branches must evaluate to the exact same type.
Live Interactive Example
Hit Run Code to see it liveConditional Evaluation
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: checkPass returns "Passed" for score >= 60; inline if evaluates to 50.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Evaluate Absolute Value
Define `absVal x = if x < 0 then -x else x`.
Print `"Absolute of -15: "` followed by `show (absVal (-15))`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.