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 ????

Recommended Answers

All 2 Replies

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'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.