Perl教程
Perl控制语句
Perl高级

Perl Switch

Perl Switch语句

perl switch 语句用于从多个条件执行代码。 perl 中没有 case 或 switch 语句。相反,我们使用 'when' 代替 case 和 'given' 代替 case。
perl 语言中 switch 语句的语法如下:
given(expression){  
when (value1)
{//code to be executed;}  
when (value2)
{//code to be executed;} 
when (value3)
{//code to be executed;}
default
{//code to be executed if all the cases are not matched.}    
}  

Perl Switch Example

来看一个简单的c语言switch语句的例子。
use feature qw(switch say);
print"Enter a Number\n ";
chomp( my $grade = <> );
given ($grade) {
    when ('10') { say 'It is 10' ;}
    when ('20') { say 'It is 20' ;}
    when ('30') { say 'It is 30' ;}
    default { say 'Not 10, 20 or 30';}
}
输出:
Enter a Number
10
It is 10
Enter a Number
55
'Not 10, 20 or 30'
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4