Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Conditional statements: if

  • if
  • ==
  • indentation

Sometimes based on some condition a piece of code has to be executed or not.

#!/usr/bin/perl
use strict;
use warnings;

print "First number: ";
my $x = <STDIN>;
chomp $x;

print "Second number: ";
my $y = <STDIN>;
chomp $y;

if ($y == 0) {
    print "Cannot divide by zero\n";
} else {
    my $z = $x / $y;
    print "The result is $z\n";
}