is it correct to ask
What are you going to do if you lose your pants on the train?
Or maybe better to ask
What are you going to do when you lose your pants on the train?
I agree with @JasonBassford - both sentences are grammatical. They have different meanings, though - the first (with if) leaves open the possibility that the listener might not lose their pants. The second (with when) assumes that the listener will definitely lose their pants and it’s just unclear at what time that will happen.