#include iostream
using namespace std;
int main(){
int a=5,b=8;
if(a=5||b==2){
cout a b;
}
}
why its giving 1 and 8 as output??
Answers
Answered by
0
Answer:
Here the program actually isn't working how you are thinking
you should see associativity and precedence of operator
Actually the logic in if statement as not what it seems
if(a=5||b==2)
doesn't check if a = 5 or b == 2
actually it's a = ( 5 || b== 2)
Now this or operator returns 1 if any of the conditions is true and 0 if both are false
Now in logical relation any positive number is considered true and so 5 gives output as true (try in any program) and even single true condition lead to output 1 by or operator and hence a is assigned with 1
And you get output as 18 not 58
Explanation:
if your condition was if (a==5 || b==2) then output will be 58
Hope it helps :-)
Similar questions
English,
5 months ago
Chemistry,
5 months ago
Social Sciences,
5 months ago
English,
10 months ago
Chemistry,
10 months ago