Problem understanding a java statement!

Discussion in 'Java' started by Moham12345, Mar 6, 2007.

  1. Moham12345

    Moham12345 Guest

    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 ?
    Moham12345, Mar 6, 2007
    #1
    1. Advertising

  2. Moham12345

    Tom Hawtin Guest

    Moham12345 wrote:
    >
    > 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
    Tom Hawtin, Mar 6, 2007
    #2
    1. Advertising

  3. Moham12345

    Adam Maass Guest

    "Moham12345" <> wrote:
    > 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.
    Adam Maass, Mar 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    783
  2. Jay McGavren
    Replies:
    11
    Views:
    1,097
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,049
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    442
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,018
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page