[英]if-else condition in C
In C language, what is the result of x=0
if I put it in if-else
condition? 在C语言中,如果我把它放在
if-else
条件下, x=0
的结果是if-else
? if it is represent false
or if this assignment is finished, then represent true
? 如果它表示
false
或者这个赋值完成,那么表示true
吗?
my colleague write code as: 我的同事写代码为:
if(condition = 0)
{
//do process A
}
else
{
// do process B
}
obviously, this code is wrong, I know it should be condition == 0
or ((condition=foo()) == 0)
but my assumption is program should always do process A because i think if(condition = 0)
should always return true since this is set value 0
to variable condition
and this set process should be true. 显然,这段代码是错的,我知道它应该是
condition == 0
或((condition=foo()) == 0)
但我的假设是程序应该总是进行处理A,因为我认为if(condition = 0)
应该总是返回true,因为这是将值0
设置为可变condition
并且此set进程应为true。 However, program always do process B, that means if
use the variable condition
value and my assumption is wrong. 但是,程序总是执行进程B,这意味着
if
使用变量condition
值并且我的假设是错误的。
Then I did a another test code: 然后我做了另一个测试代码:
if(condition = 2) //or other none-zero value
{
//do process A
}
else
{
// do process B
}
this time, program always do process A. 这一次,程序总是做过程A.
My question is why if-else
condition does not use the operation value of condition but use the left variable after setting? 我的问题是为什么
if-else
条件不使用条件的操作值但是在设置后使用左变量?
when you assign 0 to variable condition it becomes false as 0 represents false and any non-zero value represents true.so, when you assign 0 else condition is executed and when you assign 2 condition represents a true statement so, it executes... 当你为变量条件赋值0时,它变为false,因为0表示false,任何非零值表示true.so,当你赋值0 else条件被执行时,当你赋值2条件代表一个真正的语句时,它执行...
if(condition = 0)
after assigning value 0 to condition it becomes 在赋值0之后,它变为
if(condition)
and as it is false, it doesn't execute.but, when condition = 2, it works in the same way and become true .so, the if condition is executed then. 并且因为它是假的,所以它不会执行。但是,当condition = 2时,它以相同的方式工作并变为真。然后,执行if条件。
You use wrong operator. 你使用错误的运算符。
a = 10; a = 10;
The "equals" operator means "assign value to". “等于”运算符意味着“赋值给”。
Now to compare two operands, a and b, to see if a = b, you use another operand. 现在比较两个操作数a和b,看看是否a = b,你使用另一个操作数。 That operand is double equals (==).
该操作数是double equals(==)。
if(variable == 0)
{
//do process A
}
else
{
// do process B
}
Now look. 现在看。
If the variable with name "variable" has value = 8 that is not equal to 0, thus the whole "variable == 0" expression if FALSE. 如果名为“variable”的变量的value = 8不等于0,那么整个“variable == 0”表达式(如果为FALSE)。 So proccess B will run.
进程B将运行。
If otherwise variable is equal to 0 indeed, proccess A will be executed because "variable == 0" is true. 如果否则变量确实等于0,则将执行进程A,因为“variable == 0”为真。
It's like: 就像是:
if(EXPRESSION)
{
// EXPRESSION IS TRUE
}
else
{
//EXPRESSION IS FALSE
}
Got it? 得到它了? :)
:)
In C, assignment is an expression that returns the set value; 在C中,赋值是一个返回设定值的表达式; ie
x = 2
will result in 2. 即
x = 2
将导致2。
This allows you to do something like this: 这允许你做这样的事情:
unsigned char ch;
while((ch = readFromFile(f)) != EOF) {
// do something with ch
}
It also allows you to shoot yourself in the foot if you accidentally mistype ==
as =
, which is why this 'feature' doesn't appear in a lot of other languages. 如果你不小心错误输入
==
as =
,它还允许你用脚射击自己,这就是为什么这个'功能'不会出现在很多其他语言中的原因。
In your first loop, the expression condition = 0
will always result in 0
, causing the else
branch to be taken. 在第一个循环中,表达式
condition = 0
将始终为0
,从而导致执行else
分支。 Similarly, condition = 2
results in 2
, which causes the true branch to be taken. 类似地,
condition = 2
导致2
,这导致采用真分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.