Avoiding all these IF statements
in
Programming Questions
•
2 years ago
Hi all,
I'm using this function every time in draw() and it just feels
wrong . I'm sure there's a more efficient/elegant way.
The numbers returned from the array vary depending on various things so that's no so important, what I'd really like is to not use all these IF statements.
Would be very interested to hear your thoughts
Thanks
Dave
int calculateHeights(int i) { if (x < i && x > i-4) { index = 0; } if (x < i-4 && x > i-8) { index = 1; } if (x < i-8 && x > i-12) { index = 2; } if (x < i-12 && x > i-16) { index = 3; } if (x < i-16 && x > i-20) { index = 4; } if (x < i-20 && x > i-24) { index = 5; } if (x < i-24 && x > i-28) { index = 6; } if (x < i-28 && x > i-32) { index = 7; } if (x < i-32 && x > i-36) { index = 8; } if (x < i-36 && x > i-40) { index = 9; } if (x < i-40 && x > i-44) { index = 10; } if (x < i-44 && x > i-48) { index = 11; } if (x < i-48 && x > i-52) { index = 12; } if (x < i-52 && x > i-56) { index = 13; } if (x < i-56 && x > i-60) { index = 14; } if (x < i-60 && x > i-64) { index = 15; } return heights[index]; }
1