• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Perl if Statement

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Summary: in this tutorial, you are going to learn about Perl if statement that allows you to control the execution of code conditionally.

Simple Perl if statement

Perl if statement allows you to control the execution of your code based on conditions. The simplest form of the if statement is as follows:

In this form, you can put the if statement after another statement. Let’s take a look at the following example:

The message is only displayed if the expression $a == 1 evaluates to true . How Perl defines true and false? The following rules are applied when Perl evaluates an expression:

  • Both number 0 and string “0” are false.
  • The undefined value is false.
  • The empty list () is false.
  • The empty string "" is false.
  • Everything else is true.

If you do not remember the rules or you doubt whether a condition is true or false, you can always write a simple if statement to test if the expression.

The limitation of the Perl if statement is that it allows you to execute a single statement only. If you want to execute multiple statements based on a condition, you use the following form:

Noticed that the curly braces {} are required even if you have a single statement to execute. Watch-out C/C++ and C# programmers!

The following flowchart illustrates the Perl if statement:

And the following is an example of using the if statement:

Perl if else statement

In some cases, you want to also execute another code block if the expression does not evaluate to true. Perl provides the if else statement that allows you to execute a code block if the expression evaluates to true, otherwise, the code block inside the else branch will execute.

The following flowchart illustrates the Perl if else statement:

See the following example, the code block in the else branch will execute because $a and $b are not equal.

Perl if elsif statement

In some cases, you want to test more than one condition, for example:

  • If $a and $b are equal, then do this.
  • If $a is greater than $b then do that.
  • Otherwise, do something else.

Perl provides the if elsif statement for checking multiple conditions and executing the corresponding code block as follows:

The following flowchart illustrates the if elseif else statement:

Technically speaking, you can have many elsif branches in an if elseif statement. However, it is not a good practice to use a lengthy if elsif statement.

Perl if statement example

We are going to apply what we have learned so far to create a simple program called currency converter.

  • We will use a hash to store the exchange rates.
  • To get the inputs from users via command line, we will use <STDIN>. We use the chomp() function to remove newline character (\n) from user’s inputs.
  • We convert the amount from local currency to foreign currency if the currencies are supported.

The following source code illustrates the simple currency converter program:

In this tutorial, you’ve learned various forms of the Perl if statement including simple if, if else, and if esif else statements to control the execution of the code.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
浅谈Perl的类、包、模块与面对对象编程发布时间:2022-07-22
下一篇:
perl学习笔记--搭建开发环境发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap