contestada

Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "non-negative" otherwise. Ex: If userVal is -9, output is: -9 is negative.

Respuesta :

Answer:

(userVal < 0) ? "negative" : "non-negative" ;

Explanation:

The above code has been written using Java's ternary operator (? : ).

Java uses this operator to write conditional expressions that are similar to the regular if ... else statements.

This expression has three parts,

i. the conditional statement: this is the expression before the ? mark. In this case, (userVal < 0). This expression contains the condition to be tested for and it returns true or false.

ii. the second part is the statement after the ? mark. In this case, "negative". This expression will be executed if the conditional statement returns true.

iii. the third part is the statement after the : mark. In this case, "non-negative". This expression will be executed if the conditional statement returns false.

So in this case, if userVal < 0, the string "negative" will be evaluated. Otherwise, "non-negative".

ACCESS MORE