Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   What does ? : do? (http://www.programmingforums.org/showthread.php?t=14883)

namsu Jan 7th, 2008 10:56 AM

What does ? : do?
 
I am unsure of what the ? : is doing in Java. Ive searched in Google I cannot find anything, I have forgotten which book I read it out of but heres an example of some code that involves it:
:

61    public void setGrossSales( double sales )
62    {
63      grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64    }


Sane Jan 7th, 2008 11:35 AM

Re: What does ? : do?
 
A question mark is known as a "conditional operator". Essentially, it's a one line if statement.

http://www.google.ca/search?hl=en&q=...operator&meta=

It evaluates the statement before the question mark. If it's true, the statement inherits the value of the left side of the colon. If the statement is false, it inherits the value on the right side of the colon.

:

statement that will be true or false ? value on left side of colon if true : value on right side of colon if false

Is equal to:

:

if (statement that will be true or false)
    use value on left side
else
    use value on right side



So the code you posted:

:

public void setGrossSales( double sales )
{
    grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}


Is equivilent to:

:

public void setGrossSales( double sales )
{
    if (sales < 0.0)
        grossSales = 0.0;
    else
        grossSales = sales;
}


It's an easy way to prevent the number from being negative, in one line.

namsu Jan 7th, 2008 11:37 AM

Re: What does ? : do?
 
Quote:

Originally Posted by Sane (Post 139283)
A question mark is known as a "conditional operator". Essentially, it's a one line if statement.

http://www.google.ca/search?hl=en&q=...operator&meta=

It evaluates the statement before the question mark. If it's true, the statement inherits the value of the left side of the colon. If the statement is false, it inherits the value on the right side of the colon.

:

statement that will be true or false ? value on left side of colon if true : value on right side of colon if false

Is equal to:

:

if (statement that will be true or false) is (true)
    use value on left side
else
    use value on right side


Thanks

mrynit Jan 7th, 2008 3:01 PM

Re: What does ? : do?
 
@namsu you don't have to quote his entire post to say thank you.

I hardly ever see the ? operator. Using if or if else looks better as in more human readable than ?. It's style to me.

Sane Jan 7th, 2008 3:30 PM

Re: What does ? : do?
 
Well there are instances where it becomes necessary in order to prevent repeating code, or adding new variables in to the scenario. I'll make something up to demonstrate this...

Quote:

The function "isPrime(x)" returns 1 if x is prime, 0 if it is composite.

Using this function, output whether or not n is a prime number in the following format:

"The number 'n' is a prime number."
/ or /
"The number 'n' is a composite number."

E.G.

"The number '4' is a composite number."
"The number '6079' is a prime number."
So what would be the best way to code it? With if/else statements:

(Switching to C Code now)

:

  1. if (isPrime(n))
  2.     printf("The number '%d' is a prime number.", n);
  3. else
  4.     printf("The number '%d' is a composite number.", n);


With a conditional operator:

:

  1. printf("The number '%d' is a %s number.", n,
  2.       isPrime(n) ? "prime" : "composite");


Each has their drawbacks, but personally I'd go with the latter, for sake of not repeating code.
It's easier to change the format of the output, and the input specifications.

namsu Jan 7th, 2008 3:30 PM

Re: What does ? : do?
 
Quote:

Originally Posted by mrynit (Post 139297)
@namsu you don't have to quote his entire post to say thank you.

I hardly ever see the ? operator. Using if or if else looks better as in more human readable than ?. It's style to me.

Yeah sorry I will just say Thanks and post next time.


All times are GMT -5. The time now is 3:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC