Gem
Gem

Reputation: 113

Logical operator in if statement

Why does this statement returns TRUE? I thought C reads statement from left to right. What is the output of (i == 20) that results to 30 is TRUE?

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

Upvotes: 2

Views: 49

Answers (1)

lenik
lenik

Reputation: 23498

This: if(i == 20 || 30) is equivalent to if((i == 20) || 30) and 30 is always true.

If you really want to do what I think you want to do, you should have written:

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

instead.

Upvotes: 6

Related Questions