Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the long version of "blocks[i][j].isColorBox() ? pieceColor : backgroundColor"?

I've read this line of code: blocks[i][j].isColorBox() ? pieceColor : backgroundColor and I'm wondering what is its if statement counterpart. Or if it's really an if statement. I'm new in programming and I'm still learning the language. Thank you!

like image 603
Sugar Spice Avatar asked Oct 10 '12 00:10

Sugar Spice


1 Answers

Something along these lines, if you're returning the color value at the end of a method:

if (blocks[i][j].isColorBox()) {
    return pieceColor;
} else {
    return backGroundColor;
}

Or if you're assigning the color value to a variable:

if (blocks[i][j].isColorBox()) {
    someVariable = pieceColor;
} else {
    someVariable = backGroundColor;
}

Either way, the "long" version of a conditional expression (a.k.a. ternary operator of the form ?:) would be to use an if/else and do something with the values. Notice that the fundamental difference between an if/else and a conditional expression is that the former is an statement without a value whereas the later is an expression which evaluates to the value of its operands.

like image 99
Óscar López Avatar answered Oct 14 '22 10:10

Óscar López