mishi ahmad
mishi ahmad

Reputation: 47

break from If without loops in java

I want to exit from a function if length of a string( originalPath.length) is equal to some specific number and this is restriction as well I cant do any other thing any idea?

String original = originalPath.substring(0, originalPath.length() - separatorPos);

this snippet is part of a function and not loop.

Upvotes: 0

Views: 2329

Answers (2)

M_I
M_I

Reputation: 132

You can use return; if your method is a void, otherwise just return a default value like return false to finish your method if the condition fails.

The return statement will finish your method.

Upvotes: 1

Dheeraj Joshi
Dheeraj Joshi

Reputation: 1582

Make a void method and then You can do it using if condition and return statement like

if(condition){

return;
}

return is used to stop further execution and return any value back from where it was called.

There are other options like throwing exceptions for the same and break,continue for loops in java

Upvotes: 1

Related Questions