for循环中的复合条件

发布于 2024-11-08 02:53:51 字数 813 浏览 0 评论 0原文

以下两个构造有什么区别?我得到了每个不同的输出:

for (int counter = 0; (counter < numberOfFolds) && counter != currentFold; counter++)
        {
            if (instances[counter] < minimum)
            {
                return (currentFoldHasAtleastMinimum && true);
            } 

        }

AND

for (int counter = 0; (counter < numberOfFolds); counter++)
        {
            if (counter != currentFold)
            {
                if (instances[counter] < minimum)
                {
                    return (currentFoldHasAtleastMinimum && true);
                } 
            }
        }

从本质上讲,第二个代码块只是打破了 for 循环中的复合条件,并使用附加的 if 语句将其带入内部(我可能在这里遗漏了一些非常基本的东西,它可能是真的很愚蠢,但我认为它们是相同的)。

请帮忙。看起来它们实际上并不相同,我不明白为什么。

What is the difference between the following two constructs? I am getting a different output for each:

for (int counter = 0; (counter < numberOfFolds) && counter != currentFold; counter++)
        {
            if (instances[counter] < minimum)
            {
                return (currentFoldHasAtleastMinimum && true);
            } 

        }

AND

for (int counter = 0; (counter < numberOfFolds); counter++)
        {
            if (counter != currentFold)
            {
                if (instances[counter] < minimum)
                {
                    return (currentFoldHasAtleastMinimum && true);
                } 
            }
        }

Essentially, the second block of code, simply breaks the compound condition in the for loop and takes it inside using an additional if statement (I may be missing something very fundamental here, and it may be really stupid, but I thought they were the same).

Please help. It appears that they are in fact not the same, and I cannot figure out why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-11-15 02:53:51

一旦任一子条件变为 false,第一个条件就会结束循环(因此 counter >= numberIfFoldscounter == currentFold)。仅当 counter >= numberOfFolds 时,第二个循环才会终止。但是,它会检查 counter == currentFold 是否存在,如果是,则跳过执行这些语句。不过,循环仍将继续。

The first condition will end the loop as soon as either sub-condition becomes false (so counter >= numberIfFolds or counter == currentFold). The second loop will only terminate when counter >= numberOfFolds. It will, however, check if counter == currentFold and skip executing those statements if it is. The loop will continue, though.

玩世 2024-11-15 02:53:51

在第一个示例中,当 counter 等于 currentFold 时,循环终止。

在第二个示例中,当满足该条件时循环将继续,并且仅当 counter counter counter counter counter counter counter counter counter counter counter counter counter numberOfFolds 为 false。

In the first example, when counter is equal to currentFold the loop terminates.

In the second example, the loop will continue when that condition is met, and instead will only terminate when counter < numberOfFolds is false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文