Perl basics part 3: Control Statements

1 Comment

In this part of perl basics, we will going to learn about perl control statements such as if, elseif, and else statements etc. If, elseif, and else statements are conditional statements and are executed only if certain condition met.

If Conditional Statements

If statement syntax

$i = 10;

if ($i == 5){

if statements here..

} elseif ($i == 10){

elseif statements here..

} else {

else statements here..

}

In the above code snippet, elseif condition evaluates to true so statements under elseif block will get executed.

Unless Conditional Statements

Unless conditional statements are right opposite to if conditions. Unless conditional statements get executed only when certain condition evaluates to false, where as if statements are executed on true condition. And also unless can have else block just like if statements.

Unless syntax

unless (conditional_statement){

unless_statements_here;

} else {

else_statements_here;

}

Switch Statements

Switch statements are not built in perl core modules, you need to import switch module to use it. This is basically used for value-case match, where we have a switch scalar and different case statements and when switch scalar values matches to any of the case value then that particular case statements will get executed.

Switch statement

switch ($scalar_value) {

case [$case_value] { statements here..}

case[$case_value] { statements here.. }

else { else_statements here..}

}

When any case value matches with switch $scalar_value then that particular case statements are executed.


Browse Realted Articles

One Response to “Perl basics part 3: Control Statements”

satish says on Jul Sun, 2009 at 11:45:32 am

Hey bava,
Nice article ra, well written. Keep it up.
Write more articles on perl and python. I want to learn those. bye

Leave a Reply