Connecting Tech Pros Worldwide Forums | Help | Site Map

selection

Member
 
Join Date: May 2007
Posts: 34
#1: May 24 '07
I have qeustions regarding java syntax please...
Is this syntax is considered/read as the standard 'if' selection?
Expand|Select|Wrap|Line Numbers
  1. 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's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 24 '07

re: selection


Quote:

Originally Posted by javaalien

I have qeustions regarding java syntax please...
Is this syntax is considered/read as the standard 'if' selection?

Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. MutableBigInteger t = uOdd ? u : v;  
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
Member
 
Join Date: May 2007
Posts: 34
#3: May 26 '07

re: selection


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. MutableBigInteger t = uOdd ? u : v;  
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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: May 26 '07

re: selection


Quote:

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:
Expand|Select|Wrap|Line Numbers
  1. a= b?c:d?e:f;
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:
Expand|Select|Wrap|Line Numbers
  1. boolean trace(boolean cond, String text) {
  2.    System.out.println(text+": "+cond);
  3.    return cond;
  4. }
... and rewrite the first expression like this:
Expand|Select|Wrap|Line Numbers
  1. a= trace(b, "b")?c:trace(d, "d")?e:f;
It will show you on the standard output stream which branches were true during
the evaluation of the expression.

kind regards,

Jos
Member
 
Join Date: May 2007
Posts: 34
#5: May 26 '07

re: selection


Quote:

Originally Posted by JosAH

I'm sorry; I don't understand that last question. For the expression:

Expand|Select|Wrap|Line Numbers
  1. a= b?c:d?e:f;
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:
Expand|Select|Wrap|Line Numbers
  1. boolean trace(boolean cond, String text) {
  2.    System.out.println(text+": "+cond);
  3.    return cond;
  4. }
... and rewrite the first expression like this:
Expand|Select|Wrap|Line Numbers
  1. a= trace(b, "b")?c:trace(d, "d")?e:f;
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:
Expand|Select|Wrap|Line Numbers
  1. if (a > b) 
  2.  statement 1;
  3. else
  4.   statement 2; 
The answer would be either True or False and it will determine then which statement (path) to exercise.

Then the second is:
Expand|Select|Wrap|Line Numbers
  1. a= b?c:d?e:f;
What happen to this ternary operator, does it show you which statement (path) to exercise too?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: May 26 '07

re: selection


Quote:

Originally Posted by javaalien

Let's say I have 2 conditions in my program:

Expand|Select|Wrap|Line Numbers
  1. if (a > b) 
  2.  statement 1;
  3. else
  4.   statement 2; 
The answer would be either True or False and it will determine then which statement (path) to exercise.

Then the second is:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. a?
  2.    b
  3.  :
  4.    c
kind regards,

Jos
Member
 
Join Date: May 2007
Posts: 34
#7: May 26 '07

re: selection


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. a?
  2.    b
  3.  :
  4.    c
kind regards,

Jos

Thank you very much. Kindly please check below code again:
Expand|Select|Wrap|Line Numbers
  1. if(a >b)
  2.  go path 1
  3. else
  4.   go path 2
Expand|Select|Wrap|Line Numbers
  1. a?
  2.  go path 1
  3.  : 
  4.   go path 2
Do you agree with me if I say they are the same?
Reply