Computer Science, asked by sachinnarang, 7 months ago

#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 sswaraj04
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