# Using Conditional Operator- I am missing a part to the answer.

Page 1 of 1

## 5 Replies - 5308 Views - Last Post: 16 March 2011 - 10:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=222371&amp;s=840a201c7adc84a72d72984420ea3055&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lncg14

Reputation: 0
• Posts: 2
• Joined: 14-March 11

# Using Conditional Operator- I am missing a part to the answer.

Posted 15 March 2011 - 07:49 PM

Write an expression using the conditional operator ( ?: ) that compares the value of the variable x to 5 and results in:
x if x is greater than or equal to 5
-x if x is less than 5

This is what i have so far but I'm missing something
x>=5
? x:- x
Is This A Good Question/Topic? 0

## Replies To: Using Conditional Operator- I am missing a part to the answer.

### #2 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Using Conditional Operator- I am missing a part to the answer.

Posted 15 March 2011 - 08:00 PM

You may want to look at this link or this.

Jim

### #3 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: Using Conditional Operator- I am missing a part to the answer.

Posted 15 March 2011 - 08:01 PM

What result are you getting vs. what you expect to get?

### #4 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Using Conditional Operator- I am missing a part to the answer.

Posted 15 March 2011 - 08:44 PM

the conditional operator makes conditional expressions. An expression is a calculation that has a value like: "1 + 2", "sin(Pi)", "3 == 2" -- these are all expressions and they all have values(3, 1, and false).

Expressions are often made up of "sub-expressions" which are nothing more that intermediate calculations.

5 * 2 + 3 has two 5 sub-expressions: {5, 2, 3, 5*2, 10+3 }

SO when we talk about the conditional operator there are three important sub-expression

boolean-expression ? true-branch : false-branch

the boolean-expression is evaluated to either true or false (for numeric types zero is false, any non-zero value is true). --- if the boolean-expression is true, then the value of the conditional-expression (the whole ?: expression) is taken to be the value of the "true-branch" -- else if the boolean-expression is false then the value of the conditional expression becomes the false-branch.

For example:

```int a = 10;
int b = 20;
int c = (a == b ) ? 100 : 50;
```

the logic here is:

```int c;
if (a == b ) { c = 100; }
else { c = 50; }
```

However because the conditional is an expression (rather than statements like the if-else blocks) it can go places the statements can't and can often simplify code. Conversely, it can also make code very hard to parse if it is used too much.

so given your conditions the expression:

(x >= 5) ? x : -x

looks correct -- so now you have to use it somewhere:

y = (x >= 5) ? x : -x;

for example.

### #5 lncg14

Reputation: 0
• Posts: 2
• Joined: 14-March 11

## Re: Using Conditional Operator- I am missing a part to the answer.

Posted 16 March 2011 - 10:51 PM

Thank you so much NickDMax! That helped a lot!!

This post has been edited by lncg14: 16 March 2011 - 10:54 PM

### #6 sushantmagu

Reputation: -1
• Posts: 7
• Joined: 16-March 11

## Re: Using Conditional Operator- I am missing a part to the answer.

Posted 16 March 2011 - 10:54 PM

its -

condition ? true:false