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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With