Lesson 9/11 ternary conditional statement


#1

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!

https://www.codecademy.com/en/courses/learn-java/lessons/conditionals-control-flow/exercises/ternary-conditional?action=lesson_resume


no error messages.


Replace this line with your code.


#2

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".


#3

@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' .


#4

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