Decision Constructs
CHAPTER 6: DECISION CONSTRUCTS
Truth In Perl
- Expression evaluated in a scalar context
- A numeric scalar is true if it is not 0, else it is false
- A string scalar is true if it is not the null string or the
string "0", else it is false
- Ex.
0 # False
14 - 2 # True
"" # False
"0" # False
"00" # True! (Pathological Case)
"0.0" # True! (Pathological Case)
If
- if (test_expr)
{
statement(s); # Executed if test_expr is true
}
- The {} are REQUIRED!
- Syntax is really: if (test_expr) statement_block
- The () around the test_expr are also required!
- A compound statement
- Ex.
if ($age < 21)
{
print ("Go drink somewhere else!\n");
}
If-Else
- if (test_expr)
{
statement(s); # Executed if test_expr is true
}
else
{
statement(s); # Executed if test_expr is false
}
- A compound statement
- Ex.
if ($age < 21)
{
print ("Go drink somewhere else!\n");
}
else
{
print ("Belly up to the bar!\n");
}
If-Elsif-Else
- if (test_expr_1)
{
statement(s); # Executed if test_expr_1 is true
}
elsif (test_expr_2)
{
statement(s); # Executed if test_expr_2 is true
}
else
{
statement(s); # Executed if all test expressions
# are false
}
- May have as many elsif branches as desired
- Each test expression is evaluated in sequence. If an expression
is true, the corresponding statement block is executed and all
remaining test expressions and statement blocks are skipped. If
all test expressions are false, the else statement block is
executed.
- A compound statement
- Ex.
if ($age < 21)
{
print ("Go drink somewhere else!\n");
}
elsif ($age > 55)
{
print ("Senior's discount is 50%!!\n");
}
else
{
print ("Belly up to the bar!\n");
}
Unless
- unless (test_expr)
{
statement(s); # Executed if test_expr is false
}
- A compound statement
- Ex.
unless ($age >= 21)
{
print ("Go drink somewhere else!\n");
}
Unless-Else
- unless (test_expr)
{
statement(s); # Executed if test_expr is false
}
else
{
statement(s); # Executed if test_expr is true
}
- A compound statement
- Ex.
unless ($age >= 21)
{
print ("Go drink somewhere else!\n");
}
else
{
print ("Belly up to the bar!\n");
}
Special Notes
- No unlessif
- No switch or case
Expression Modifiers
- Can use if and unless as expression modifiers
- Similar to the if and unless statement except that only an
expression can be modified, NOT a statement block
If Modifier
- exec_expr if test_expr;
- Exec_expr executed if the test_expr is true
- Equivalent to: if (test_expr)
{
exec_expr;
}
- The () around the test expression are NOT required here!
- A simple statement
- Ex.
print ("Go drink somewhere else!\n") if ($age < 21);
Unless Modifier
- exec_expr unless test_expr;
- Exec_expr executed if the test_expr is false
- Equivalent to: unless (test_expr)
{
exec_expr;
}
- The () around the test expression are NOT required here!
- A simple statement
- Ex.
print ("Belly up to the bar!\n") unless ($age < 21);
Short-Circuit AND Operator (&&)
- expression_1 && expression_2;
- First expression_1 is evaluated. If true, Perl then must evaluate
expression_2 to find the value of the entire expression. If
expression_1 is false, the entire expression is false, and the
evaluation of expression_2 is skipped.
- Similar to:
if (expression_1)
{
expression_2;
}
- Value is value of last expression evaluated
- A simple statement
- Ex.
# Open "file2" only if the open for "file1" succeeds.
open (FILE1, "file1") && open (FILE2, "file2");
# Print a list value if there are still elements remaining.
($x = shift (@list)) && print $x;
Short-Circuit OR Operator (||)
- expression_1 || expression_2;
- First expression_1 is evaluated. If false, Perl then must evaluate
expression_2 to find the value of the entire expression. If
expression_1 is true, the entire expression is true, and the
evaluation of expression_2 is skipped.
- Similar to:
unless (expression_1)
{
expression_2;
}
- Value is value of last expression evaluated
- A simple statement
- Ex.
# Open file ".btrc successfully or die!
open (FILE, ".btrc") || die ("Can not open input file\n");
Three-Operand Conditional (?:)
- test_expr ? expr_1 : expr_2;
- If test_expr is true, expr_1 is evaluated, else expr_2 is evaluated
- Similar to:
if (test_expr)
{
expr_1;
}
else
{
expr_2;
}
- A simple statement
- Ex.
$access == 1 ? print ("r") : print ("-");
Bob Tarr
University of Maryland, Baltimore County
tarr@umbc.edu