Lesson 12: Truth and conditional statements


What is TRUE (in Perl)?

  1. The string "0" is False.
  2. The number 0 is False.
  3. The empty string ("" or '') is False
  4. The empty list is False
  5. The undefined value is False (e.g. an uninitialized variable)
  6. A number is false if it converts to string "0".
  7. Everything else is True.
$a; FALSE (not yet defined)
$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 #false
a 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 #true
a gt b #false
==
eq
equality 1 == 2 #false
a eq b #false
!=
ne
non-equality 1 != 2 #true
a ne b #true
cmp comparison
  • -1 if left < right
  • 0 if equal
  • +1 if left > right
  • 1 2 #-1
    a 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

    1. 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
    2. Change the value of the variables you used in your testing. Run script again.
    3. Create a new conditional statement in which you use numeric conditional operators

    Print Friendly

    2 thoughts on “Lesson 12: Truth and conditional statements

    1. 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. 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";
      

    Leave a Reply

    Your email address will not be published. Required fields are marked *