elsif在Perl中如何工作? [英] How does elsif work in Perl?

查看:96
本文介绍了elsif在Perl中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Perl有'elsif'而不是'elseif'?

Why does Perl have 'elsif' and not 'elseif'?

我在做这样的事情:

$somevariable = 0;
if ($idcount==5) 
{
    # Do something
    if (somestatement is true) # 1 
    {
        $somevariable = 1;
    }
}
elsif ($idcount > 1 && somevariable = 0)
{
    # Do something else here
}

该代码永远不会出现在elsif语句中。在这种情况下, idcount 实际上是5,因此它涉及第一个if语句。但是内部的 if 语句(1)不正确,因此 $ somevariable 仍为0。由于 idcount 是5,由于 $ idcount 是,因此它也应该出现在 elsif 大于1且 $ somevariable 仍为0。

The code never comes to the elsif statement. In this case idcount is actually 5, so it comes to the first if statement. But an inner if statement (1) is not true, so $somevariable is still 0. Since idcount is 5, it should come to elsif as well since $idcount is greater than 1 and $somevariable is still 0.

也许我只是累了而看不到明显的东西。 / p>

Maybe I am just tired and not seeing something obvious.

推荐答案

不, elsif 中的任何东西都只能执行如果先前的if / elsif条件都不成立,则为 else部分。

No, anything in an elsif only has a chance to be executed if none of the previous if/elsif conditions have been true, that's the "else" part of it.

如果您希望同时对它们进行评估,则将其设为 if 代替 elsif

If you want both to be evaluated, just make it an if instead of an elsif.

这篇关于elsif在Perl中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