Ad space (banner)
Lesson 4 / 20

Branching (the if statement)

This lesson covers how to write an if statement in Perl.

if / elsif / else branches on a condition. Note that unlike the else if of most other languages, Perl writes it as a single word: elsif.

The sample code uses if / elsif / else to print one of three messages depending on $score.

A common early stumble is spelling elsif. Writing it as two words, or as "elseif", is an error — Perl's own spelling is worth committing to memory.

In professional work, if statements come up constantly for branching on the contents of a log line.

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

๐Ÿงช This site can't compile or run Perl 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)