+ 1
#include<iostream> using namespace std; int main() { int a=7,b=1; if(a=8||b==5) {cout<<a*b<<a<<b;} cout<<" "<<a;
How the ans is 1 1
11 Answers
+ 1
Here's how things are happening inside if statement.
if(a=8||b==5)
After considering operator precedence, the statement will become
if ( a = ( 8 || ( b == 5) ) )
Which will become
if ( a = (8 || 0) )
if ( a = 1 )
And as this is assignment operator, so value of "a" is changed to 1.
+ 2
Remove one equle (=) from b
Then the answer will become 7
+ 1
Tnx
+ 1
You define a = 7, b=1
If (a=8 || b=5 )
Here nighter a =8 nor b=5
So the program will not do this step so it will jump to
cout <<" "<<a
And a is equal 7
That's all
+ 1
Tnx
0
I am confused 😕
0
How the a and b values changes to 1
0
How
0
san sheva S
Just edit the code by removing one (=) from b
b= 5
Not
b==5
0
Yeah I understand can you explain how the ans is 7
0
san sheva S Here is what happening in code
Concept is that
(Any value except 0) || (any other expression even with false condition) will return 1 (boolean True)
#include<iostream>
using namespace std;
int main()
{
int a=7,b=1; //initialisation and assignment
if(a=(8||(b==5))) // your code actually like this to compiler ,
inside if first (8||(b==5) ) evaluated since b not equal to 5 so 0 is return and || operator with 8 will give 1 which then assign to 'a' variable
{cout<<a*b<<a<<b;}//output1 here is a*b=1*1=1
a=1
b=1
111
cout<<" "<<a;// here is a=1 so output2 will be value of a which is 1
}
Output combination of output1 and output2 which is
111 1