📅  最后修改于: 2020-10-16 05:27:40             🧑  作者: Mango
Perl条件语句有助于进行决策,这要求程序员指定一个或多个条件(由程序确定或测试),如果确定条件为真,则还应指定要执行的一个或多个语句。如果确定条件为假,将执行的语句。
以下是大多数编程语言中常见的典型决策结构的概况-
在布尔上下文中,数字0,字符串’0’和“”,空列表()和undef均为false ,其他所有值均为true 。否定真值!或不返回特殊的false值。
Perl编程语言提供以下类型的条件语句。
Sr.No. | Statement & Description |
---|---|
1 | if statement
An if statement consists of a boolean expression followed by one or more statements. |
2 | if…else statement
An if statement can be followed by an optional else statement. |
3 | if…elsif…else statement
An if statement can be followed by an optional elsif statement and then by an optional else statement. |
4 | unless statement
An unless statement consists of a boolean expression followed by one or more statements. |
5 | unless…else statement
An unless statement can be followed by an optional else statement. |
6 | unless…elsif..else statement
An unless statement can be followed by an optional elsif statement and then by an optional else statement. |
7 | switch statement
With the latest versions of Perl, you can make use of the switch statement. which allows a simple way of comparing a variable value against various conditions. |
让我们检查条件运算符吗? :可用于替换if … else语句。它具有以下一般形式-
Exp1 ? Exp2 : Exp3;
其中Exp1,Exp2和Exp3是表达式。注意冒号的使用和放置。
a的值表达式是这样确定的:Exp1被求值。如果为true,则将评估Exp2并成为整个?的值。表达。如果Exp1为false,则计算Exp3,其值成为表达式的值。以下是使用此运算符的简单示例-
#!/usr/local/bin/perl
$name = "Ali";
$age = 10;
$status = ($age > 60 )? "A senior citizen" : "Not a senior citizen";
print "$name is - $status\n";
这将产生以下结果-
Ali is - Not a senior citizen