Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 4 / 68

Branching (the if statement)

This lesson covers how to write an if statement in Julia, with the aim of being able to branch on a condition.

if / elseif / else / end branches on a condition. It helps to picture it as translating an everyday judgement — "if it is raining, take an umbrella" — into a program.

The sample code uses if / elseif / else to print one of three messages depending on score, closing with end. Marking the extent of a block with end rather than braces is characteristic of Julia.

A common early stumble is forgetting that end. If you are used to a brace language, it is easy to leave it out and get an error.

In professional work, the if statement is a basic construct used in practically every program — validating input, switching on state.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)