Skip to main content

Perl Control Statement

Control statement are used to manage execution flow of program during run time. Syntax of this statement are predefined, but conditional condition are based on logical expression. Conditional statement are capable to bind single or multiple instruction. This instruction are executed when logical expression are valid.

There are following conditional statement are available in perl programming.

if statement

That is simple and selection based statement that is depending upon given expression condition. Because condition is decide the which statements allowed to the current execution. Generally when condition are true or given expression is not in 0 (zero). Then this statement are works. if-statement are define by given syntax.

if(expression){
#when expression is valid
}

if-else statement

This statement are relative to if-statement, That statement are test when if-statement produce 0 values. We can divide this statement into two section.

if(conditional-expression){
	#define conditional statement here
}
elsif(conditional-expression){
	#define conditional statement here
}
elsif(conditional-expression){
	#define conditional statement here
}
elsif(conditional-expression){
	#define conditional statement here
}
else{

	#This is an optional when associate 
	#all above conditions are not work 
}

elsif statements : When a if-statement are not work and we are need to test other associate condition then this statement are helpful.

else statements : else is an optional block, that are mainly use when if-statement are not work and we are need to execute other statement in else-block.

unless statement

This is an another conditional statement which is work on 0 value. When if given expression result are produce 0 then this statement are works. In other word this is an opposite to if-statement.

#!/usr/bin/perl
use warnings;
use strict;


my $age = 17 ;

unless ($age > 18) {
	#when expression is not valid 
	
	print "Your age is less than 18";
}

unless elsif else statement

#!/usr/bin/perl
use warnings;
use strict;


my $age = 65 ;

unless ($age > 18) {
	#when expression is not valid 

	print "Your age is less than 18";
}
elsif($age>=18 and $age<60){

	print "You are Young";
}
elsif($age>=60){
	print "Senior citizen";
}
else{
	print "Undefine";
}
Senior citizen

switch statement

Switch case are used to check multiple test case of single value and pattern. Inside the body of switch define test condition. If condition is match by given expression then executs case block statements.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment