Monday, December 05, 2005

 

Perl: IF ELSE

Conditionals are put to use in IF THEN ELSE. The following are acceptable
uses of IF:

if (EXPRESSION ) {
STATEMENTS;
}

if (EXPRESSION ) {
# executed if true
STATEMENTS;
} ELSE {
# executed if false
STATEMENTS;
}

if (EXPRESSION ) {
STATEMENTS;
} elsif {
STATEMENTS;
# optional additional ELSIF's
} else {
STATEMENTS;
}

This is the first time we've seen the { } (braces) used outside the
context of associative arrays. In addition to denoting the key for an
associative array, they are used to mark of blocks of expressions. Any
number of statements can be part of the true or false portions of the IF
ELSE, and the braces are to enclose all of them. Unlike C, the braces are
not optional - requiring braces avoids the dangling else problem
Perl has no SWITCH statement - it can be imitated several ways, including
using ELSIF for each possible case.


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?