Lesson 9/11 ternary conditional statement

ok so , my question is why does it return the value of W? pointScored takes the value of 21 ,and gameresult=21>20 which is true . but why does it return W and not L? also what does the : mean? me not understanding the : symbol is the reason i dont get the whole thing?
thanks for reading!
<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
no error messages.


Replace this line with your code.

<do not remove the three backticks above>

I suppose it is similar to some other programming language when we use if/else statement. Unless the format is a bit unique.
From this line:

char gameResult = (pointsScored > 20) ? 'W' : 'L';

we can assume that this part

(pointsScored > 20) ?

is like questioning, “is variable pointsScored more than 20?”. And then this part

'W' : 'L';

is like giving choice, “if the statement is true, then return ‘W’. Otherwise return ‘L’.” So I think the “:” mark is for saying “otherwise”.


@cakthe yep that’s exactly it! Before the : is what to return if true and after the : if what to return if false i.e. the ‘else’ .

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