Problem understanding a java statement!

M

Moham12345

Please could somebody help me understand what this statement is doing,

k = (k1 > s0) ? k1 : s0;

what is this statement doing?

what will k be equal to if
k1 = 3 and s0 = 2 ?
 
T

Tom Hawtin

Moham12345 said:
k = (k1 > s0) ? k1 : s0;

what is this statement doing?

if (k1 > s0) {
k = k1;
} else {
k = s0;
}

Or, assuming suitable types:

k = Math.max(k1, s0);

Tom Hawtin
 
A

Adam Maass

Moham12345 said:
Please could somebody help me understand what this statement is doing,

k = (k1 > s0) ? k1 : s0;

what is this statement doing?

what will k be equal to if
k1 = 3 and s0 = 2 ?

The trick is in understanding the ternary operator,

? :

This takes three operands,

<op1> ? <op2> : <op3>


op1 must have a boolean type; op2 and op3 must have compatible types.

The value of the expression is op2 if op1 is true, op3 otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top