+ 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; }
7 ответов
+ 5
b is equal to one, so condition is met.
Thus, 1*1=1 is printed, followed by the value of a, 1.
+ 5
It outputs first 1, and then again 1 in the same line... so it's 11 :)
+ 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 =.
+ 2
you need a second = to compare two values.
Currently you are assigning a the value of true which is one.
+ 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