Perl 中的if-else语句,在上一篇教程中,我们了解了 perl 中的if
语句。在本教程中,我们将了解if-else
条件语句。这是if-else
语句的外观:
if(condition) {
Statement(s);
}
else {
Statement(s);
}
如果条件为真,则if
内的语句将执行,如果条件为假,则else
内的语句将执行。
示例如下:
#!/usr/local/bin/perl
printf "Enter any number:";
num = <STDIN>;
if(num >100 ) {
# This print statement would execute,
# if the above condition is true
printf "number is greater than 100\n";
}
else {
#This print statement would execute,
#if the given condition is false
printf "number is less than 100\n";
}
输出:
Enter any number:120
number is greater than 100