Can someone explain me why output is 1 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Can someone explain me why output is 1 1?

#include <iostream> using namespace std; int a=7,b=1; int main() { if (a=8||b==5) cout <<a*b; cout<<" "<<a; return 0; }

15th Apr 2019, 11:18 AM
Coder21
Coder21 - avatar
5 Answers
+ 5
b is equal to one, so condition is met. Thus, 1*1=1 is printed, followed by the value of a, 1.
15th Apr 2019, 11:24 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
It outputs first 1, and then again 1 in the same line... so it's 11 :)
15th Apr 2019, 1:58 PM
I‘m Sarah🌹
I‘m Sarah🌹 - avatar
+ 4
Nice explanation ~ swim ~ . I always thought that 8 was assigned to a and the result (8) of this assignment was or'ed with the result of b==5 (1) giving (1) for the if condition but that would have made the output 88 which is not the case. Lesson for me is that || has higher precedence than =.
16th Apr 2019, 8:45 AM
Sonic
Sonic - avatar
+ 2
you need a second = to compare two values. Currently you are assigning a the value of true which is one.
16th Apr 2019, 10:38 AM
Farb Papier
+ 1
This one is tricky It deals with operator precedence ... In simpler words then priority order of operators It's just like the BODMAS or PEDMAS rule in Algebra In the given problem, the expression in 'if' is evaluated as a = ( "8" or "b is equal to 5) Since "8" is a numerical value, in logical operation it is considered as True So True or "anything" = True So a = 1 b is already 1 So the program prints a*b = 1*1 = 1 then it prints a = 1
17th Apr 2019, 8:06 AM
Dheeraj Lalwani
Dheeraj Lalwani - avatar