Everything Pro
Everything Pro

Reputation: 5

C programming exercise

I am studying C in college and in some exercises like the one below I don't understand the operations of increment and decrement in an "if" statement. Why the answer is 0 2 ?

#include <stdio.h>

int main () {
  int x = 1, y = 1;

  if ((x-- || y --) && (--x || --y))
    printf ("%d %d\n",x+1,y +1);
  else if ((x++ && y++) || (++ x && ++y))
    printf ("%d %d\n",x+2,y +2);
  else
    printf ("%d %d\n",x+3,y +3);

  return 0;
}

Upvotes: 0

Views: 368

Answers (3)

MED LDN
MED LDN

Reputation: 669

&& (logical AND) Example : (x>5) && (y<5) It returns true when both conditions are true.

|| (logical OR) Example : (x>=10) || (y>=10) It returns true when at-least one of the condition is true

printf("\n%d , %d\n",x--,y--); // x=1 , y=1

So, this expression return 1 (at-least one of the condition is true)

printf("\n%d , %d\n",--x,--y); // x=0 , y=0

So, this expression return 0 (at-least one of the condition is true)

So, in the condition if

&&(--x || --y)) //y-- does not excuted 

he just excute --x ,here x=0,y=1 (y=1 because if condition verified just x-- because of OR ||)

The output is : 0 2

Difference between if and else if :

for example :

first case :

if(x == 0) ... //if x = 0 this will work and skip the following else-if statements
else if(x == 1) ...//if x not equal to 0 and if x = 1 this will work and skip the following else-if statement
else if(x == 2) ...// if x not equal to 0 or 1 and if x = 2 the statement will execute

Second case :

if(x == 0) ...//if x = 0 this will work and check the following conditions also
if(x == 1) ...//regardless of the x == 0 check, this if condition is checked
if(x == 2) ...//regardless of the x == 0 and x == 1 check, this if condition is checked

For the first case: once an else if (or the first if) succeeds, none of the remaining else ifs or elses will be tested. However in the second case every if will be tested even if all of them (or one of them) succeeds.

In your code , the first if is correct ,so the else if & else does not excuted ,if you want to excute the three condition , you should be use if in all of your conditions

Upvotes: 0

Mustafa Kubaji
Mustafa Kubaji

Reputation: 3

&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.

! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

Upvotes: 0

anotherOne
anotherOne

Reputation: 1573

The && returns 1 when both sides are non-zero. If the first expression evaluetes to 0, the second one it's not going to be executed.

The || returns 1 when at least one side is non-zero. If the first expression doesn't evaluetes to 0, the second one is not going to be executed.

The pre increment do its job before a value is used. The post increment doesn't do anything until a sequence point is found. && and || are sequence points.

Now in the first if there are two expressions divided by an &&. This means that the if will be entered only if both sides evaluetes to a non-zero value.

if ((x-- || y --) && (--x || --y))

x-- is 1, so the first || returns 1 straight away, without y-- being executed.

A sequence point is found and x becomes 0.

Now --x is -1 which is not 0. So even the second || returns 1 straight away.

Both sides of the && are non-zeros, so the if is entered with x being -1 and y being 1.

Upvotes: 4

Related Questions