codescracker


perl

Perl If-Else and Switch



« Previous Tutorial Next Tutorial »


Sometimes you need to evaluate one or more conditions in your perl program. Perl provide following types of conditional or decision making statements:

Perl if statement

Perl if statement, consists of a boolean expression, followed by one or more statement(s). Here is the general form to use if statement in perl:

if(boolean_expression)
{
	# statement(s)
}

Example

Here is an example, illustrates if statement in perl:

#!/usr/local/bin/perl
		
$num = 5;
if($num<10)
{
	print("num is less than 10\n");
}

print("Exiting...\n");

Here is the sample output of the above perl program:

num is less than 10
Exiting...

Here is another perl program, also illustrates the concept and use of if statement in perl:

#!/usr/local/bin/perl
		
$num = 5;
if($num<2)
{
	print("num is less than 10\n");
}

print("Exiting...\n");

Here is the sample output of the above perl program:

Exiting...

Perl if-else Statement

Perl if-else statement is similar to perl if statement. But in if-else statement, there is an else statement, which is to be executed when the boolean expression is evaluated to be false. Here is the general form to use if-else statement in perl:

if(boolean_expression)
{
	# statement(s), if boolean_expression evaluate to be true
}
else
{
	# statement(s), if boolean_expression evaluate to be false
}

Example

Here is an example, illustrates if-else statement in perl:

#!/usr/local/bin/perl
		
$num = 5;
if($num<10)
{
	print("num is less than 10\n");
}
else
{
	print("num is not less than 10\n");
}

print("Exiting...\n");

Here is the sample output of the above perl program:

num is less than 10
Exiting...

Here is another perl program, also illustrates the concept and use of if-else statement in perl:

#!/usr/local/bin/perl
		
$num = 5;
if($num<2)
{
	print("num is less than 10\n");
}
else
{
	print("num is not less than 10\n");
}

print("Exiting...\n");

Here is the sample output of the above perl program:

num is not less than 10
Exiting...

Perl switch Statement

Perl switch statement allows a simple way of comparing a variable value against some various conditions. Here is the general form to use switch statement in perl:

use Switch;
switch(argument)
{
	case 1				{print("number 1")}
	case "a"			{print("string a")}
	case [1..10, 42]		{print("number in the list")}
	case (\@array)			{print("number in the list")}
	case /\w+/			{print("pattern")}
	case qr/\w+/			{print("pattern")}
	case (\%hash)			{print("entry in the hash")}
	case (&sub)			{print("arg to subroutine")}
	else				{print("previous case not true")}
}

A switch statement in perl, takes a single scalar argument of any type that is specified inside the parentheses. The value is followed by a block, that may contain one/more case statement followed by a block of perl statement(s).

Example

#!/usr/local/bin/perl
		
use Switch;

$num = 10;
@array = (10, 20, 30);
%hash = ('key1' => 10, 'key2' => 20);

switch($num)
{
	case 10				{print("number 10\n")}
	case "a"			{print("string a")}
	case [1..10, 42]	{print("number in the list")}
	case (\@array)		{print("number in the list")}
	case (\%hash)		{print("entry in hash")}
	else				{print("previous case is not true")}
}

Here is the sample output of the above perl program:

number 10

Perl unless Statement

Perl unless statement also consists of a boolean expression which is followed by one/more statement(s).

Perl unless-else Statement

Perl unless-else statement is similar to the perl unless statement. But in this case, there is an else statement, executes when the expression evaluates to be false.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test