简体   繁体   English

if 语句中的逻辑运算符

[英]Logical operator in if statement

Why does this statement returns TRUE?为什么这个语句返回 TRUE? I thought C reads statement from left to right.我认为 C 从左到右读取语句。 What is the output of (i == 20) that results to 30 is TRUE? (i == 20) 结果为 30 的输出是 TRUE 是什么?

i = 10;
if(i == 20 || 30)
{
    printf("True");
}
else
{
    printf("False");
}

This: if(i == 20 || 30) is equivalent to if((i == 20) || 30) and 30 is always true .这: if(i == 20 || 30)等价于if((i == 20) || 30)并且30总是true

If you really want to do what I think you want to do, you should have written:如果你真的想做我认为你想做的事情,你应该写:

if(i == 20 || i == 30)

instead.反而。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM