# Thread: A clever way of doing this ... (avoiding a LOT of if-else statements)

1. Member
Join Date
Jun 2010
Posts
4
Rep Power
0

## A clever way of doing this ... (avoiding a LOT of if-else statements)

Hi, currently my code looks like this:

Java Code:
```// Adjust x aim point
if(xWind < 0.50) {
offsetX = 1;
} else if(xWind < 0.70) {
offsetX = 1;
} else if(xWind < 0.80) {
offsetX = 2;
} else if(xWind < 1.00) {
offsetX = 5;
} else {
offsetX = 7;
}
if(yWind < 0.50) {
offsetY = 14;
} else if(yWind < 0.70) {
offsetY = 16;
} else if(yWind < 0.80) {
offsetY = 17;
} else if(yWind < 1.00) {
offsetY = 18;
} else {
offsetY = 19;
}```
However, offsetX and offsetY should both depend on both yWind and xWind. So I need to combine the two, so that I can adjust both the X and Y offset for every combination of xWind and yWind, as such:
Java Code:
```if(xWind < 0.50) {
if(yWind < 0.50) {
offsetX = 1;
offsetY = 14;
} else if(yWind < 0.70) {
offsetX = 1;
offsetY = 16;
} else if(yWind < 0.80) {
offsetX = 1;
offsetY = 17;
} else if(yWind < 1.00) {
offsetX = 2;
offsetY = 18;
} else {
offsetX = 2;
offsetY = 19;
}
} else if(xWind < 0.70) {
if(yWind < 0.50) {
offsetX = 1;
offsetY = 14;
} else if(yWind < 0.70) {
offsetX = 2;
offsetY = 17;
}
// and so on...```
But that results in 100s of if-else-statements. Is there a better way to do this? I need to be able to manually adjust the offsets for any range of xWind and yWind easily in my code.

2. The best way is to use a mathematical formula for calcuating x and y offsets based on the input. How that's done, I have no idea based on what you've told us. Perhaps you can tell us how these numbers are calculated?

3. Member
Join Date
Jun 2010
Posts
4
Rep Power
0
Ok thanks. I think the formula is hard to figure out manually, so I guess I'll have to collect as much data as possible so I can use a curve fitting tool to find polynomials that match the data (non-linear regression I guess).

4. You can do without that long sequence of nested if-else statements but you have to supply the actual data in a couple of matrixes (or, as Fubarable suggested: find a functional depenceny fx(x, y) and fy(x, y) where both fx and fy are the wanted offsets).

Else a few data matrixes and a bit of code are a solution:

Java Code:
```double winds[][] = // you have to supply the data here.
double[] windx[] = // you have to supply the data here.
int offsetsX[][] = // again, you supply the data here.
int offsetsY[][] = // again, you supply the data here.

for (int x= 0; x < windx.length; x++)
if (windx[x] >= actualX)
for (int y= 0; y < winds[x].length; y++)
if (winds[x][y] >= actualY)
// found a value for actualX and actualY: offsetsX[x][y] and offsetsY[x][y] are the answer ...```
kind regards,

Jos
Last edited by JosAH; 07-01-2010 at 08:42 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•