What's happening in line 6,7 anyway ? Please help ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's happening in line 6,7 anyway ? Please help !

https://code.sololearn.com/c2h4i0c3CDcH/?ref=app

15th Aug 2019, 3:10 PM
Brutual bro
Brutual bro - avatar
2 Answers
+ 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.
15th Aug 2019, 3:31 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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
15th Aug 2019, 3:31 PM
D_Stark
D_Stark - avatar