# Help with problem in c++ class



## Asylum (Oct 10, 2012)

Our teacher gave us some problems to work for class but I dont see anywhere in the book that explains these.
Im sure it is something simple that im overlooking because im good in math.
Could someone show me how to work these.
Thanks in advance.


Following the pseudocode below, what is the value of X if Y = 10?

X=0
If Y > 10 then
    X=5
Else
If Y < 20 then 
    X=7
   Else
    X=9


----------



## Asylum (Oct 10, 2012)

Ok I think I see it already.

X= 7 correct?


----------



## librin.so.1 (Oct 10, 2012)

Yes, You are correct. Also, the last branch will never trigger no matter what is the value of Y.

And to finding an answer: if You can't figure out the end result by reading a description/pseudocode/whatever, but can see/understand the algorithm, just write a small test program and simply get the result


----------



## MrSeanKon (Oct 15, 2012)

X=5 for all Y values greater than 10.
For values less than 10 e.g. Y=8, Y=9, Y=9.999999999 (not exactly Y=10) X is always 9.
10 value for Y is a "critical" value where the second if/else conditional works.


----------



## GSquadron (Oct 15, 2012)

I don't even get it why you asked?


----------



## 3870x2 (Oct 15, 2012)

Aleksander Dishnica said:


> I don't even get it why you asked?



He didn't understand it at first.  I thought it was pretty easy.  I am sure there was something very specific he wasn't understanding.


----------



## W1zzard (Oct 15, 2012)

much more readable once you indent it


```
X=0
If Y > 10 then
  X=5
Else
  If Y < 20 then 
    X=7
  Else
    X=9
```

when using a language that uses closing blocks for _if _it will be even more readable

```
X=0
If Y > 10 then
  X=5
Else
  If Y < 20 then 
    X=7
  Else
    X=9 
  Endif
Endif
```


----------



## Drone (Oct 16, 2012)

Asylum said:


> X=0
> If Y > 10 then
> X=5
> Else
> ...



It makes no sense at all, especially the first else. 

Else means that Y<10 and "if Y<20" is redundant. Because if Y<10 then of course Y<20, that part is captain obvious.


----------



## librin.so.1 (Oct 16, 2012)

@Drone

Uhm, it's not Y<10 and Y<20
it's Y>10 and Y<20 
	

	
	
		
		

		
			





Yet, the _last_ else is redundant, 'cause it will never trigger (as I said in my 1st reply  )


----------



## Drone (Oct 16, 2012)

Vinska said:
			
		

> Uhm, it's not Y<10 and Y<20
> it's Y>10 and Y<20 http://imageshack.us/a/img690/5658/orange1.gif



No, it's y<10. They wrote:



> If Y > 10 then
> X=5
> Else



It means that whatever comes after the "else" operator can happen only when y<10


----------



## librin.so.1 (Oct 16, 2012)

@Drone

> No, it's y*<*10. They wrote:


> If Y *>* 10 then
> X=5



Uhm, squeeze me, but aren't You contradicting Yourself?


----------



## Drone (Oct 16, 2012)

No need to play word games here. Else means that y is less or equal 10. Period.


----------



## librin.so.1 (Oct 16, 2012)

Okay, I won't be playing word games.

Graphical representation:





Edit: OK, _now_ I see Your point. That thing is quite obvious when looking at that "code".
Yet, you wrote it very ambiguously. Ambiguously enough to confuse me into thinking You were trying to say a completely different thing. If You would be less ambiguous next time, I would be grateful. Thank You!


----------



## 3870x2 (Oct 16, 2012)

Asylum said:


> Our teacher gave us some problems to work for class but I dont see anywhere in the book that explains these.
> Im sure it is something simple that im overlooking because im good in math.
> Could someone show me how to work these.
> Thanks in advance.
> ...


Would look a lot easier if:

X = 0;
if (Y > 10) then x = 5;
elseif ( Y < 20 ) then X = 7;
else x = 9;
end if

Sometimes pseudocode is just more confusing.


----------

