英文:
Why don't we have to use two '=' operands for mentioning equality for boolean variable?
问题
当我在Java的while循环中编码时,我发现我们不必使用两个赋值操作符来表示仅对布尔变量的相等性进行赋值(我只找到这个情况)。这种情况背后的原因是什么?从现在开始谢谢。
`int a = 30;
boolean d = true;
while (a == 30) { <- 对于其他变量,我们需要像这样使用
}`
`int a = 30;
boolean d = true;
while (d == false) { <- 对于其他变量,我们需要像这样使用
}`
英文:
When I coding in a while loop in java , I discovered that we don't have to use two assignment operands for give meaning equality for JUST BOOLEAN VARİABLE(I found just it). What is the reason behind this situation? Thank you from now
` int a=30;
boolean d=true;
while (a==30) { <- We need to use like that for other variables
}`
` int a=30;
boolean d=true;
while (d=false) { <- We need to use like that for other variables
}`
答案1
得分: 2
因为赋值会解析为被赋的值。这将d
设为false
(并且解析为false
),因此while
不会进入循环体。
while (d=false) {
而且对于boolean
类型,你不需要任何=
。
while (!d) {
或者
while (d) {
都是正确的,不涉及赋值。
英文:
Because assignment resolves to the assigned value. That sets d
to false
(and evaluates to false
) and thus the while
does not enter the loop body.
while (d=false) {
And you don't need any =
for boolean
(s).
while (!d) {
or
while (d) {
are correct, and don't involve assignment.
答案2
得分: 0
如果您真正使用两个"="运算符(而不是操作数),您将获得编译器错误...
"="是赋值运算符,"=="是相等运算符,而"= ="是语法错误。
如何使用它在此答案中有解释。
英文:
If you really use two "=" operators (not operands), you would get a compiler error …
"=" is the operator for assignments, "==" is the equality operator, and "= =" is a syntax error.
How to use it is explained in this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论