HTML CSS PHP PERL

The if Statement

 
Previous Table of Contents Next

The if Statement

To control whether statements are executed based on a condition in a Perl program, you usually use an if statement. The syntax of an if statement is as follows:


if (expression) block


The statement works like this: If the expression evaluates to true, the block of code is run. If the expression is false, the block of code is not run. Remember that the block includes the braces. Consider this example:


if ( $r == 5 ) {

    print 'The value of $r is equal to 5.';

}


The expression being tested is $r == 5. The == symbol is an equality operator. If the two operands on either side$r and 5are numerically equal to one another, the expression is considered to be true, and the print statement is executed. If $r is not equal to 5, the print statement is not executed.

The if statement can also run one set of statements if a condition is true and another set of statements if it's not. That structure is called an if-else statement. The syntax looks like this:




if (expression)      #If expression is true...

    block1           # ...this block of code is run.

else

    block2           # Otherwise this block is run.


The first block, block1, is run only if the expression is true; if the expression is not true, block2, following the else, is run. Now consider this example:


$r=<STDIN>;

chomp $r;

if ($r == 10) {

    print '$r is 10', "\n";

} else {

    print '$r is something other than 10...', "\n";

    $r=10;

    print '$r has been set to 10', "\n";

}


Watch Out!

In the preceding example, notice that to assign a value to $r, I used the assignment operator, =. To test the value of $r, I used the numeric equality test operator, ==. Do not confuse them in your programs because debugging can be very difficult. Remember that = assigns a value and == tests for equality. If you use the -w option to turn warnings on, Perl can sometimes warn you if you have made this error.


Yet another way of structuring an if statement is to check multiple expressions and run code depending on which expressions are true:


if (expression1)     # If expression1 is true ...

    block1           # ...run this block of code.

elsif (expression2)  # Otherwise, if expression2 is true...

    block2           # ...Run this block of code.

else

    block3           # If neither expression was true, run this.


You can read the preceding block like this: If the expression labeled expression1 is true, the block block1 is run. Otherwise, control falls to the elsif and expression2 is tested; if it's true, block2 is run. If neither expression1 nor expression2 is true, block3 is run. The following is an example of real Perl code that demonstrates this syntax:


$r=10;

if ($r==10) {

    print '$r is 10!';

} elsif ($r == 20) {

    print '$r is 20!';

} else {

    print '$r is neither 10 nor 20';

}


The Other Relational Operators

So far, you've been comparing numeric quantities in your if statements with the equality operator, ==. Perl actually has quite a few operators for comparing numeric values, most of which are listed in Table 3.1.

Table 3.1. Numeric Relational Operators

Operator

Example

Explanation

==

$x == $y

True if $x equals $y

>

$x > $y

True if $x is greater than $y

<

$x < $y

True if $x is less than $y

>=

$x >= $y

True if $x is greater than or equal to $y

<=

$x <= $y

True if $x is less than or equal to $y

!=

$x != $y

True if $x is not equal to $y


To use these operators, you can simply put them in anywhere that your program needs to test relations between numeric values. An example of the use of these operators in an if statement is shown in Listing 3.1, which you can type in and run (do not type the line numbers"1:" and so forth). The various parts of the program work as follows:

Listing 3.1. A Small Number Guessing Game

1:   #!/usr/bin/perl -w

2:

3:   $im_thinking_of=int(rand 10);

4:   print "Pick a number:";

5:   $guess=<STDIN>;

6:   chomp $guess;   # Don't forget to remove the newline!

7:

8:   if ($guess>$im_thinking_of) {

9:       print "You guessed too high!\n";

10:  } elsif ($guess < $im_thinking_of) {

11:      print "You guessed too low!\n";

12:  } else {

13:      print "You got it right!\n";

14:  }


Line 1: This line is the standard first line of a Perl program; it indicates the interpreter you want to run and the -w switch to enable warnings. See Hour 1, "Getting Started with Perl"; your first line may need to look slightly different.

Line 3: The (rand 10) function picks a number between 0 and 10, and the int()function truncates it so that only integers 0 to 9 are assigned to $im_thinking_of.

Lines 46: These lines ask the user for the guess, assign it to $guess, and remove the trailing newline character.

Lines 89: If $guess is greater than the number in $im_thinking_of, these lines print an appropriate message.

Lines 1011: Otherwise, if $guess is less than the number in $im_thinking_of, these lines print that message.

Lines 1213: The only choice left is that the user guessed the number.

The operators in Table 3.1 are used only for testing numeric values. Using them to test nonalphabetic data results in behavior that you probably don't want. Consider this example:


$first="Simon";

$last="simple";

if ($first == $last) {    # == is not what you want!

    print "The words are the same!\n";  

}


