Conditional question cumulative project three

just general question. when I was doing the 3rd project we were asked to write code that would return true if correctAnswer was the playerAnswer. I thought the only way to do this was with a if/then statement. Obviously this is not true. Just was hoping on some explanation on the logic on how the top code works vs what I wrote. Thanks for the help as always.

boolean isCorrect() {

      return correctAnswer == playerAnswer;

    }

vs

public boolean isCorrect(){


     if(correctAnswer == playerAnswer){

       return true;

     }else{

       return false;

     }

     }
return correctAnswer == playerAnswer;

The return statement takes an expression, literal or otherwise, which above is the evaluation of the identity comparison (a boolean). No conditional required since the output matches the declared data type.

Are correctAnswer and playerAnswer both global variables?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.