if statement in perl

htt‮sp‬://www.theitroad.com

In Perl, the if statement is used for conditional branching. It allows you to execute a block of code only if a certain condition is true. The basic syntax of the if statement is:

if (condition) {
    # Code to execute if condition is true
}

Here, condition is a Boolean expression that is evaluated to either true or false. If the condition is true, the code block enclosed in curly braces {} is executed. If the condition is false, the code block is skipped and the program continues executing from the next statement.

For example, the following code checks whether a given number is even or odd using the if statement:

my $num = 10;

if ($num % 2 == 0) {
    print "$num is even\n";
} else {
    print "$num is odd\n";
}

In this code, the condition $num % 2 == 0 checks whether the remainder of $num divided by 2 is equal to 0, which indicates that $num is even. If the condition is true, the code block print "$num is even\n"; is executed, which prints a message indicating that the number is even. If the condition is false, the code block print "$num is odd\n"; is executed, which prints a message indicating that the number is odd.