Conditional logic lets a program choose what to do based on a condition. A Boolean expression evaluates to `true` or `false`: `8 > 3` is true, while `3 == 5` is false. Combine checks with `&&` (“and”) or `||` (“or”) when one comparison is not enough. section 3.0 section 3.2 section 3.2.1
Give recursion a stopping point
A factorial function that only calls `fac(n-1)` keeps calling itself until it fails. An `if` check can stop it: `function fac(n) if n <= 2 return n end n*fac(n-1) end` This handles positive inputs, but `fac(0)` incorrectly returns `0`. The condition solves the stopping problem without yet handling every input. section 3.6 section 3.7 section 3.7.1
Use `else` when there are two possible paths. In Julia, the entire `if`–`else` construct is an expression: its value comes from the branch that runs. For example, `y = if x > 3; 6; else; 4; end` assigns `6` when `x` is `4`. section 3.7.1
For more than two cases, add `elseif` checks:
- Start with exactly one `if`. section 3.7.2
- Add as many `elseif` clauses as needed; checks proceed until one is true. section 3.7.2
- Optionally finish with one `else` for cases no earlier condition accepts. section 3.7.2
Listing 3.16 If-else statement
function fac(n)
if n > 2
n*fac(n-1)
elseif n > 0
n
elseif n == 0 #1
1
else
err = DomainError(n, "`n` must not be negative.") #2
throw(err) #3
end
end
The completed factorial function uses separate branches for recursive multiplication, small positive inputs, zero, and a `DomainError` for negative inputs. section 3.7.2
Conditions also govern repetition: a `while` loop runs while its condition remains true, whereas an `if` chooses a branch. Together, these forms let a calculation respond to its inputs rather than follow one fixed path. section 3.3 section 3.0




















