+ 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

23rd Mar 2021, 3:14 PM
san sheva S
11 Respostas
+ 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.
23rd Mar 2021, 3:27 PM
Arsenic
Arsenic - avatar
+ 2
Remove one equle (=) from b Then the answer will become 7
24th Mar 2021, 2:54 AM
Mohamed
Mohamed - avatar
+ 1
Tnx
23rd Mar 2021, 3:29 PM
san sheva S
+ 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
24th Mar 2021, 2:25 PM
Mohamed
Mohamed - avatar
+ 1
Tnx
24th Mar 2021, 3:23 PM
san sheva S
0
I am confused 😕
23rd Mar 2021, 3:14 PM
san sheva S
0
How the a and b values changes to 1
23rd Mar 2021, 3:15 PM
san sheva S
0
How
24th Mar 2021, 2:16 PM
san sheva S
0
san sheva S Just edit the code by removing one (=) from b b= 5 Not b==5
24th Mar 2021, 2:19 PM
Mohamed
Mohamed - avatar
0
Yeah I understand can you explain how the ans is 7
24th Mar 2021, 2:21 PM
san sheva S
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
25th Mar 2021, 6:21 AM
Deepesh Patel
Deepesh Patel - avatar