What is the output of this code and why?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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;

12th Sep 2021, 7:04 AM
Nariman Tajari
Nariman Tajari - avatar
15 Answers
+ 4
Nariman Tajari The right side of || is never executed as long as the left side of || is true
12th Sep 2021, 7:29 AM
Tim
Tim - avatar
+ 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 =)
13th Sep 2021, 2:13 PM
Rishi
Rishi - avatar
+ 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
12th Sep 2021, 8:29 AM
Rahul Yadav
Rahul Yadav - avatar
+ 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
12th Sep 2021, 8:30 AM
A͢J
A͢J - avatar
+ 2
Rahul Yadav but the output is not the answer you mentioned and the reason is explained in the comments.
12th Sep 2021, 8:31 AM
Nariman Tajari
Nariman Tajari - avatar
+ 2
Nariman Tajari yeah right🙌👍
13th Sep 2021, 3:14 PM
Rishi
Rishi - avatar
+ 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
13th Sep 2021, 4:08 PM
Tim
Tim - avatar
+ 1
Thank you nick. Solved and understood.
12th Sep 2021, 7:33 AM
Nariman Tajari
Nariman Tajari - avatar
+ 1
Especially in high speed language like c and c++ Rishi
13th Sep 2021, 2:14 PM
Nariman Tajari
Nariman Tajari - avatar
0
https://code.sololearn.com/cv8Jqc6IBMTl/?ref=app You should try running the code yourself first
12th Sep 2021, 7:22 AM
Tim
Tim - avatar
0
I ve run the code Nick but you shoud know that i m looking for the reason not the solution and last answer.
12th Sep 2021, 7:24 AM
Nariman Tajari
Nariman Tajari - avatar
0
You should also know that no one's responsible for your questions, be grateful that you even get an authentic response
12th Sep 2021, 7:25 AM
Tim
Tim - avatar
0
Ok thank you for your compiling and nice answer but give me the debugging and reason for the output if you can.
12th Sep 2021, 7:26 AM
Nariman Tajari
Nariman Tajari - avatar
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.
13th Sep 2021, 5:22 AM
TeaserCode
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.
13th Sep 2021, 10:20 AM
Nariman Tajari
Nariman Tajari - avatar