Perl 条件判断语句

if 系列语句有一下几种:

if语句语法:

if ( some_expression ){
	true_statement1;
	true_statement2;
	...
}
next_statment;
程序执行到这里时,如果 some_expression 为真,则执行大括号内的其它语句;如果 some_expression 为假,程序则跳过大括号内的语句块,直接执行 next_statment。

if else 语句语法:

if ( some_expression ){
	true_statement1;
	true_statement2;
	...
}
else{
	false_statement1;
	false_statement2;
	....
}
next_statment;
...


if elsif else 语句语法:
if ( some_expression_one ){
	one_true_statement1;
	one_true_statement2;
}
elsif ( some_expression_two ){
	two_true_statement1;
	two_true_statement2;
}
elsif ( some_expression_three ){
	three_true_statement1;
	three_true_statement2;
}
else{
	all_false_statement1;
	all_false_statement2;
}
next_statment;
...


if语句示例:
#!/usr/bin/perl
 
$number=<>;
chomp($number);
if($number > 0){
	print ("puls\n");
}
else{
	if ($number < 0){
		print ("minus\n");
	}
	else{
		print ("zero\n");
	}
}

unless语句
unless语句与if语句相反,条件为假时执行大括号中的语句。

unless语法:

unless(some_expression){
	false_statement1;
	false_statement2;
}

unless( some_expression){
	false_statement1;
	false_statement2;
}
else{
	true_statement1;
	true_statement2;
}
示例:
#!/usr/bin/perl   
  
$number=<>;
chomp($number);
unless($number != 0){
	print ("zero\n");
}
else{
	unless ($number < 0){
		print ("plus\n");
	}
	else{
		print ("minus\n");
	}
}




你可能感兴趣的:(Perl 条件判断语句)