Explain this code | SoloLearn: Learn to code for FREE!

+6

Explain this code

int a= 7; int b= 1; if(a=8 || b==5) cout<< a * b; cout<< " "<< a; // 1 1

10 Answers

New Answer

+26

if(a=8 || 1 ==5) since b =1 1==5 will return 0 (false) And since 8 is a positive integer you can assign the value 8 to a so a=8 will return true that is 1 So 1 || 0 will return 1. That is a*b will be 1*1 and a is also 1. so the final answer would be 1 1

+12

If (a = 8 || b == 5) is evaluated as if ( a = (8 || b == 5)) lhs of || is true (8 means true value, all non-zero values evaluates to true) hence rhs is not evaluated at all. So 'a' gets the value as 1 since true is convertible to 1 so if expression is reduced to if(true) cout << a * b; // a is 1, b is 1 so output = 1 then last line prints the value of 'a' after printing the space. So your final answer is 1 1

+6

+5

If value of a is 8 or value of b is equal to 5 then the next line will be executed.

+4

Aditya Pardeshi Please stop spamming in someone else thread. Delete your posts. By continuing spamming you risk your account being banned. The choice is yours. Thank you.

+1

It will generate an error because in the third line, if(a=8||b==5), you can not use an assignment statement as a condition for if. a=8 is an assignment statement

+1

if condition will be skipped because it return false, so it will execute cout<< " "<< a; equivalent with cout<<a; where int a=7; "" empty space. The output is 7. if condition use assignment operator == instead of = to avoid certain error in c++.

-2

if(a=8 || 1 ==5) since b =1 1==5 will return 0 (false) And since 8 is a positive integer you can assign the value 8 to a so a=8 will return true that is 1 So 1 || 0 will return 1. That is a*b will be 1*1 and a is also 1. so the final answer would be 1 1

-4

What can I call you

-5