Go Down

### Topic: If(predetermined situtation); statement? (Read 685 times)previous topic - next topic

#### Hydromea

##### Jul 01, 2012, 04:48 pm
Hi. I have a project in which I am reading data from an accelerometer, where certain ranges of X, Y, & Z values correspond to certain scenarios. I then have to check to see if these scenarios are happening using if... statements. Here's an idea of how the code is now, and how I want it to be... (psuedo code)....

NOW:

if ("value of x" > *insert*     AND     "value of x" < *insert*     AND     "value of y" > *insert*     AND    "value of y" < *insert*)
{
DO A BUNCH OF STUFF  }

WANT IT TO BE:

if (movingForwardRight)
{
DO A BUNCH OF STUFF }

movingForwardRight IS WHEN ("value of x" > *insert*     AND     "value of x" < *insert*     AND     "value of y" > *insert*     AND    "value of y" < *insert*)

I didn't really know the best way to word this question, but what is the best way to do something like this ?

#### PaulS

#1
##### Jul 01, 2012, 04:55 pm
Quote
Here's an idea of how the code is now

I'm pretty sure that that code won't compile. Post your real code.

You could create a macro that took several arguments, called movingForwardRight, but I don't recommend that.

Perhaps better would be to break the code down into a moving forward and a moving backward section. Make each section a function. In each section/function, determine if you are/should be moving left or right, and call the appropriate function.

Do the same, in those functions, for up and down, if needed.

#### a.mlw.walker

#2
##### Jul 01, 2012, 04:56 pm
It looks like you want to set movingForwardRight to true when
Quote

Code: [Select]
`("value of x" > *insert*     AND     "value of x" < *insert*     AND     "value of y" > *insert*     AND    "value of y" < *insert*)`

is true, but what the hell does:
Quote

movingForwardRight IS WHEN

mean?
I think you are wanting from the way you worded that:
Code: [Select]
`boolean movingForwardRight = FALSE;if ((x > xx     &&     x < xz)     &&     (y > yy     &&    y < yz))      {           movingForwardRight = TRUE;       }if (movingForwardRight){//TODO}`
but in that case you could scrap off movingForwardRight and just use the condition.

#### pekkaa

#3
##### Jul 01, 2012, 05:10 pm

I think you are wanting from the way you worded that:
Code: [Select]
`boolean movingForwardRight = FALSE;if ((x > xx     &&     x < xz)     &&     (y > yy     &&    y < yz))      {           movingForwardRight = TRUE;       }`

Or simply:

Code: [Select]
`boolean movingForwardRight  = (x > xx     &&     x < xz)     &&     (y > yy     &&    y < yz);`

#### Hydromea

#4
##### Jul 01, 2012, 07:31 pm
Thanks everyone.. Yeah, the pseudo code I posted was kind of sloppy, and I didn't want to post my real code because the thing is pretty big and had a lot of other stuff. But I saw what you guys said and figured something out. I'll just have the 'if' statement contain all of my needed things, to figure out the situation, and then just do things from there, instead of referencing something else. Got it figured out now.

Go Up

Please enter a valid email to subscribe