mercredi 6 avril 2016

Java: Can the if/else statement be used with a 'return true' and a 'return false'

public boolean ifElseStatement(){

    if(conditions){
       statementBody++;
       return true;
    }
       else
       return false;

If the condition is met, will this method return true? Or might it return false because it the final return statement is outside of the if body?

I am effectively asking is this correct use of the if/else statement when a boolean value needs to be returned? Or perhaps there is a better or universally preferred way to do this?

To clarify, I am asking if the if-else statement can be used like this, with two return statements. I have always been told to use only one return statement.

Aucun commentaire:

Enregistrer un commentaire