编程中的决策与现实生活中的决策相似。在编程中,当某些条件得到满足时,需要执行特定的代码块。编程语言使用控制语句根据特定条件控制程序的执行流程。它们用于根据程序状态的变化使执行流前进和分支。
Perl中的决策语句:
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" ; } |
输出:
Even Number
if-else语句
如果条件为真,if语句将对代码进行求值,但如果条件为非真,则会出现else语句。它告诉代码当if条件为false时该怎么做。 语法:
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" ; } |
输出:
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" ; } } |
输出:
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" ; } elsif ( $i == 15) { printf "i is 15" ; } elsif ( $i == 20) { printf "i is 20" ; } else { printf "i is not present" ; } |
输出:
i is 20
除非声明
在这种情况下,如果条件为false,则将执行语句。这个 数字0、空字符串“”、字符“0”、空列表()和未定义 都是 错误的 在布尔上下文中,所有其他值都为真。 语法:
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" ; } |
输出:
a is not equal to 10
除非另有声明
除非语句后面可以跟一个可选的else语句,该语句在布尔表达式为true时执行。 语法:
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" ; } else { # if condition is true then # print the following printf "a is equal to 10" ; } |
输出:
a is equal to 10
除非–elsif声明
除非语句后面可以跟一个可选的elsif…else语句,这对于使用单个elsif…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" ; } elsif ( $a == 60) { # if condition is true printf "a is equal to 60" ; } else { # if none of the condition matches printf "The value of a is $a" ; } |
a is not equal to 60
输出:
a is not equal to 60