0

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;
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • 7
    Do you know how the pre and post increment operators work, and how short-circuit evaluation of logical operators work? If so, you should be able to work it out with a paper and a pencil. – Sam Varshavchik Dec 30 '20 at 22:14
  • 1
    Isn't this entire program **undefined behavior**? See [this question](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior). I thought the guarantees are thrown out the window when an auto-incremented or decremented variable is evaluated twice within the same expression. – selbie Dec 30 '20 at 22:22
  • Yeah I understand the concept but still I don't know the logics when the increment and decrement is in the condition of if. I am used to usually see some kind of if(x>1). – Everything Pro Dec 30 '20 at 22:33
  • 1
    @selbie no it is actually well-defined because short circuit logical operators are sequence points. – n. m. could be an AI Dec 30 '20 at 22:39

3 Answers3

4

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.

anotherOne
  • 1,513
  • 11
  • 20
  • 2
    Good Explanation. The fact that in a compound statement of `||` or `&&` if the first condition satisfies the condition, the second not being evaluated allows for evaluating a first condition and skipping a second that would otherwise be undefined. – David C. Rankin Dec 30 '20 at 22:57
  • @DavidC.Rankin yes it can be an helpful behaviour, but makes the code hard to read. – anotherOne Dec 30 '20 at 23:48
  • Yeah but this is an exercise supposed to be on an exam so it makes sense. – Everything Pro Jan 01 '21 at 10:47
0

&& 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.

mrflash818
  • 930
  • 13
  • 24
0

&& (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

MED LDN
  • 684
  • 1
  • 5
  • 10