Rabbit Out of the Hat!!, Please help explain this hocus pocus! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Rabbit Out of the Hat!!, Please help explain this hocus pocus!

If you run the code below', you'll get 1 1 as your result, please help explain to me how the value of the variable a became 1 and also how that of b became 1. Thanks. Here's the code: int main() { int a = 7, b = 1; if(a = 8 || b == 5); cout << a * b; cout << " " << a;

19th Dec 2022, 3:56 PM
DevAbdul
DevAbdul - avatar
2 Answers
+ 4
DevAbdul Single equal (=) is used to assign value so here you have assigned (8 || b == 5) to variable a inside if condition so (8 || b == 5) will return 1 so a would be 1 So a * b = 1 So 11 Note: b is already 1
19th Dec 2022, 4:31 PM
A͢J
A͢J - avatar
+ 4
DevAbdul A bit of additional info on short circuit evaluation of logical operators https://en.cppreference.com/w/cpp/language/operator_logical And about short circuit evaluation itself https://en.m.wikipedia.org/wiki/Short-circuit_evaluation
19th Dec 2022, 4:54 PM
Ipang