473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

selection

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
May 24 '07 #1
6 1197
JosAH
11,448 Expert 8TB
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
May 24 '07 #2
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?
May 26 '07 #3
JosAH
11,448 Expert 8TB
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
May 26 '07 #4
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?
May 26 '07 #5
JosAH
11,448 Expert 8TB
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
May 26 '07 #6
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?
May 26 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Martin Karlsson | last post by:
Hi guys, Does anyone know how I can restore a selection created with createRange()? The problem is that I have a function that opens a new window with window.open() which of course makes the...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
1
by: bob | last post by:
I asked a question yesterday about how I would parse a Word document using functions in some Interop.Word COM. Some told me to run a macro in Word to see how it works. Most of the code was......
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
1
by: 4Ankit | last post by:
Hey guys i am having some trouble with nesting one selection structure within another selection structure. At the moment i am unclear what selection structures are and just need a simple example of...
5
by: Jure Bogataj | last post by:
Hi all! I have a problem (performance issue) with listview. I have implemented an ItemSelectionChange on my listview and put some code in it (I build some toolbar based on selection and update...
7
by: robtyketto | last post by:
Greetings, I'm slowly building up code to do the following:- Display TWO selection option boxes (cascading). If the FIRST selection option box changes then reload the jsp using onchange...
2
by: vanditnagar | last post by:
Hi, I am facing the difficulty in the selecting a text on the browser . And highlighting the text. I am using the window.getSelection() to get the object and after...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.