The two values $first and $last actually test equal to each other. The reason was explained in Hour 2: If nonnumeric strings are used when Perl is expecting numeric values, the strings evaluate to zero. So the preceding if expression looks something like this to Perl: if ( 0 == 0 ). This expression evaluates to true, and that's probably not what you wanted.

By the Way

If warnings are turned on, trying to test two alphabetic values (simple and Simon in the preceding snippet) with == will generate a warning message when the program runs to alert you to this problem.


If you want to test nonnumeric values, you can use another set of Perl operators, which are listed in Table 3.2.

Table 3.2. Alphanumeric Relational Operators

Operator

Example

Explanation

Eq

$s eq $t

True if $s is equal to $t

Gt

$s gt $t

True if $s is greater than $t

Lt

$s lt $t

True if $s is less than $t

Ge

$s ge $t

True if $s is greater than or equal to $t

Le

$s le $t

True if $s is less than or equal to $t

Ne

$s ne $t

True if $s is not equal to $t


These operators decide "greater than" and "less than" by examining each character left to right and comparing them in ASCII order. This means that strings sort in ascending order: most punctuation first, and then numbers, uppercase, and finally lowercase. For example, 1506 compares less than Happy, which compares less than happy.

What Truth Means to Perl

Up to this point, you've been reading about "if this expression is true…" or "…evaluates to true…," but you haven't seen any formal definition of what Perl thinks "true" is. Perl has a few short rules about what is true and what is not true, and the rules actually make sense when you think about them for a bit. The rules are as follows:

  • The number 0 is false.

  • The empty string ("") and the string "0" are false.

  • The undefined value undef is false.

  • Everything else is true.

Make sense? The only other point to remember is that when you're testing an expression to see whether it's true or false, the expression is simplifiedfunctions are called, operators are applied, math expressions are reduced, and so onand then converted to a scalar value for evaluation to determine whether it is true or false.

Think about these rules, and then take a look at Table 3.3. Try to guess whether the expression is true or false before you look at the answer.

Table 3.3. True or False Examples

Expression

True or False?

0

False. The number 0 is false.

10

True. It is a nonzero number and therefore true.

9>8

True. Relational operators return true or false, as you would expect.

-5+5

False. This expression is evaluated and reduced to 0, and 0 is false.

0.00

False. This number is another representation of 0, as are 0x0, 00, 0b0, and 0e00.

""

False. This expression is explicitly mentioned in the rules as false.

" "

True. There's a space between the quotes, which means they're not entirely empty.

"0.00"

True. Surprise! It's already a string, but not "0" or "". Therefore, it is true.

"00"

True also, for the same reason as "0.00"

"0.00" + 0

False. In this expression, 0.00+0 is evaluated, the result is 0, and that is false.


Until now, you've seen only expressions with relational operators as the conditions in if statements. Actually, you can use any expression that will evaluate to true or false the way you would want:


# The scalar variable $a is evaluated for true/false

if ($a) {  ...  }



# Checks the length of $b.  If nonzero, the test is true.

if (length($b)) { ....  }


Recall from Hour 2 that the assignment operator = returns a valuethe value that was assigned. That value, of course, is also true or false:


$a = 1;

$b = 2;

print qq(The statement "$a = $b" is );

if ($a = $b) { # value is 2, therefore true

    print "true";

} else {

    print "false";

}


This code prints The statement "1 = 2" is true. Now you see why using = when you meant == is such a pitfall.

The value undef is a special value in Perl. Variables that have not yet been set have the value of undef, and some functions return undef on failure. It's not 0, and it's not a regular scalar value. It's kind of special. In a test for truth, undef always evaluates to false. If you try to use the undef value in a math expression, it's treated as though it were 0.

Using variables that haven't been set yet is usually a sign of a programming error. If you're running your Perl programs with warnings enabled, the value undef in an expression or as an argument to some functions causes Perl to generate the warning Use of uninitialized value.

Logical Operators

When you're writing programs, you sometimes need to code something like the following: Do this if $x is true and if $y is true, but not if $z is true. You can code this example into a series of if statements, but it's not pretty:


if ($x) {

    if ($y) {

        if ($z) {

            # do nothing

        } else {

            print "All conditions met.\n";

        }

    }

}


Perl has a whole class of operators for connecting together true and false statements like this, called logical operators. The logical operators are shown in Table 3.4.

Table 3.4. Logical Operators

Operator

Alternative Name

Example

Analysis

&&

and

$s && $t

True only if $s and $t are true

  

$q and $p

True only if $q and $p are true

||

or

$a || $b

True if $a is true or $b is true

  

$c or $d

True if $c is true or $d is true

!

not

Previous Table of Contents Next

© 2000- NIV