Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Missing Return Statement Problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am playing around with search methods for a binary tree (not a binary search tree). I have written the following method and the compiler keeps complaining about a "missing return statement." It seems to me that if none of the other conditions are satisfied the if-else structure should always hit the "return null;" statement at the end of the logic thanks to the last "else" statement being without a logical condition. If I strip out the two "else if" blocks it compiles just fine. Am I reading this wrong? Any help is much appreciated. (FYI item, lChild, and rChild are properties of the class that contains this method). Thanks.

BTreeNode find(Object item) {
if (this.item.equals(item)) {
return this;
} else if (lChild!=null) {
lChild.find(item);
} else if (rChild!=null) {
rChild.find(item);
} else {
return null;
}
}
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must return from each else if. If one of those is satisfied they must return value, because that is where execution should stop.

Edit: And please put the code inside code tags.
 
Richard Engberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting and that makes sense. Thanks!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seems to me that if none of the other conditions are satisfied the if-else structure should always hit the "return null;" statement at the end of the logic thanks to the last "else" statement being without a logical condition.


This is true, but if any of those conditions is true, the execution never enters else block so you must return value from them.

And welcome to the Ranch!
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard,

Please use code tags when you post your java. Once you past it in, highlight it all, then click the 'code' button above. It will preserver your formatting, making it much easier to read. It should look like this:

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for adding the code tags, Fred. Unfortunately, they simply highlight the fact that some of the if-elses do not lead to an execution path which returns anything. The compiler can run through the method and see that there is the possibility of executing it without returning a BTreeNode, which it doesn’t like.
Many of us believe that multiple returns are confusing, so I would write it like this:-Note the assignment to null is at the beginning of that method, so your last else vanishes.
A lot of people, myself included, prefer { and } on lines to themselves, and would indent it like this:-It may take more space on screen, but it might be easier to understand the relationships of the blocks to one another.

Is that method looking for something in a binary tree?
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic