selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaalien
    New Member
    • May 2007
    • 34

    selection

    I have qeustions regarding java syntax please...
    Is this syntax is considered/read as the standard 'if' selection?
    Code:
    MutableBigInteger t = !uOdd ? v : u;
    How it's been generated in jvm?
    If I want to check either the boolean value is true or false, can it be read as staadard 'if' (e.g: if (a > b) ...)
    Thank you
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by javaalien
    I have qeustions regarding java syntax please...
    Is this syntax is considered/read as the standard 'if' selection?
    Code:
    MutableBigInteger t = !uOdd ? v : u;
    How it's been generated in jvm?
    If I want to check either the boolean value is true or false, can it be read as staadard 'if' (e.g: if (a > b) ...)
    Thank you
    Yep, the ternary operator ?: reads as an if statement. There is a difference though:
    that operator takes just expressions while the if statement takes statements.

    While I'm at it: not few people like non-double negations ;-) so your example
    might be better expressed as:[code=java]
    MutableBigInteg er t = uOdd ? u : v; [/code]

    If you want to see what the virtual machine code looks like, try the "javap" tool
    that comes with the JDK distribution.

    kind regards,

    Jos

    Comment

    • javaalien
      New Member
      • May 2007
      • 34

      #3
      Originally posted by JosAH
      Yep, the ternary operator ?: reads as an if statement. There is a difference though:
      that operator takes just expressions while the if statement takes statements.

      While I'm at it: not few people like non-double negations ;-) so your example
      might be better expressed as:[code=java]
      MutableBigInteg er t = uOdd ? u : v; [/code]

      If you want to see what the virtual machine code looks like, try the "javap" tool
      that comes with the JDK distribution.

      kind regards,

      Jos
      I have one last question about this ternary operator.
      If I wanna check either branch condition is true or false,
      and check which path's taken (from the output) do I have to include this syntax too in my assignment?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by javaalien
        I have one last question about this ternary operator.
        If I wanna check either branch condition is true or false,
        and check which path's taken (from the output) do I have to include this syntax too in my assignment?
        I'm sorry; I don't understand that last question. For the expression:[code=java]
        a= b?c:d?e:f;[/code]
        the possible 'outputs' are c, e or f. If at least two of them are equal you can't
        tell which branch was taken looking at just the output. You could build a little
        tracer function:[code=java]
        boolean trace(boolean cond, String text) {
        System.out.prin tln(text+": "+cond);
        return cond;
        }[/code]
        ... and rewrite the first expression like this:[code=java]
        a= trace(b, "b")?c:trac e(d, "d")?e:f;[/code]
        It will show you on the standard output stream which branches were true during
        the evaluation of the expression.

        kind regards,

        Jos

        Comment

        • javaalien
          New Member
          • May 2007
          • 34

          #5
          Originally posted by JosAH
          I'm sorry; I don't understand that last question. For the expression:[code=java]
          a= b?c:d?e:f;[/code]
          the possible 'outputs' are c, e or f. If at least two of them are equal you can't
          tell which branch was taken looking at just the output. You could build a little
          tracer function:[code=java]
          boolean trace(boolean cond, String text) {
          System.out.prin tln(text+": "+cond);
          return cond;
          }[/code]
          ... and rewrite the first expression like this:[code=java]
          a= trace(b, "b")?c:trac e(d, "d")?e:f;[/code]
          It will show you on the standard output stream which branches were true during
          the evaluation of the expression.

          kind regards,

          Jos
          Let's say I have 2 conditions in my program:
          Code:
          if (a > b) 
           statement 1;
          else
            statement 2;
          The answer would be either True or False and it will determine then which statement (path) to exercise.

          Then the second is:
          Code:
          a= b?c:d?e:f;
          What happen to this ternary operator, does it show you which statement (path) to exercise too?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by javaalien
            Let's say I have 2 conditions in my program:
            Code:
            if (a > b) 
             statement 1;
            else
              statement 2;
            The answer would be either True or False and it will determine then which statement (path) to exercise.

            Then the second is:
            Code:
            a= b?c:d?e:f;
            What happen to this ternary operator, does it show you which statement (path) to exercise too?
            It's like an if-then-else statement but the difference is that it only takes expressions
            as the operands and it's an expression itself; for the example: the value of
            the entire expession depends on the boolean values b and d. If b is true,
            the value will be c; otherwise if d is true the value will be e, otherwise f.
            a simple a?b:c ternary expression can be written in an 'if then else' manner:[code=java]
            a?
            b
            :
            c[/code]
            kind regards,

            Jos

            Comment

            • javaalien
              New Member
              • May 2007
              • 34

              #7
              Originally posted by JosAH
              It's like an if-then-else statement but the difference is that it only takes expressions
              as the operands and it's an expression itself; for the example: the value of
              the entire expession depends on the boolean values b and d. If b is true,
              the value will be c; otherwise if d is true the value will be e, otherwise f.
              a simple a?b:c ternary expression can be written in an 'if then else' manner:[code=java]
              a?
              b
              :
              c[/code]
              kind regards,

              Jos
              Thank you very much. Kindly please check below code again:
              Code:
              if(a >b)
               go path 1
              else
                go path 2
              Code:
              a?
               go path 1
               : 
                go path 2
              Do you agree with me if I say they are the same?

              Comment

              Working...