public static boolean isEmpty(int grid[][]) {
        for (int r = 0; r < grid.length; r++)
            for (int c = 0; c < grid.length; c++)
                if (grid[r][c]==0) {

                    return true;
        return false;

the question is :
after returning true it back to (for loop) or not ????

3 Years
Discussion Span
Last Post by stultuske

Your question seems to be missing some words. Did you mean "After returning true, does the for loop continue?" If so, the answer is no. When the return statement is reached, the control flow returns to the call site.


any return statement will end the method.
consider this method as "my code runs, until the requirement is met and the method is flagged as 'ended'.

this is exactly what the return keyword does (even in a method with no returntype - void )

here, your signature reads:
public static boolean isEmpty(int grid[][])

There are a few ways this method can end:
1. an Exception is thrown
2. the JVM is terminated (System.exit)
3. a return statement is reached.

Since this method has boolean as a returntype, you'll need to return a boolean.
You can read the method signature as:

"I pass the grid variable to method isEmpty, which will run until an Exception is thrown, the JVM is terminated, or a boolean is returned".

when either return true; or return false; is reached, the requirement is fulfilled, and the method has 'done it's job'.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.