原文: https://beginnersbook.com/2017/02/if-else-statement-in-perl/

上一篇教程中,我们了解了 perl 中的if语句。在本教程中,我们将了解if-else条件语句。这是if-else语句的外观:

  1. if(condition) {
  2. Statement(s);
  3. }
  4. else {
  5. Statement(s);
  6. }

如果条件为真,则if内的语句将执行,如果条件为假,则else内的语句将执行。

例:

  1. #!/usr/local/bin/perl
  2. printf "Enter any number:";
  3. $num = <STDIN>;
  4. if( $num >100 ) {
  5. # This print statement would execute,
  6. # if the above condition is true
  7. printf "number is greater than 100\n";
  8. }
  9. else {
  10. #This print statement would execute,
  11. #if the given condition is false
  12. printf "number is less than 100\n";
  13. }

输出:

  1. Enter any number:120
  2. number is greater than 100