Can anybody help me to explain continue, because I don't really understand.
and why this thing work only with 0 and 1? creating loop for even numbers
有人能帮我解释一下吗,继续,因为我真的不明白。为什么这个东西只对0和1起作用?为偶数创建循环
for (let i = 2; i <= 10; i++) {
if(i % 2 == 1) continue;
alert(i);
}
Thank U:)
谢谢您:)
somebody help me, I'm really confused hehehe
谁来帮帮我,我真的很困惑呵呵
更多回答
优秀答案推荐
The continue statement in JavaScript is like a skip button for loops. Imagine you're counting from 2 to 10. If you find a number that's odd (like 3 or 5), you press the skip button. This means you won't pay attention to that odd number and instead move on to the next one. So, with your code, you're counting from 2 to 10, but only showing the numbers that aren't odd. And by the way, 0 and 1 aren't odd or even – they're kind of special.
JavaScript中的Continue语句就像循环的跳过按钮。想象一下你正在从2数到10。如果你找到一个奇数(如3或5),你就按下跳过键。这意味着你不会注意那个奇数,而是转到下一个。因此,使用您的代码,您将从2数到10,但只显示不是奇数的数字。顺便说一句,0和1不是奇数或偶数--它们是一种特殊的东西。
With the continue
statements basically we say to stop the current loop execution but we also say to keep going with the next iteration. So, when the interpreter finds a continue
statement inside a loop, it stops the loop right there and skips everything under that continue
statement. Everything under continue
is ignored and the next iteration starts!
对于Continue语句,基本上我们说停止当前循环执行,但我们也说继续执行下一次迭代。因此,当解释器在循环中发现CONTINUE语句时,它会立即停止循环,并跳过该CONTINUE语句下的所有内容。CONTINUE下的所有内容都被忽略,下一次迭代开始!
Regarding your question 'why does it work only with 1 and 0' I don't know what you mean but I think you are referring to the result of the operation i % 2
. There is nothing special about 1 and 0. But I think you are missing what the %
operator does. It returns the remainder of dividing the two operands. So, in your example, it always returns 1 or 0 because you've chosen to divide every number by 2. And the remainder of an odd number divided by 2 is 1 and the remainder of an even number divided by 2 is 0. That's it. But if you change 2 with another number (let's say 5) you will see that there is no more 1 or 0 as a remainder (check the example that I've written). Take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder for the remainder operator and what it does.
关于你的问题‘为什么它只对1和0起作用’,我不知道你的意思是什么,但我想你指的是运算i%2的结果。1和0没有什么特别的。但我认为您错过了%运算符所做的工作。它返回两个操作数相除的余数。因此,在您的示例中,它总是返回1或0,因为您选择了将每个数字除以2。奇数除以2的余数是1,偶数除以2的余数是0。就这样。但是,如果您将2改为另一个数字(比方说5),您将看到没有更多的1或0作为余数(请查看我编写的示例)。看看余数操作符的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder以及它的作用。
Bye!
再见!
//This is your remainder operation with 2
for (let i = 2; i <= 10; i++) {
console.log(i % 2);
}
//This is another remainder operation with 5. You can see that the result is not only 1 or 0
for (let i = 2; i <= 10; i++) {
console.log(i % 5);
}
`
”
更多回答
我是一名优秀的程序员,十分优秀!