珀尔 |决策(if、if-else、嵌套-if、if-elsif 阶梯、除非、除非-else、除非-elsif)
编程中的决策类似于现实生活中的决策。在编程中,当某个条件满足时,需要执行某个代码块。编程语言使用控制语句根据特定条件控制程序的执行流程。这些用于根据程序状态的变化使执行流程前进和分支。
Perl 中的决策声明:
- 如果
- 如果别的
- 嵌套 - 如果
- if – elsif 梯子
- 除非
- 除非——否则
- 除非 – elsif
if 语句
if 语句与其他编程语言中的相同。它用于执行基于基本条件的任务。它用于决定是否执行某个语句或语句块,即如果某个条件为真,则执行一个语句块,否则不执行。
句法 :
if(condition)
{
# code to be executed
}
注意:如果大括号 { } 不与 if 语句一起使用,则会出现编译时错误。所以必须在 if 语句中使用方括号 {}。
流程图:
例子 :
Perl
# Perl program to illustrate if statement
$a = 10;
# if condition to check
# for even number
if($a % 2 == 0 )
{
printf "Even Number";
}
Perl
# Perl program to illustrate
# if - else statement
$a = 21;
# if condition to check
# for even number
if($a % 2 == 0 )
{
printf "Even Number";
}
else
{
printf "Odd Number\n";
}
Perl
# Perl program to illustrate
# Nested if statement
$a = 10;
if($a % 2 ==0)
{
# Nested - if statement
# Will only be executed
# if above if statement
# is true
if($a % 5 == 0)
{
printf "Number is divisible by 2 and 5\n";
}
}
Perl
# Perl program to illustrate
# if - elseif ladder statement
$i = 20;
if($i == 10)
{
printf "i is 10\n";
}
elsif($i == 15)
{
printf "i is 15\n";
}
elsif($i == 20)
{
printf "i is 20\n";
}
else
{
printf "i is not present\n";
}
Perl
# Perl program to illustrate
# unless statement
$a = 10;
unless($a != 10)
{
# if condition is false then
# print the following
printf "a is not equal to 10\n";
}
Perl
# Perl program to illustrate
# unless - else statement
$a = 10;
unless($a == 10)
{
# if condition is false then
# print the following
printf "a is not equal to 10\n";
}
else
{
# if condition is true then
# print the following
printf "a is equal to 10\n";
}
Perl
# Perl program to illustrate
# unless - elsif statement
$a = 50;
unless($a == 60)
{
# if condition is false
printf "a is not equal to 60\n";
}
elsif($a == 60)
{
# if condition is true
printf "a is equal to 60\n";
}
else
{
# if none of the condition matches
printf "The value of a is $a\n";
}
输出 :
Even Number
if – else 语句
if 语句在条件为真时评估代码,但如果条件不为真怎么办,这里是 else 语句。它告诉代码当 if 条件为假时要做什么。
句法 :
if(condition)
{
# code if condition is true
}
else
{
# code if condition is false
}
流程图:
例子 :
Perl
# Perl program to illustrate
# if - else statement
$a = 21;
# if condition to check
# for even number
if($a % 2 == 0 )
{
printf "Even Number";
}
else
{
printf "Odd Number\n";
}
输出 :
Odd Number
嵌套 - if 语句
if 语句中的 if语句称为嵌套 if。在这种情况下,if 语句是另一个 if 或 else 语句的目标。当需要满足多个条件且其中一个条件是父条件的子条件时,可以使用嵌套的 if。
句法 :
if (condition1)
{
# Executes when condition1 is true
if (condition2)
{
# Executes when condition2 is true
}
}
流程图:
例子 :
Perl
# Perl program to illustrate
# Nested if statement
$a = 10;
if($a % 2 ==0)
{
# Nested - if statement
# Will only be executed
# if above if statement
# is true
if($a % 5 == 0)
{
printf "Number is divisible by 2 and 5\n";
}
}
输出 :
Number is divisible by 2 and 5
if – elsif – else 阶梯语句
在这里,用户可以在多个选项中做出决定。 if 语句是自上而下执行的。一旦控制 if 的条件之一为真,与该条件相关的语句就会被执行,而梯形图的其余部分将被绕过。如果没有一个条件为真,那么最后的 else 语句将被执行。
句法 :
if(condition1)
{
# code to be executed if condition1 is true
}
elsif(condition2)
{
# code to be executed if condition2 is true
}
elsif(condition3)
{
# code to be executed if condition3 is true
}
...
else
{
# code to be executed if all the conditions are false
}
流程图:
例子 :
Perl
# Perl program to illustrate
# if - elseif ladder statement
$i = 20;
if($i == 10)
{
printf "i is 10\n";
}
elsif($i == 15)
{
printf "i is 15\n";
}
elsif($i == 20)
{
printf "i is 20\n";
}
else
{
printf "i is not present\n";
}
输出 :
i is 20
除非语句
在这种情况下,如果条件为假,则语句将执行。数字 0、空字符串“”、字符'0'、空列表 () 和 undef在布尔上下文中都是假的,所有其他值都是真。
句法 :
unless(boolean_expression)
{
# will execute if the given condition is false
}
流程图:
例子 :
Perl
# Perl program to illustrate
# unless statement
$a = 10;
unless($a != 10)
{
# if condition is false then
# print the following
printf "a is not equal to 10\n";
}
输出 :
a is not equal to 10
除非-else 语句
除非语句后跟可选的 else 语句,当布尔表达式为真时执行。
句法 :
unless(boolean_expression)
{
# execute if the given condition is false
}
else
{
# execute if the given condition is true
}
流程图:
例子 :
Perl
# Perl program to illustrate
# unless - else statement
$a = 10;
unless($a == 10)
{
# if condition is false then
# print the following
printf "a is not equal to 10\n";
}
else
{
# if condition is true then
# print the following
printf "a is equal to 10\n";
}
输出 :
a is equal to 10
除非 – elsif 语句
unless 语句后面可以跟一个可选的 elsif...else 语句,这对于使用单个 unless...elsif 语句测试各种条件非常有用。
要记住的要点:
- 除非语句可以有零到多个 elsif,并且所有必须在 else 之前。
- 除非语句可以有零或一个其他的,并且必须在任何 elsif 之后。
- 一旦一个 elsif 成功,那么剩下的 elsif 或 else 都不会被测试。
句法 :
unless(boolean_expression 1)
{
# Executes when the boolean expression 1 is false
}
elsif( boolean_expression 2)
{
# Executes when the boolean expression 2 is true
}
else
{
# Executes when the none of the above condition is met
}
流程图:
例子 :
Perl
# Perl program to illustrate
# unless - elsif statement
$a = 50;
unless($a == 60)
{
# if condition is false
printf "a is not equal to 60\n";
}
elsif($a == 60)
{
# if condition is true
printf "a is equal to 60\n";
}
else
{
# if none of the condition matches
printf "The value of a is $a\n";
}
a is not equal to 60
输出 :
a is not equal to 60