• Post Reply Bookmark Topic Watch Topic
  • New Topic

return() call  RSS feed

 
harshal deolekar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[See UseCodeTags]

i think output is 7.
if(b) gives true so (i/7); becomes (343/7); which returns 49.

but after that return(i/49) => (343/49) ..does it have any significance ?

answer should be 49
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshal deolekar wrote:answer should be 49

Perhaps I'm misunderstanding something, but the answer is 49, so what exactly is your problem/question ?
 
harshal deolekar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is 49
but i am not able to understand how?

does the second return(i/49); come into picture? if it comes answer should be 7.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshal deolekar wrote:does the second return(i/49); come into picture? if it comes answer should be 7.

No. A method will stop executing and return to its caller as soon as it reaches a return statement.*
So your method returns the value of i/7 if b evaluates to true and the value of i/49 if b evaluates to false.





*Unless the return is in a try/finally block, in which case any code in the finally block will be run before the method returns.
 
harshal deolekar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not very good style code there. Have a look at better style here. If you write
return b ? i / 7 : i / 49;
it becomes clearer. As you have seen from the link, that is identical to what you wrote (or maybe i / 49 : i / 7 )
To be pedantic: it is not a return() call, but a return call. Remember return is a keyword, so you cannot use it as a method name, so the () is part of the expression, not attached to the return word. In fact the precedences are such that you can remove all the () from those return statements and get exactly the same results.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!