jeudi 9 mars 2017

What other alternatives do I have for nested if statements for float variables going to infinity?

I'm trying to construct a code for a float variables that does not rely on nested if statements unlike you can see in the following code-example. as you can see the higher the velocity of the ball, the longer the code would get.

is there a shorter way to write the code or do I have to use the nested if-else statement for the variable of ball_vector_x when going up to infinity or just as example up to 100? counter is an int variable starting at zero

if(ball_vector_x == 4 || ball_vector_x == -4) {
    counter = 1;
  } else if(ball_vector_x == 5 || ball_vector_x == -5) {
    counter = 2;
  } else if(ball_vector_x == 6 || ball_vector_x == -6) {
    counter = 3;
  } else if(ball_vector_x == 7 || ball_vector_x == -7) {
    counter = 4;
  } else if(ball_vector_x == 8 || ball_vector_x == -8) {
    counter = 5;
  } else if(ball_vector_x == 9 || ball_vector_x == -9) {
    counter = 6;
  } else if(ball_vector_x == 10 || ball_vector_x == -10) {
    counter = 7;
  } else if(ball_vector_x == 11 || ball_vector_x == -11) {
    counter = 8;
  } else if(ball_vector_x == 12 || ball_vector_x == -12) {
    counter = 9;
  } else if(ball_vector_x == 13 || ball_vector_x == -13) {
    counter = 10;

Aucun commentaire:

Enregistrer un commentaire