This is not necessarily about performance, but I usually go from specific to general to prevent such cases:
int i = 15; if(i % 3 == 0) System.out.println("fizz"); else if(i % 5 == 0) System.out.println("buzz"); else if(i % 3 == 0 && i % 5 == 0) System.out.println("fizzbuzz");
Here the code above will never say "fizzbuzz" because 15 matches the conditions i % 3 == 0 and i % 5 == 0 . If you reorder something more specific:
int i = 15; if(i % 3 == 0 && i % 5 == 0) System.out.println("fizzbuzz"); else if(i % 3 == 0) System.out.println("fizz"); else if(i % 5 == 0) System.out.println("buzz");
Now the above code will reach fizzbuzz before more general conditions stop it
Hunter mcmillen
source share