for循环中的复合条件
以下两个构造有什么区别?我得到了每个不同的输出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦任一子条件变为 false,第一个条件就会结束循环(因此
counter >= numberIfFolds
或counter == currentFold
)。仅当counter >= numberOfFolds
时,第二个循环才会终止。但是,它会检查counter == currentFold
是否存在,如果是,则跳过执行这些语句。不过,循环仍将继续。The first condition will end the loop as soon as either sub-condition becomes false (so
counter >= numberIfFolds
orcounter == currentFold
). The second loop will only terminate whencounter >= numberOfFolds
. It will, however, check ifcounter == currentFold
and skip executing those statements if it is. The loop will continue, though.在第一个示例中,当
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 tocurrentFold
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.