6 Replies - 500 Views - Last Post: 05 June 2009 - 07:48 AM Rate Topic: -----

#1 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Question Intent

Posted 04 June 2009 - 08:59 PM

MC question from C++ textbook :

An assignment of the value of a conditional expression to a variable (x = y ? z : w;) can always be
replaced by:
A. a switch statement with assignment statements for its case statements.
B. one or more ifs with else clauses and assignment statements for its true and false clauses.
C. one or more nested while loops with assignments for the bodies of the loops.
D. All of the above


Answer is D...I understand why...

Any yes I checked they say = rather than ==- which I also know would compile...

But shouldn't this expression

x = y ? z : w; be this x = y ? z : w;


What is the intent here- would the answer above change by changing = to ==
That's what I am not sure of..any insight

This post has been edited by jcmaster2: 04 June 2009 - 08:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question Intent

#2 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Question Intent

Posted 04 June 2009 - 09:13 PM

x = y ? z : w;

Is the same as

x = (y ? z : w);



In other words, if y evaluates to true, x will be assigned to z. Otherwise x will be assigned to w.

x == y ? z : w;

Is the same as

(x == y ? z : w);



In other words, if x == y evaluates to true, the value of the whole expression will be z. Otherwise it will be w. However, the value of expression is not getting assigned anywhere. So nothing will happen in the second example.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Question Intent

Posted 04 June 2009 - 09:13 PM

> EDIT: This is wrong. See below. <

Are you asking why it isn't: x == y ? z : w;? Consider what that statement is saying. Evaluate y. If true, you get z, if false, you get w. Now, you end up with x == z or x == w. == is the equality comparison. Even if you wanted to compare x to z or x to w, it's meaningless, because having x == z; is like having x < z. So what? You don't make use of the result. You don't assign it. Floating by itself, it's meaningless.

This post has been edited by Oler1s: 04 June 2009 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Question Intent

Posted 04 June 2009 - 09:18 PM

View PostOler1s, on 4 Jun, 2009 - 08:13 PM, said:

Are you asking why it isn't: x == y ? z : w;? Consider what that statement is saying. Evaluate y. If true, you get z, if false, you get w. Now, you end up with x == z or x == w. == is the equality comparison. Even if you wanted to compare x to z or x to w, it's meaningless, because having x == z; is like having x < z. So what? You don't make use of the result. You don't assign it. Floating by itself, it's meaningless.


This is partially wrong. As I previously noted in x == y ? z : w; the boolean expression is not y, it is x == y. Though you are correct in the sense that the value of the expression is not getting assigned anywhere.

This post has been edited by Dantheman: 04 June 2009 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Question Intent

Posted 04 June 2009 - 09:21 PM

Ah, very true. My mistake. == has higher operator precedence than ?:

This post has been edited by Oler1s: 04 June 2009 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2242
  • View blog
  • Posts: 9,228
  • Joined: 18-February 07

Re: Question Intent

Posted 04 June 2009 - 09:41 PM

x = y ? z : w;

without putting too much thought into this I see:

switch (y) { case 0: x = w; break; default: x = z; }

if (y) { x = z; } else { x = w; }

x = w; while(y) { x = z; break; }

int ch[] = {w, z}; x = ch[y];



x == y ? z : w; -- If x == y then expression z is evaluated else expression w is evaluated -- the result is thrown away.
Was This Post Helpful? 0
  • +
  • -

#7 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Question Intent

Posted 05 June 2009 - 07:48 AM

Nick,


I got something similar to yours for the others
Here is the loop solution they provide...

 #include<iostream>
using namespace std;
int main()
{
  int x , z = 10, w =-10; 
  bool y = true;
  while( y || (x = w))
  {
	while (y)
	{
	   x = z;
	   break;
	}
	break; 
  }
  cout << x << endl;

  y = false;
  while( y || (x = w))
  {
	while (y)
	{
	   x = z;
	   break;
	}
	break; 
  }
  cout << x << endl;
  return 0; 
} 

/* Output is:
10
-10
*/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1