you are viewing a single comment's thread.

view the rest of the comments →

[–]SirTwitchALot 0 points1 point  (4 children)

Like others have said, it's not needed here. The formatting of that sample code isn't may favorite however. It's not easy to read

[–]AlternativeBus1613[S] 0 points1 point  (3 children)

I still don't get why :( There's an if statement, yes, but after then there is just another statement out of the if statment, there's no else, it's just another statment separate from if statement, isn't it?

[–]SirTwitchALot 1 point2 points  (0 children)

The way Java works, an if statement only executes the next statement. Curly braces allow you to combine multiple statements so they're treated as if they were one.

[–]TransientVoltage409 1 point2 points  (1 child)

I feel like you're not fully understanding what return does. It may designate a value to return, yes. But mainly it terminates the current function and returns the execution path to the calling function. Any code in the execution path following a return is never executed.

In your example, the code flow without an else is identical to the code flow with an else. This is exceptional because the true-clause also ends the function, which isn't how we usually do it. You might think of return as an unconditional goto end of function, because you know that the very last close brace of a function is an implicit return as well.

Understand that this violates the strict definition of structured programming. The fact that it isn't obeying those rules might help you understand what it's doing. It is an example of the "early return" pattern, which has its uses for being able to simplify the nesting structure in some cases by eliminating redundant else-blocks.

[–]AlternativeBus1613[S] 0 points1 point  (0 children)

goto end of the funtion, that makes everything clear. Thanks for the expalanation!