liveBook · Manning

Conditional logic

As explained in Julia as a Second Language

Conditional execution is the execution of code only when a specified logical condition holds.

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

How Julia as a Second Language covers it

How deep it goes
Works through Boolean checks, `if`, `else`, and ordered `elseif` branches, including the value an executed branch produces.
Examples
Shows REPL comparisons and branch values, then develops a factorial function that distinguishes positive, zero, and negative inputs.
What it assumes
Introduces Boolean comparisons before applying them; defines factorial and develops the recursive function through examples.
What you can do after
Build it from scratch: write a Julia factorial function with a stopping case, a zero case, and an error for negative inputs.
How it connects
Connects Boolean expressions and logical operators to recursion, while loops, and domain errors.

Also explained in

Compare how 21 books explain conditional logic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.