Help me


#1

what dose : and ? means in java


#2

Example:

int pointsScored = 21;
char gameResult = (pointsScored > 20) ? 'W' : 'L';  // W stands for Win, L stands for Lose

if pointsScored is bigger than 20 (which is 21), then gameResult becomes W, if not, L.

I hope I explained myself simple enough


#3

It's called the ternary operator and it is liked a condensed if statement.

Consider you have a boolean expr and you want to set a variable to 1 if expr is true and to -1 if it is false. The syntax to do this is:

int ans = expr ? 1 : (-1);

If you already know if-statements you can also write this code as:

int ans = 0;
if(expr) {
    ans = 1;
} else {
    ans = -1;
}

You can for instance use it to quickly get the absolute value of a number:

int num = -34; // randomly chosen
int abs = (num < 0) ? (-num) : num;