- Perl 基础
- Perl - 主页
- Perl - 简介
- Perl - 环境
- Perl - 语法概述
- Perl - 数据类型
- Perl - 变量
- Perl - 标量
- Perl - 数组
- Perl - 哈希
- Perl - IF...ELSE
- Perl - 循环
- Perl - 运算符
- Perl - 日期和时间
- Perl - 子例程
- Perl - 引用
- Perl - 格式
- Perl - 文件 I/O
- Perl - 目录
- Perl - 错误处理
- Perl - 特殊变量
- Perl - 编码标准
- Perl - 正则表达式
- Perl - 发送电子邮件
- Perl 高级
- Perl - Socket 编程
- Perl - 面向对象
- Perl - 数据库访问
- Perl - CGI 编程
- Perl - 程序包和模块
- Perl - 进程管理
- Perl - 内嵌文档
- Perl - 函数引用
- Perl 有用资源
- Perl - 问题和答案
- Perl - 快速指南
- Perl - 有用资源
- Perl - 讨论
Perl IF...ELSE 语句
一个 Perl if 语句可以紧随一个可选的 else 语句,它当布尔表达式为 false 时执行。
语法
Perl 编程语言中 if...else 语句的语法为 -
if(boolean_expression) {
# statement(s) will execute if the given condition is true
} else {
# statement(s) will execute if the given condition is false
}
如果布尔表达式计算为 true,则会执行 if 块代码,否则会执行 else 块代码。
数字 0、字符串 '0' 和 ""、空列表 () 和 undef 在布尔上下文中均为 false,所有其他值均为 true。通过 ! 或 not 求得的真值的否定结果返回一个特殊的 false 值。
流程图
示例
#!/usr/local/bin/perl
$a = 100;
# check the boolean condition using if statement
if( $a < 20 ) {
# if condition is true then print the following
printf "a is less than 20\n";
} else {
# if condition is false then print the following
printf "a is greater than 20\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using if statement
if( $a ) {
# if condition is true then print the following
printf "a has a true value\n";
} else {
# if condition is false then print the following
printf "a has a false value\n";
}
print "value of a is : $a\n";
当执行以上代码时,会产生以下结果 -
a is greater than 20 value of a is : 100 a has a false value value of a is :
perl_conditions.htm
广告