英文:
difference of true or false in variable
问题
If I change the variable 'isOver' value to true, I will also change the while condition to (isOver) and else to isOver = false;
我只想知道为什么一开始使用了'false'?
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let names = [];
let isOver = false;
while (!isOver) {
let name = prompt("Enter another name or press cancel.");
if (name != null) {
names.push(name);
} else {
isOver = true;
}
}
for (let i = 0; i < names.length; i++) {
console.log(names[i]);
}
<!-- end snippet -->
If I change the vaiable 'isOver' value to true, I will also change the while condition to (isOver) and else to isOver = false;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let names = [];
let isOver = true;
while (isOver) {
let name = prompt("Enter another name or press cancel.");
if (name != null) {
names.push(name);
} else {
isOver = false;
}
}
for (let i = 0; i < names.length; i++) {
console.log(names[i]);
}
<!-- end snippet -->
I just want to know why used 'false' in the first place?
答案1
得分: 1
以下是翻译好的内容:
虽然从技术上讲两种方式都可以,但原始代码的编写方式在词法上更有意义:
while (!isOver)
也就是说,持续执行直到完成。如果要切换到使用第二个代码块,更改变量名称会有所帮助,例如:
while (notOver)
这不会影响代码的执行,但对未来的阅读者/维护者来说有很大的区别。
个人建议仍然使用第一种方式,利用语言的特性来定义“不”(!
),因为当你设置标志时,我认为这种方式:
isOver = true;
比以下方式更容易阅读和理解:
notOver = false;
英文:
While either way works technically, the way the original code is written makes more sense lexically:
while (!isOver)
ie, keep doing this until it's finished. If you want to switch to using the 2nd block, a name change of the variable would help, eg:
while (notOver)
It makes no difference to the execution of the code, but a world of difference to future readers/maintainers of the code.
Personally I'd stick with the first one, using the features of the language to define the not (!
), as when you're setting the flag, I believe this:
isOver = true;
is easier to read and make more sense of than:
notOver = false;
答案2
得分: 0
在代码的第一个版本中,isOver
最初被设置为 false,意味着循环尚未结束,应该继续运行。
while 循环的条件检查 isOver
不为 true
(!isOver
),因此循环将持续运行,直到 isOver
变为 true
。
这发生在用户不输入名称时,此时 isOver
被设置为 true
,循环结束。
在代码的第二个版本中,您反转了逻辑。isOver
现在最初被设置为 true
,意味着循环已经结束。
然而,while 循环的条件现在检查 isOver
是否为 true
,因此只要 isOver
为 true
,循环就会继续运行。这一次,当用户不输入名称时,isOver
被设置为 false
,循环结束。
之所以最初使用 false
,更多地是为了提高代码的可读性和直观理解。大多数开发人员会理解标志 isOver
代表完成,因此在任务完成时将其设置为 true
是有意义的。这就是为什么最初将 isOver
的初始状态设置为 false 的原因 - 因为任务(在这种情况下是 while 循环)尚未完成。如果反转这种逻辑,可能会使以后阅读您代码的其他开发人员感到困惑。
英文:
In the first version of the code, isOver
is initially set to false, meaning the loop is not over and should continue to run.
The while loop condition checks if isOver is not true
(!isOver
), so the loop will continue running until isOver
becomes true
.
This happens when the user doesn't enter a name, at which point isOver
is set to true, and the loop ends.
In the second version of the code, you've inverted the logic. isOver
is now initially set to true
, meaning the loop is over.
However, the condition in the while loop is now checking if isOver
is true
, so the loop will continue running as long as isOver
is true
. This time, when the user doesn't enter a name, isOver
is set to false
, and the loop ends.
The reason to use false
in the first place has more to do with readability and intuitive understanding of the code. Most developers will understand the flag isOver
to represent completion, so having it set to true
when the task is complete makes sense. This is why the initial state of isOver
is false in the first place - because the task (in this case, the while loop) is not yet complete. If you invert this logic, it might confuse other developers who read your code later.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论