• Post Reply Bookmark Topic Watch Topic
  • New Topic

return by if  RSS feed

 
mdanish khan
Greenhorn
Posts: 3
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does----- if(...)
return a value?

I saw it in Head First Java exercise question.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"if" evaluates the condition you provide

However you can use the evaluation to return some value
 
Kieran Webster
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above can also be used to check if a condition is false, by using the following

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kieran Webster wrote:The above can also be used to check if a condition is false, by using the following



True. But the OP's question was regarding returning values
mdanish khan wrote:Does----- if(...)
return a value?
 
mdanish khan
Greenhorn
Posts: 3
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Manish Godbole.

thanks!!
But i need further help.
The if block goes like this::



The Question is whether maybeNew() will return 1 or 0. I thought that it will return 0 only as the value for returning changes from 1 to 0 as soon as the if block finishes.However that was not so.!!!

Please Help
Thanks in advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be surprised if that actually compiled. I would think you'd get an unreachable statement error.
 
Jasper Verelst
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you forgot to put a } between the 2 return statements. A } there will close the if and then the method will return 0 if the condition to execute if is not met.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!