You use
if to talk about something that could
possibly happen.
You use when to talk about something you think is definitely going to happen.
If I go out later, I’ll lock the door. → You don’t know yet if you are going out or not.
When I go out later, I’ll lock the door. → You already know for sure that you are going out.