Thomas Geraghty
Thomas Geraghty

Reputation: 13

Can't understand this if statement

I've a C university exam coming up next week and i was looking at old exam papers a one of the questions gives this fragmented bit of code.

int a=2, b=-1, c=0;

   if (a-2||b&&c||a){

     printf("True\n");

   } else {

     printf("False\n");

   }

We have to determine what the output of this code will be but the if statement makes no sense to me any if statement I've come across has been very specific like saying

if( x == 0)

I don't know what this is looking for my only assumption is that its going to be always true. Am I right or is there more to it then that?

Upvotes: 0

Views: 215

Answers (7)

Kevin Ano
Kevin Ano

Reputation: 1

In almost every programming language as far as I know 0 means false and 1 means true.

So coming up to your question: you have used && and || operators. Both of these are called Logical operators. Now first block of yours is a-2||b :- 2-2||-1 so 0||-1. Now since the right expression of || is -1 the or operator will return 1 i.e. True because one of the values of 0 and -1 is non-zero 0 i.e. -1.

Therefore the expression resolves to 1&&c||a :-

Now c=0, therefore 1&&0 returns a 0 because && will only return 1 if both the expressions right and left of it are non zero. So expression becomes 0||2 :-

Now since || (or operator) requires only one of operands either on right or left side to be non zero hence 0||2 returns 1.

Now your if (a-2||b&&c||a) statement resolves to

if (1)
{

     printf("True\n");  }
else......

Therefore since 1 means TRUE the if statement will execute and you will get output as True.

Upvotes: -1

glglgl
glglgl

Reputation: 91149

This assignment has two goals:

  • to show what booleans are in C: Essentially they evaluate to ints with false mapping to 0 and true mapping to 1. In turn, any numeric or pointer value can be used in an integer context, with the respective zero value (0, 0.0, NULL (pointer), 0.0f, 0L etc.) evaluating as false and all others as true.
  • to show the precedence of operators

&& has a higher precedence than ||, so this statement is equivalent to

a-2 || (b&&c) || a

which will evaluate to true if any of the values is true.

As a==2, a-2 is 0. c is 0, so b && c is 0 as well.

So we have 0 || 0 || a, which is true as a is 2.

Upvotes: 12

Dinesh Agrawal
Dinesh Agrawal

Reputation: 346

you need to understand two things before solving this problem that is operator precedence and associativity of operators

operator precedence tells c compiler that which operation to perform first. and if two operators have same precedence than associativity tells evaluate left to right or right to left in you expression

int a=2, b=-1, c=0;

   if (a-2||b&&c||a){

you can think it as

    if((a-2)||(b&&c)||a){}
means - has top precedence so it will solved first
reduced to if(0||(b&&c)||a){}
then && has higher precedence so
reduced to if(0||false||a)
then the associativity is left to right so 
reduced to if(false||a)
that is(false||2)
return true

Upvotes: 0

Anmol Kumar
Anmol Kumar

Reputation: 121

In c language integers 0 is treated as false and any non-zero integer value is true but it should be noted that it is language specific and the sme statement will show compilation error in java as java is more strict and integers are not converted to booleans.

Talking about the above assignment problem the expression inside if statement will evaluate to true as (a-2||b&&c||a) is same as (2-2||-1&&0||2) which is same as (0||0||2) which is evaluated as (false||false||true) and hence the entire expression evaluates to true. hope it helps.

Upvotes: 1

Yassine Badache
Yassine Badache

Reputation: 1851

Most languages interprets non-zero integers as true and zero as false, so here you would have to calculate each one of the terms. Without any parenthesis, I would suggest that the && statement is taken in account first. So we have:

if (2-2 // gives zero
|| // OR
-1 && 0 // -1 AND 0 gives false
|| // OR
a) // Which is 2, which is true

So you're right, this statement is always true. This exercice was about showing predecence orders, and the fact that everything is numerical, even in boolean logic. This is really important for you to understand.

If the predecence was the other way around (|| > &&), you must understand that it would have been false instead. I think this example's whole point is here.

(a-2 || b) && (c || a)
false && true
--> false

Upvotes: 2

yajnesh
yajnesh

Reputation: 2129

int a=2, b=-1, c=0;

int first=a-2; //0 -> false
bool second= b&& c; // nonZero&&zero -> true&&false -> false
int third = 2; //  nonZero -> true

// false|| false|| true -> true
   if (first || second || third ){

     printf("True\n");

   } else {

     printf("False\n");

   }

Upvotes: 0

Jeff Y
Jeff Y

Reputation: 2466

You need to understand that truth and falsity in C is always numerical.

https://www.le.ac.uk/users/rjm1/cotter/page_37.htm

Namely, anything that evaluates to numerical zero is false, and anything that evaluates to numerical non-zero is true.

Upvotes: 1

Related Questions