
Perl else if简介
if-else "语句对选择一个条件很有用,但当涉及到多个条件时,"Perl else if "语句在技术上就很有用。它对同一优先级的多个条件的语句是有用的,在这种技术中只选择一个条件。它从上到下工作,在else if语句中逐一传递参数。"else if "在技术上称为Perl "elsif",在语言中作为一个语句使用。
语法。
else if (elsif)语句的语法如下。
elsif(condition for statement)
{
Perl code for statement;
}
else if (elsif)语句在条件中使用的语法如下。
if(condition_1 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_2 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_3 for statement)
{
Perl code for statement execution (if the condition is true);
}
else(condition_4 for statement)
{
Perl code for statement execution (if all above conditions are failed);
}
Perl的else if(elsif)规则的工作语法如下。
- 它被放在 "if语句 "之后,"else语句 "之前。
- 它在代码中需要两个以上的条件。
- 当 "if条件 "失败时,它就会成功。
- 如果它成功了,那么剩下的elsif和else条件就会失败。
在Perl中else if语句如何工作?
下载并安装你的设备的操作系统的最新版本。
www.Perl.org/ 或 strawberryPerl.com/,主要是使用Perl IDE网站的链接。
创建一个扩展名为.pl的文件,并将该文件保存在所需的命令行路径中。
例子。
helloo.pl or first pearl.pl
在代码中使用else if(elsif)语句的语法。
if(condition_1 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_2 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_3 for statement)
{
Perl code for statement execution (if the condition is true);
}
else(condition_4 for statement)
{
Perl code for statement execution (if all above conditions are failed);
}
Perl else if 语句的描述。
- 首先将if语句作为一个参数传递,如果条件失败,则将else if (elsif)语句作为一个参数传递。
- 条件变得失败后,另一个else if (elsif)语句作为参数被传递。
- 当最后一个else if (elsif)条件变得失败时,else条件变成了真。
- 当else条件变为真时,if语句和所有else if语句都失败。
- 它与 "Perl unless "语句一起使用,用于多个条件语句。
下面是else if语句的工作示例。
代码。
$number_var = 240;
if($number_var == 40)
{
print "the given number is 40 \n";
}
elsif($number_var == 24)
{
print "the given number is 24 \n";
}
elsif($number_var== 240)
{
print "the given number is 240 \n";
}
else
{
print "the given number is not present here... \n";
}
Perl else if的例子
下面是一些例子。
例子 #1
简单的else if(elsif)语句的例子和输出。
代码。
$number_var = 240;
if($number_var == 40)
{
print "the given number is 40 \n";
}
elsif($number_var == 24)
{
print "the given number is 24 \n";
}
elsif($number_var== 240)
{
print "the given number is 240 \n";
}
else
{
print "the given number is not present here... \n";
}
输出。

例子 #2
语句中的If语句为真 例子和输出。
代码。
$number_var = 410;
if($number_var == 410)
{
print "the given number is 410 \n";
}
elsif($number_var == 240)
{
print "the given number is 240 \n";
}
elsif($number_var== 420)
{
print "the given number is 420 \n";
}
else
{
print "the given number is not present here... \n";
}
输出。

解释。
- if 语句的条件是true。
- 上面的例子在屏幕上显示了if语句的输出。
例子#3
带有else语句的语句示例和输出。
代码。
$number_var = 840;
if($number_var < 410)
{
print "the given number is 410 \n";
}
elsif($number_var > 940)
{
print "the given number is 240 \n";
}
elsif($number_var == 420)
{
print "the given number is 420 \n";
}
else
{
print "the given number is not present here... \n";
}
输出。

解释。
- if语句和else if(elsif)语句的条件是错误的。
- 上面的例子在屏幕上显示else语句的输出。
例子 #4
语句与除非语句的例子和输出。
代码。
$number_var = 840;
unless($number_var == 840)
{
print "the given number is not 410 \n";
}
elsif($number_var == 840)
{
print "the given number is 840 \n";
}
else
{
print "the given number is not present here... \n ";
}
输出。

解释。
- 上述例子中的unless和else if语句是一样的。
- 语句先作为一个参数传递,然后是除非语句。
- 它在屏幕上显示为输出。
例子#5
带有多个else if的语句示例和输出。
代码。
$number_var = 840;
if($number_var < 240)
{
print "the given number is not 410 \n";
}
elsif($number_var > 240)
{
print "the given number is greater than 240 \n";
}
elsif($number_var == 840)
{
print "the given number is 840 \n";
}
else
{
print "the given number is not present here... \n ";
}
输出。

解释。
- 多于两个else if (elsif)语句为真,则第一个else if语句显示为输出。
- 上面的例子中,两个 "else if "语句都是真的,但第一个语句被显示为输出。
总结
else if (elsif) 语句使得在该技术中选择多个条件变得容易和简单。该语句对类似的优先条件和它们的一个输出很有用。