while循环条件的一个疑问(已经明白)
时间:2010-09-19
来源:互联网
本帖最后由 gaochong 于 2010-09-19 14:45 编辑
自己应该也算是perl入门的人了,但看到一个while循环条件后搞不明白,请各位指点。
如下子例程中的while循环条件中 $n=$n-1 怎么理解?
sub mask {
my $n=shift;
my $j;
while ($n=$n-1) {
$j++;
}
return $j;
}
print mask(2);
自己应该也算是perl入门的人了,但看到一个while循环条件后搞不明白,请各位指点。
如下子例程中的while循环条件中 $n=$n-1 怎么理解?
sub mask {
my $n=shift;
my $j;
while ($n=$n-1) {
$j++;
}
return $j;
}
print mask(2);
作者: gaochong 发布时间: 2010-09-19
本帖最后由 justagain 于 2010-09-19 14:35 编辑
回复 gaochong
共同学习一下,我的理解是
sub mask {
my $n=shift;
my $j;
while ($n=$n-1) { #这里因为$n=$n-1始终为真,$n开始递减,while一直会运行下去,一直到$n-1之后变成0。
$j++; #$j值会不断加1
}
return $j; # 返回最终的$j值
}
print mask(2); #当指定了值为2后, n开始从2递减,运行一次后,j值为1;第二次停住运行。直接返回$j值,为1
整个程序可以看成是在$n递减的同时,将$j进行递增的操作。
不知道我的理解是否正确。
回复 gaochong
共同学习一下,我的理解是
sub mask {
my $n=shift;
my $j;
while ($n=$n-1) { #这里因为$n=$n-1始终为真,$n开始递减,while一直会运行下去,一直到$n-1之后变成0。
$j++; #$j值会不断加1
}
return $j; # 返回最终的$j值
}
print mask(2); #当指定了值为2后, n开始从2递减,运行一次后,j值为1;第二次停住运行。直接返回$j值,为1
整个程序可以看成是在$n递减的同时,将$j进行递增的操作。
不知道我的理解是否正确。
作者: justagain 发布时间: 2010-09-19
while (expression)
当里面的表达式值不为0的时候,就为真,会执行里面的循环
当表达式值为0,说明表达式为假,不会执行循环
当里面的表达式值不为0的时候,就为真,会执行里面的循环
当表达式值为0,说明表达式为假,不会执行循环
作者: 99超人 发布时间: 2010-09-19
2.6.1. Boolean Values
You may use any scalar value as the conditional of the if control structure. That's handy if you want to store a true or false value into a variable, like this:
$is_bigger = $name gt 'fred';
if ($is_bigger) { ... }
But how does Perl decide whether a given value is true or false? Perl doesn't have a separate Boolean data type as some languages have. Instead, it uses a few simple rules:[*]
[*] These aren't the rules Perl uses but are rules you can use to get the same result.
If the value is a number, 0 means false; all other numbers mean true.
If the value is a string, the empty string ('') means false; all other strings mean true.
If the value is another kind of scalar than a number or a string, convert it to a number or a string and try again.[]
[] This means that undef (which we'll see soon) means false, and all references (which are covered in the Alpaca book) are true.
There's one trick hidden in those rules. Because the string '0' is the same scalar value as the number 0, Perl has to treat them the same. That means that the string '0' is the only nonempty string that is false.
If you need to get the opposite of any Boolean value, use the unary not operator, !. If what follows it is a true value, it returns false; if what follows is false, it returns true:
if (! $is_bigger) {
# Do something when $is_bigger is not true
}
You may use any scalar value as the conditional of the if control structure. That's handy if you want to store a true or false value into a variable, like this:
$is_bigger = $name gt 'fred';
if ($is_bigger) { ... }
But how does Perl decide whether a given value is true or false? Perl doesn't have a separate Boolean data type as some languages have. Instead, it uses a few simple rules:[*]
[*] These aren't the rules Perl uses but are rules you can use to get the same result.
If the value is a number, 0 means false; all other numbers mean true.
If the value is a string, the empty string ('') means false; all other strings mean true.
If the value is another kind of scalar than a number or a string, convert it to a number or a string and try again.[]
[] This means that undef (which we'll see soon) means false, and all references (which are covered in the Alpaca book) are true.
There's one trick hidden in those rules. Because the string '0' is the same scalar value as the number 0, Perl has to treat them the same. That means that the string '0' is the only nonempty string that is false.
If you need to get the opposite of any Boolean value, use the unary not operator, !. If what follows it is a true value, it returns false; if what follows is false, it returns true:
if (! $is_bigger) {
# Do something when $is_bigger is not true
}
作者: gaochong 发布时间: 2010-09-19
本帖最后由 珞水的大叔 于 2010-09-19 15:03 编辑
复制代码
等于
复制代码
- while ($n=$n-1) {
- $j++;
- }
- $n=$n-1;
- while ($n != 0) {
- $j++;
- $n=$n-1;
- }
作者: 珞水的大叔 发布时间: 2010-09-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28