+ 1
What is the output of this code and why??
int x=7,y=4; if(++x>5||++y<5) std::cout<<x<<y; else std::cout<<x*y;
15 Réponses
+ 4
Nariman Tajari
The right side of || is never executed as long as the left side of || is true
+ 3
Actually the designers want their language to be more efficient and this is one way of achieving it. If you know that one side of the or operator is true, then no matter the other side, the answer's gonna be true. So by eluding(avoiding) second condition check, they reduce the time by about half. So be careful when making conditions in and or or operator with side effects. Hope you understand =)
+ 2
What is the output of this code and why??
int x=7,y=4; if(++x>5||++y<5) std::cout<<x<<y; else std::cout<<x*y;
++x means
x=x+1;// so x=7+1
++y means
y=y+1 // so y=4+1
Output
8 4
because ++x>5 this condition is true then code will not check ++y>5 condition
so x value will increase but not y's value.
Nariman Tajari please tell me If I m wrong
+ 2
Nariman Tajari
|| (or) means if left side is true then right side will not be check.
&& (and) means if left side is false then right side will not be check
true || false => true
false || true => true
true || true => true
false || false => false
true && false => false
false && true => false
true && true => true
false && false => false
So
++x > 5 => true, x = 8, y = 4 => 84
if x = 4, y = 4; then o/p => 25 because both are false but x and y will be increment by 1
+ 2
Rahul Yadav but the output is not the answer you mentioned and the reason is explained in the comments.
+ 2
Nariman Tajari yeah right🙌👍
+ 2
Abhishek Shelar
I think you should scroll up and take a look at other people's answers, especially A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟'s explanation
+ 1
Thank you nick.
Solved and understood.
+ 1
Especially in high speed language like c and c++ Rishi
0
https://code.sololearn.com/cv8Jqc6IBMTl/?ref=app
You should try running the code yourself first
0
I ve run the code Nick but you shoud know that i m looking for the reason not the solution and last answer.
0
You should also know that no one's responsible for your questions, be grateful that you even get an authentic response
0
Ok thank you for your compiling and nice answer but give me the debugging and reason for the output if you can.
0
I think the solution is the first version, because in if condition you have operator || (it means or or). For this opeator is valid if one of statement is true then it results as true. So this condition is true. And the program compile statement under if statement.
0
Yes TeaserCode but the point A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ said was also interesting that if you put x=4 you will recieve 25 as o/p.