Perl - Conditional
Statemnts - IF...ELSE
Perl conditional
statements helps in decision making which require the programmer specifies one
or more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.
Following is the
general from of a typical decision making structure found in most of the
programming languages:
The number 0, the
strings '0' and "" , the empty list () , and undef are all falsein
a boolean context and all other values are true. Negation of a true
value by ! or not returns a special false
value.
Perl programming
language provides following types of conditional statements. Click the
following links to check their detail.
|
Statement
|
Description
|
|
An if
statement consists of a boolean expression followed by one or more
statements.
|
|
|
An if
statement can be followed by an optional else statement.
|
|
|
An if
statement can be followed by an optional elsif statement and
then by an optional else statement.
|
|
|
An unless
statement consists of a boolean expression followed by one or more
statements.
|
|
|
An unless
statement can be followed by an optionalelse statement.
|
|
|
An unless
statement can be followed by an optionalelsif statement and
then by an optional else statement.
|
|
|
With latest
versions of Perl, you can make use of switchstatment which allows
a simple way of comparing a variable value against various conditions.
|
The ? : Operator
Let's check conditional
operator ? : which can be used to replace if...else statements.
It has the following general form:
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and
Exp3 are expressions. Notice the use and placement of the colon.
The value of a ?
expression is determined like this: Exp1 is evaluated. If it is true, then Exp2
is evaluated and becomes the value of the entire ? expression. If Exp1 is
false, then Exp3 is evaluated and its value becomes the value of the expression.
Below is a simple example making use of this operator:
#!/usr/local/bin/perl
$name = "Ali";
$age = 10;
$status = ($age > 60 )? "A senior citizen" : "Not a senior
citizen";
print "$name is - $status\n";
This will produce
following result:
Ali is - Not a senior citizen
Perl - Loops
There may be a
situation when you need to execute a block of code several number of times. In
general statements are executed sequentially: The first statement in a function
is executed first, followed by the second, and so on.
Programming languages
provide various control structures that allow for more complicated execution
paths.
A loop statement
allows us to execute a statement or group of statements multiple times and
following is the general from of a loop statement in most of the programming
languages:
Perl programming
language provides following types of loop to handle looping requirements. Click
the following links to check their detail.
|
Loop Type
|
Description
|
|
Repeats a
statement or group of statements while a given condition is true. It tests
the condition before executing the loop body.
|
|
|
Repeats a
statement or group of statements until a given condition becomes true. It
tests the condition before executing the loop body.
|
|
|
Execute a
sequence of statements multiple times and abbreviates the code that manages
the loop variable.
|
|
|
The foreach loop
iterates over a normal list value and sets the variable VAR to be each
element of the list in turn.
|
|
|
Like a while
statement, except that it tests the condition at the end of the loop body
|
|
|
You can use one
or more loop inside any another while, for or do..while loop.
|
Loop Control Statements:
Loop control
statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.
C supports the
following control statements. Click the following links to check their detail.
|
Control Statement
|
Description
|
|
Causes the loop
to skip the remainder of its body and immediately retest its condition prior
to reiterating.
|
|
|
Terminates
the loop statement and transfers execution to the statement
immediately following the loop.
|
|
|
A continue BLOCK,
it is always executed just before the conditional is about to be evaluated
again.
|
|
|
The redo command
restarts the loop block without evaluating the conditional again. The
continue block, if any, is not executed.
|
|
|
Perl supports a
goto command with three forms: goto label, goto expr, and goto &name.
|
The Infinite Loop:
A loop becomes
infinite loop if a condition never becomes false. The for loop
is traditionally used for this purpose. Since none of the three expressions
that form the for loop are required, you can make an endless loop by leaving
the conditional expression empty.
#!/usr/local/bin/perl
for( ; ; )
{
printf "This loop will run
forever.\n";
}
You can terminate above infinite loop by pressing Ctrl + C keys.
When the conditional
expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but as a programmer more commonly use the for(;;)
construct to signify an infinite loop.
Comments
Post a Comment