I agree with sebsauvage... you need to learn something called "structured programming", which was invented in the 1960s. His links should be helpful.
To get you started, here's how I would write your second example in Python:
# This is the main loop. It runs forever
while True...