How do I use an if/else control structure in perl?



  • This is frequently used logic in many programming languages.

    The if/else control structure, similar to making human decisions.

    $num = 50;
    if ($num lt 19) {
      print "'$num' is smaller than 19.\n";
    } else {
      print "'$num' is bigger than 19.\n";
      print "We've hit the big time!.\n";
    }
    

    Normally you would not hardcode the $num value, it would be taken from user input or calculated within the script. If the $num is greater than 19 it will print one message otherwise it will print the other.

    In the example, it will always print the messages after the else keyword, because $num is hardcoded and is greater than 19. Just change the $num to a number less than 19 to demo the other print statement.


Log in to reply
 

© Lightnetics 2024