0
What's happening in line 6,7 anyway ? Please help !
2 Antworten
+ 7
In line 6:
a is 1, b is 1
The expression is: int c = a || --b;
As a has value 1 which evaluates to true, so c will get equal to 1.
Remember that --b will not be checked and executed.
It's because in logical OR operator, if the first condition is true then compiler will not check the next condition after OR operator.
That's why c is equal to 1.
Now in line 7:
Here first a = 1, b = 1
Expression is int d = a-- && --b;
As there is AND operator in the expression, both conditions will be checked if the first condition is true.
Since we are using postfix decrement operator with a, so the value of a remain same in this statement i.e 1 which evaluates to true.
Now compiler will move to the next condition i.e --b, here we are using prefix decrement operator so the value of b will be decremented first which will become 0 and condition evaluates to false.
So, logically (true && false = false) which is equal to 0.
So d will be equal to 0.
+ 4
int a=1; //a=1
int b=1; //a=1,b=1
int c= a||--b; //a=1,b=1,c=1
int d= a--&&--b; //a=0,b=0,c=1,d=0