Can someone explain why the output of this program is false??
x && y gives 1. Still the output is false.
#include <stdio.h>
int main()
{
int x = 1, y = 2;
if(x && y == 1)
{
printf("true.");
}
else
{
printf("false.");
}
return 0;
}
Because == has a higher precedence than && So first this get's evaluated:
x && (y == 1)
y == 1 // 2 == 1
//Result: false
Which is false and then second:
x && false //1 && false
//Result: false
So the if statement will be false
For more information about operator precedence see here: http://en.cppreference.com/w/cpp/language/operator_precedence
if(x && y == 1)
Is the same as
if( ( x != 0 ) && ( y == 1 ) )
Here,x != 0 is true, but y == 1 is false. And since at least one of the operands of && is false, the condition evaluates to false and the else part executes.
It clearly stated X = 1 & Y = 2;
Now with your expression
X && Y == 1
The expression is evaluated as
Y == 1 (Precedence Rule, Also output is False)
X != 0 (Its True)
Now && is Logical And Operator, so it evaluates to True only if both the parts in expression evaluates to True!!!
It's okay to false, then 2 and 2 and it is different from one.
What you're asking is whether both x and y both are worth 1. If this happens say true but false
Related
Say you have this nested if-statement:
int *x;
int y;
if (x == NULL || y > 5)
{
if (x != NULL)
// this should print if x != NULL and y > 5
printf("Hello!\n");
// this should only print if x == NULL.
printf("Goodbye!\n");
}
return 0;
Here, if either statement is true, it will return the same value (0). We should only print "Goodbye" if the left side of the outside if statement is true, regardless of whether the right hand side is true or false. Is it possible to eliminate the inside if-statement by short-circuiting, turning this into a single if-statement?
If I have understood correctly what you need is the following
if ( x == NULL )
{
printf("Goodbye!\n");
}
else if ( y > 5 )
{
printf("Hello!\n");
}
Otherwise if the first compound statement includes other statements that have to be executed in case when x == NULL or y > 5 then the if statements can look like
if ( x == NULL || y > 5)
{
// common statements that have to be executed when x == NULL or y > 5
//...
if ( !x )
{
printf("Goodbye!\n");
}
else
{
printf("Hello!\n");
}
}
This will only print Goodbye if x is NULL and y > 5
if (x == NULL || y > 5)
{
if (x != NULL)
// this should print if x != NULL and y > 5
printf("Hello!\n");
else
// this should only print if x == NULL.
printf("Goodbye!\n");
}
#include<stdio.h>
void main() {
int x = 0,y = 0,k = 0;
for(k = 0; k < 5; k++){
if(++x > 2 && ++y > 2) x++;
}
printf("x = %d and y = %d",x,y);
}
I'm not able to understand how the above piece of code generates x = 6 and y = 3.
RESOLVED : I didn't know that when there is &&, if the first statement evaluates to false the second will not be executed.
&& is a short-circuit operator.
The first time through the loop, only ++x is evaluated.
The second time through the loop, only ++x is evaluated.
The third time through the loop, both are evaluated.
...
Not related to your question, but please read What should main() return in C and C++? int.
c enables short circuit, and && is an operator that follows that. So, this:
if(++x > 2 && ++y > 2)
says:
Increment x by 1.
If x is greater than 2 (thus the first operand of && is true),
evaluate the second operand.
The second operand says to increment y by 1, and if y > 2 is
true, then the whole if condition will be true.
Your code is equivalent to this:
#include <stdio.h>
int main() {
int x = 0, y = 0, k = 0;
for(k = 0; k < 5; k++){
x = x + 1;
if(x > 2)
{
y = y + 1;
if(y > 2)
{
x = x + 1;
}
}
}
printf("x = %d and y = %d", x, y);
return 0;
}
&& is the short-circuit operator.
if ( ++x > 2 && ++y > 2 )
in this if statement the second operand will be evaluated only if the first one is true.
when k=0 X will be incremented by 1. Now x value is 1. x > 2 is false. So Y won't increase.
When k=1 X will be incremented by 1 . Now X value is 2 . X > 2 is false. So Y won't increase.
when k=2 X will be incremented by 1 . Now X value is 3 . X > 2 is true . So Y will be incremented by 1 . Now Y value is 1 . but Y > 2 is false . So total if condition is false.
when k=3 X will be incremented by 1 . Now X value is 4 . X > 2 is true . So Y will be incremented by 1 . Now Y value is 2 . but Y > 2 is false . So total if condition is false.
when k=4 X will be incremented by 1 . Now X value is 5 . X > 2 is true . So Y will be incremented by 1 . Now Y value is 3 . Y > 2 is true . So total if condition is true. Then X will be incremented by 1.
The final answer is X=6 and Y=3 .
if(++x > 2 && ++y > 2)
In this line if first condition is false it will not evalute second condition. So first condition is false until value of x is 3
Why does this following program print "Yes" instead of "No"?
None of the variables is initialized to 2.
bool hello = 0;
int a = 1;
int b = 3;
int c = 4;
int d = 5;
if (a || b || c || d == 2) {
hello = 1;
}
if (hello == 1) {
printf("Yes");
}
if (hello == 0) {
printf("No");
}
return 0;
}
The statement
if (a || b || c || d == 2)
is equivalent to:
if (a != 0 || b != 0 || c != 0 || d == 2)
The equality comparison does not automatically distribute across all the variables. If you want to do that, you need to perform all the comparisons explicitly:
if (a == 2 || b == 2 || c ==2 || d == 2)
The expression (a || b || c || d == 2) evalutates to true because it treats a, b, c as booleans, and any non-zero integer is true.
You have given logical operator in the expression It means that if non zero value came then the expression is true. Then hello=1 is set and in next f statement it prints YES
You just meet the short circuit behavior of logical expressions OR.
The order of evaluation of logical OR || is left to right.
So in the following expression:
left || right
if left = true then right will never going to be executed (short circuit). In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, a which is 1 is true. So, take a look:
if (a || b || c || d == 2)
if (true || bla bla bla) //rights are not even checked!
if (true)
hello = 1;
Tada! So the program print "Yes"!
None of the variables is initialized to 2.
Yes of course! But your if condition is not going to check that. To do so, try this:
if (a == 2 || b == 2 || c ==2 || d == 2) {
//...
because if judge num is not zero , if think this is true. so your code
if (a || b || c || d == 2)
like
if ( true || true || true || false)
the result is true, programe print "YES"
I have the below code to choose sin or cos to be integrated,
while( x !=1 || y !=(1||0) ){
printf("Sin (1) or Cos (0)?\n");
x = scanf("%d",&y);
_flushall();
if(y==1){
printf("Sin set\n");
}
else if(y==0){
printf("Cos set\n");
}
}
However the
y!= (1||0)
never evaluates to true for y == 0 , can someone explain what's wrong here? Thanks.
You need (y != 1 && y != 0) (or similar, it depends on what you really mean to express there). The || operator is being applied to the operands 1 and 0. Put another way, y != (1 || 0) means "Do (1 || 0) then do y != result".
You are attempting to effectively code directly Boolean algebra, and C doesn't accept it in the manner you've provided.
while( x !=1 || y !=(1||0) )
should be
while( (x!=1) || ( (y!=1) || (y!=0) ) )
Never underestimate the value of using excess parentheses in C. The optimizer will likely optimize the code to be more efficient anyways.
The part of code that generates this error evaluates as follows:
LHS (left hand side), RHS (right hand side)
LHS = y
!= (1||0) [definition given]
!= (1) [b/c (1||0) = (1)]
y != (0||1)
is equivalent to
y != 1
since 0||1 is 1. You'll need two comparisons if you want y != 0 or y != 1.
#include <stdio.h>
int main () {
int x, y, z;
x = y = z = 1;
++x || ++y && ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 2 y = 1 z = 1
//why is 'x' only incrementd?
x = y = z = -1;
++x || ++y && ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 0 y = 0 z = -1
//why are 'x' and 'y' incremented?
x = y = z = 1;
++x && ++y || ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 2 y = 2 z = 1
//why is 'x' only incrementd?
x = y = z = -1;
++x && ++y || ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 0 y = -1 z = 0
//why are 'x' and 'z' incremented?
//Does this incrementation depend on the value stored in the variable?
}
The reason is that && and || short circuit.
That is, once they know what the end result of the boolean expression will be, they stop evaluating.
So, if you do 1 || x++, then x will never be incremented, because any non-zero boolean value is a true value.
Likewise, if you do 0 && x++, x++ never will get executed either.
See also:
Short-circuit evaluation on Wikipedia
|| and && short-circuit. What that means is that they perform as little work as possible to return their value, only executing the right side if the left side doesn't nail the answer.
For instance:
1 || anything();
In this case, anything() will never execute, because || can simply return as soon as it evaluates the 1; no matter what anything()'s return value, the return value of || in this expression can never be 0.
Similarly:
0 && anything_else();
Here, anything_else() will never execute, because && already knows that its value can never be anything but 0.
In your examples, the ++ preincrements don't actually affect the short-circuiting, except to hide the values that the boolean short-circuit operators are actually making their decisions on.
In general the answer to your questions is that if you have the code A||B
If A evaluates to true then B is NEVER evaluated.
So in your first case if ++x is true (in your case x==2 so it is true) then the rest of the expression is never evaluated
Similarly in the second case ++x evaluates to 0 which is false thus the second art of the expression ++y must be evaluated (and this also evaluates to 0 which is false); no wis a similar manner to the || operator if the first operand of the && operator is false then there is no need to evaluate the second operand - thus the third term is never evaluated.
The same logic can be applied to the third case.
This has to do with the way c short-circuits the || and && operators.
For example, if you have A && B, the program will evaluate A. If that turns out to be false, we already know the result will be false no matter what's in B, so B is never evaluated. If you have C || D and C evaluates to true, you know the result is true no matter what's in D, so D is never evaluated.
In this code, the increments of variables are used in conjugation with expressions.
This is to avoid using if-else structure.
Here is the examples where precedence of operators are clarified:
++x || (++y && ++z);
++x || (++y && ++z);
(++x && ++y) || ++z;
(++x && ++y) || ++z;
the expressions are evaluated from left to right.
In the first example, the program already know the whole expression will yield true after ++x, and the program will not bother to evaluate the rest of the expression. C uses something called short-circuit evaluation.
In C++ or C:
True is 1 or any non-zero value
False is 0 or any negative value
These conditions are short-circuited - so when C++/C comes across the first true part - it breaks and doesn't evaluate the rest.
x = y = z = 1;
++x || ++y && ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 2 y = 1 z = 1
//why is 'x' only incrementd?
x is true (2) and the rest of the condition is not evaluated
x = y = z = -1;
++x || ++y && ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 0 y = 0 z = -1
//why are 'x' and 'y' incremented?
x is false (0) so then y is evaluated which is false (0) - since it is false it is not checked further..
x = y = z = 1;
++x && ++y || ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 2 y = 2 z = 1
//why is 'x' only incrementd?
x is true (2 which is non zero)
y is true (2) - since x and y are true - the first part is true so it doesnt evaluate z.
x = y = z = -1;
++x && ++y || ++z;
printf ("x = %d\t y = %d\tz = %d\n", x, y, z);
//op : x = 0 y = -1 z = 0
//why are 'x' and 'z' incremented?
it is evaluated as (x && y) || z
x is evaluated and it is false (0)
z is then evaluated and it is false(0)
the result of this condition is actually false.
I may be completely wrong but's how I understand this.
Hope it helps..