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

## 5 Replies - 5015 Views - Last Post: 16 March 2011 - 10:54 PM

### #1

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

Posted 15 March 2011 - 07:49 PM

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

### #2

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

Posted 15 March 2011 - 08:00 PM

### #3

## 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

## 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:

the logic here is:

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.

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

## 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

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

Posted 16 March 2011 - 10:54 PM

its -

condition ? true:false

condition ? true:false

Page 1 of 1