C# Greater Than or Equal

Im having a problem with my code. When my number is equal or greater than 1 it works.
But it doesnt change the described things in the code when its equal or greater than 2.

if(number >= 1)
   timer = 10;

else if(number >= 2)
   timer = 5;

When my number is equal to 2 it doesnt change the timer value.
Whats the problem? Am i just missing a simple thing or is it more complicated?

Your code executes in order, and since you have an else if, the second condition won’t happen if the first one is. In English, with your variable replaced with the number “2”, your code does this…

if 2 >= 1
  set the timer to 10
otherwise if 2 >= 2
  set the timer to 5

Do you see the issue? 2 is greater than or equal to 1, so only the “set timer to 10” code is executed. There are two ways around this. The first is to just eliminate the else…

if (number >= 1)
  timer = 10;

if (number >= 2)
  timer = 5;

This way, both if statements always get executed, and the last one that is true will execute the proper code. However, if you have a lot of these it’s less efficient. So another way to do this is to keep the else, but always check against the highest value first…

if (number >= 2)
  timer = 5;
else if (number >= 1)
  timer = 10;

If number is 2, the first one will be executed, but even though number is greater than 1 the second one won’t because of the else.

So, pick one of those ways depending on your needs.