6.5 – Short circuit evaluation

by subbu on September 27, 2013

Short circuit evaluation:

While evaluating AND logical operator if the first condition is true then goes to the second condition to evaluate, otherwise it will skip the evaluation of second condition and returns false as the result of total expression. Because logical AND knows it must return false anyway regardless of whether the second condition evaluates to true or false. In this case, the logical AND operator will go ahead and return false immediately without even evaluating the second condition

Similarly while evaluating OR logical operator if the first condition is true then skips the evaluation of other part and returns true as the result of total expression. Because logical OR knows it must return true anyway regardless of whether the second operand evaluates to true or false. In this case, the logical OR operator will go ahead and return true immediately without even evaluating the second condition

It is generally known as short circuit evaluation, which is done for optimization purposes.

Example:

#include<stdio.h>
int main()
{
int x;
x=10<5&&40>20||80!=20;
printf("x=%d",x);
return 0;
}

Output:
x=1

Here in this example according to precedence of operators && get higher priority over || hence the binding expression would be (10<5&&40>20)||80!=20. As the result of (10<5&&40>20) is false 80!=20 is checked and returns true(1).

Example:

#include<stdio.h>
int main()
{
int x;
x=10>5&&40>20||80!=20;
printf("x=%d",x);
return 0;
}

Output:
x=1

Here in this example according to precedence of operators && get higher priority over || hence the binding expression would be (10>5&&40>20)||80!=20. As the result of (10>5&&40>20) is true, it will return true (1) as the result of expression without checking 80!=20.

Example:

#include<stdio.h>
int main()
{
int x,y,z;
x=y=1;
z=--x&&++y;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}

Output:
x=0
y=1
z=0

While evaluating the expression – -x is 0 (false) hence returns false (0) as the result of expression without even evaluating ++y.

Example:

#include<stdio.h>
int main()
{
int x,y,z;
x=y=0;
z=++x||--y;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}

Output:
x=1
y=0
z=1

While evaluating the expression ++x is true hence returns true (1) as the result of expression without even evaluating –-y.

Quiz:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=1;
z=++x||++y&&++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=2
y=1
z=1

As the precedence of && is more than || the binding expression would be ++x||(++y&&++z). As the result of ++x is true (2) returns true as the result of expression without evaluating ++y&&++z.

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=1;
z=--x||++y&&++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=0
y=2
z=1

As the precedence of && is more than || the binding expression would be – -x||(++y&&++z). As the result of – -x is false (0), goes to check ++y&&++z. As the result of ++y is true(2) returns true(1) as the result of expression without even evaluating ++z.

3) What would be output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=1;
z=++x&&++y||++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=2
y=2
z=1

As the precedence of && is more than || the binding expression would be (++x&&++y)||++z. As the value of ++x is 2(true) goes to evaluates ++y and returns true as the result of expression (++x&&++y). As the result of (++x&&++y) is true, it returns true as the result of total expression without even evaluating ++z.

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=1;
z=++x&&++y&&++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=2
y=2
z=1

As all the logical operators in the expression have the same precedence, associativity plays a major roll in evaluation of expression. As the associativity of logical operators is from left to right, ++x evaluates first, as it is true(2) goes to evaluate ++y, as it is true (2) goes to evaluate ++z, as it is true(2) returns true(1) as the result of total expression and assigns to z.

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=-1;
z=++x&&++y||++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=0
y=-1
z=0

As the precedence of && is more than || the binding expression would be (++x&&++y)||++z. As the value of ++x is 0(false), it will skip evaluation of ++y and returns false(0) as the result of expression. As the result of && expression is false goes to evaluate ++z. As the value of ++z is 0 (false) returns false (0) as the result of expression and assigns to z.

6) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=-1;
z=++x||++y&&++z;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answer
Output:
x=0
y=0
z=0

As the precedence of && is more than || the binding expression would be ++x||(++y&&++z). As the value of ++x is 0 (false) goes to evaluate (++y&&++z). While evaluating the expression ++y&&++z, as the value of ++y is 0(false) goes to evaluate ++z. As the value of ++z is also 0(false) returns false(0) as the result of total expression and assigns to z.

Previous post:

Next post: