This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Using return statement in a for-loop within a method

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the following:


give me an error saying "This method must return a result of type int"?
I am returning. And it's not like I'm returning in an if-statement where it's possible that the condition might never be fulfilled, this is a loop which means that sooner or later the return-statement will be carried out. Ideas?
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are trying to return from an inner scope. The compiler checks the outer scope of the method and says, there is no return type, because the inner scope does not exist until runtime.

Omar Sharaki wrote:Why does the following:


give me an error saying "This method must return a result of type int"?
I am returning. And it's not like I'm returning in an if-statement where it's possible that the condition might never be fulfilled, this is a loop which means that sooner or later the return-statement will be carried out. Ideas?

 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is not smart enough to realize that you will always enter the loop. It thinks "ok...but what if somehow the loop never executes...what will the method return?", and then tells you that there is no return statement for that execution path.

The compiler is dumb. Remember that.
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:. . . The compiler is dumb. Remember that.

It is very difficult to program a “smart” compiler.
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what Fred and Campbell said, while it's not *that* difficult for a compiler to check that i < 100, it is in general impossible to reason about the outcome of all decision problems. This is called the Entscheidungsproblem.

The Java designers could have made a special case for the standard for-loop idiom, but they probably felt it wasn't worth the trouble.
 
Campbell Ritchie
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . The Java designers could have made a special case for the standard for-loop idiom . . .

That would work when the continuation predicate can be determined in advance, but in many cases you cannot be 100% certain that the loop will actually start.
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was referring to the special case where the counter is initialized with a compile-time constant, the predicate uses a relational operator to compare the counter to a compile-time constant, and the counter is incremented by a compile-time constant. You can then determine at compile-time if and how many times the loop will run.

Like I said though, this is a lot of effort for a little gain.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I was referring to the special case where the counter is initialized with a compile-time constant, the predicate uses a relational operator to compare the counter to a compile-time constant, and the counter is incremented by a compile-time constant. You can then determine at compile-time if and how many times the loop will run.

Like I said though, this is a lot of effort for a little gain.



Minor note: The compiler wouldn't have to look at the increment clause to determine if the body of the loop will be executed at least once - just the initialization and the continuation condition.

But I agree... more trouble than it's worth. Just put a return 1 after the for() loop.
 
It's never done THAT before. Explain it to me tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic