beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

if-elsif-else statement in perl

By Chaitanya Singh | Filed Under: Perl

if-elsif-else statement is used when we need to check multiple conditions. In this statement we have only one “if” and one “else”, however we can have multiple “elsif”. This is how it looks:

if(condition_1) {
   #if condition_1 is true execute this
   statement(s);
}
elsif(condition_2) {
   #execute this if condition_1 is not met and
   #condition_2 is met
   statement(s);
}
elsif(condition_3) {
   #execute this if condition_1 & condition_2 are
   #not met and condition_3 is met
   statement(s);
}
.
.
.
else {
   #if none of the condition is true
   #then these statements gets executed
   statement(s);
}

Note: The most important point to note here is that in if-elsif-else statement as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.

Example:

#!/usr/local/bin/perl

printf "Enter any integer between 1 & 99999:";
$num = <STDIN>;
if( $num <100 && $num>=1) {
   printf "Its a two digit number\n";
}
elsif( $num <1000 && $num>=100) {
   printf "Its a three digit number\n";
}
elsif( $num <10000 && $num>=1000) {
   printf "Its a four digit number\n";
}
elsif( $num <100000 && $num>=10000) {
   printf "Its a five digit number\n";
}
else {
   printf "Please enter number between 1 & 99999\n";
}

Output:

Enter any integer between 1 & 99999:8019
Its a four digit number

Enjoyed this post? Try these related posts

  1. Loops and loop control statements in Perl
  2. Conditional Statements in Perl
  3. Perl – Lists and Arrays
  4. Subroutines in Perl
  5. my keyword – Local and global variables in Perl
  6. For loop in Perl with example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap