BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

If statement in Perl

Last Updated: February 7, 2017 by Chaitanya Singh | Filed Under: Perl

If statement consists a condition, followed by statement or a set of statements as shown below:

if(condition){
  Statement(s);
}

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.

Example:

In the following example, we have an integer value assigned to variable “num”. Using if statement, we are checking whether the value assigned to num is less than 100 or not.

#!/usr/local/bin/perl

printf "Enter any number:";
$num = <STDIN>;
if( $num < 100 ){
   # This print statement would execute,
   # if the above condition is true
   printf "num is less than 100\n";
}

Output:

Enter any number:78
num is less than 100

Nested if statement in perl

When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the conditions( condition_1 and condition_2) are true.

Example:

#!/usr/local/bin/perl

printf "Enter any number: ";
$num = <STDIN>;
if( $num < 100 ){
   printf "num is less than 100\n";

   if( $num > 90 ){
      printf "num is greater than 90\n";
   }
}

Output:

Enter any number: 99
num is less than 100
num is greater than 90

Top Related Articles:

  1. Installing Perl on Windows, Mac, Linux and Unix
  2. Perl Variables
  3. Perl Tutorial for beginners
  4. Until loop in Perl with example
  5. If else statement in Perl

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap