Variables inside if statements?

What is the reason that some variables are created within an if statement and some are outside the loop? What’s the difference?

Are you able to provide an example, or two?