Perl: if then else

By Xah Lee. Date: . Last updated: .

Example of “if” statement:

#-*- coding: utf-8 -*-
# perl

# example of if
$x = 1;
if ($x == 1) { print "yes\n"; }

Example of “if else”:

#-*- coding: utf-8 -*-
# perl

# example of if else

$y = 2;
if ($y == 1) {
  print "yes\n";
} else {
  print "no\n";
}

Example of “if else” chain:

#-*- coding: utf-8 -*-
# perl

# example of if else chain

$z = 2;
if ($z < 0) {
  print 'neg';
} elsif ($z == 0) {
  print 'zero';
} elsif ($z == 1) {
  print 'one';
} else {
  print 'other';
}

perldoc perlsyn

Ask me question on patreon