# Lesson 12: Truth and conditional statements

``` What is TRUE (in Perl)?```

``` The string "0" is False. The number 0 is False. The empty string ("" or '') is False The empty list is False The undeﬁned value is False (e.g. an uninitialized variable) A number is false if it converts to string "0". Everything else is True. \$a; FALSE (not yet deﬁned) \$a = 1; TRUE \$b = 0; FALSE \$c = ""; FALSE \$d = 'true'; TRUE \$e = 'false'; TRUE (watch out! "false" is a non-empty string) \$f = ' '; TRUE (a single space is non-empty) \$g = "\n"; TRUE (a single newline is non-empty) @h = (); FALSE array is empty \$i = 0.0; FALSE \$j = '0.0'; TRUE (watch out! The string "0.0" is not the same as "0") Many operators and functions return values of 0 and 1. These results can be used to control the flow of your code. Comparison operators and truth Comparison operators return 1 if the comparison is true undefined if the comparison is false -1 in special cases with specific operators numeric/string desc examples < lt less than 1 < 2 #true a lt b #true by ascii order > gt greater than 1 > 2 #falsea gt b #false <= le less than or equal 1 <= 2 #true 2 <= 2 #true a le b #true >= ge greater than or equal 3 >= 2 #true 2 >= 2 #truea gt b #false == eq equality 1 == 2 #falsea eq b #false != ne non-equality 1 != 2 #truea ne b #true cmp comparison -1 if left < right 0 if equal +1 if left > right 1 2 #-1a cmp b #-1 Testing for truth with if/elsif/else Code: if/else ```1 2 3 4 5 6 my \$num = 50; if (\$num){ print "True: \$num\n"; }else { print "False: \$num\n"; }``` Output: %% ./truth.pl True: 50 Code: if/elsif/else ```1 2 3 4 5 6 7 8 my \$num = 30; if (\$num &lt; 20){ print &quot;True: \$num &lt; 20\n&quot;; }elsif (\$num == 25) { print &quot;True: \$num is 25\n&quot;; }else{ print &quot;False: \$num is not &lt; 20 and \$num is not 25\n&quot;; }``` Output: %% ./truth.pl False: 30 is not < 20 and 30 is not 25 Code: if/elsif/else Change the value of \$num to change the outcome ```1 2 3 4 5 6 7 8 my \$num = 25; if (\$num &lt; 20){ print &quot;True: \$num is &lt; 20\n&quot;; }elsif (\$num == 25) { print &quot;True: \$num is 25\n&quot;; }else{ print &quot;False: \$num is not &lt; 20 and \$num is not 25\n&quot;; }``` Output: %% ./truth.pl True: 25 is 25 You can use as many elsif statements that are needed. Once a true statement is found, no more statements are tested. If all statements are false the else code block is executed. More on conditional statements Tests can be strung together using 'and', '&&' , 'or' , '||' operators operator example To be true and      && \$i 5 both tests need to be true or      || \$str eq 'clack' or \$str ne 'black' only one test needs to be true Exercises Write a script in which you have a if/elsif/elsif/else conditional statement. Use strings comparison operators in your tests. Create a different print statement foreach condition Change the value of the variables you used in your testing. Run script again. Create a new conditional statement in which you use numeric conditional operators ```

## 2 thoughts on “Lesson 12: Truth and conditional statements”

1. Sofia on said:

one liner “if statement”
If you have a simple test and a simple statement to execute you can use a one liner ‘if statement’.

Code:

```my \$i = 5;
print "The number \$i is < 10n" if \$i < 10;
```

Output:

```%% oneLiner.pl
The number 5 is < 10```
2. Sofia on said:

testing for failure using the ‘unless’ instead of ‘if’

if you want to failure to be true, you can use unless.

Code:

```my \$line = "They lived happily ever after";

print "Not the endn" unless \$line eq "They lived happily ever after";
```

Using an ‘if statement’ to do the same thing:

```my \$line = "They lived happily ever after";

print "Not the endn" if \$line ne "They lived happily ever after";
```