5 Replies - 3921 Views - Last Post: 16 March 2011 - 10:54 PM Rate Topic: -----

#1 lncg14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 lncg14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 sushantmagu  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1