Conditional Statements
The above code asks a question ("Is the name equal to 'Joe'???).
If the answer is yes, the statement(s) between the curly braces
will be executed. If the answer is no, nothing will happen
and execution will continue with the statement after the closing
brace.
Another example:
Here we have multiple statements inside the braces.
And there is a separate group of statements in case
the answer were no (this is called the 'else clause').
32-33 117-119,126-128 80-83
Carefully note the position of the braces and the indentation. This is the "O'Reilly Standard". Use it. This is one of the few things about which it is a good thing to be obsessive compulsive. Indentation is most easily accomplished with tabs. Tabs are usually set at 4. Note that unlike C/C++ or Java, braces are required even if there is only one statement within the condition. If you think this is annoying, you are right and rest assured that Perl has an answer to your annoyance - but not just yet. Here is the full conditional:
This presents a list of questions to ask.
The first one to which you can answer yes is the
one that is chosen. The corresponding statements will
be executed and then the program will continue with
the statement following the else clause.
There is no 'switch' statement in Perl as there is in C/C++. 'if' statements can be nested as shown here:
Perl has no limit to how deep the nesting can be.
The only limit is your imagination and patience!
139-140 126-128 80-84
String Comparision OperatorsIn addition to 'eq' as shown above there are also these operators to ask questions inside the 'if' condition:31-32 57-58 81-82eq equal ne not equal gt greater than lt less than ge greater than or equal le less than or equal This does character by character ASCII comparision just like dictionary order. Even if the things you are comparing are numbers these operators will still do a character by character comparison. Numeric Comparison Operators31-32 51-52 81-82== numeric equal != not equal > greater than < less than >= greater than or equal <= less than or equal
You must be aware of what kind of comparision you are doing!!! For example:
Boolean Operators
The questions inside the if statements can be as
complex as they need to be. If it doesn't seem to be
working as you expected it to try putting in parentheses to
more precisely state what you want.
How do you ask if a number is between two others? e.g. In Perl how do you ask if a number is between 1 and 10? This will not work:
You have to do this:
148-149 54,123-124 89-91
True and False in PerlOne can ask whether a plain variable is true or not.
Four things are FALSE:
Everything else is TRUE.
Again, as we noted before,
Perl generally does what you want and expect it to do.
undefUninitialized variables appearing for the first time (not in an assignment statement) have the value of 'undef' - a perl keyword. As a string it is the same as the empty string. As a number it is treated as zero. As a logical value it is false.35-36 (189) (101)
Exercises
|