You use the conjunction "if" when you think of condition.

You use the conjunction "when" when you think of time.
 
Of course, there is a gray zone where both conjunctions are possible.

- Taste the soup and add salt and pepper if necessary (condition).

- Watch the pot and tell me when the water boils (time).