+ 4

How do you return to the start of the loop?

My question might be simple for you, how do you return to the start of the loop (can be for/while) in one of its runs?

29th Aug 2018, 4:11 AM
ice wizard
4 Answers
+ 6
Use "continue" to jump back to beginning of loop body. i = 0 while i < 10: i += 1 if i % 2 == 0: continue # go back to "i += 1"" print("i = {}".format(i)) print() for i in range(1, 11): if i % 2 == 0: continue # go back to "if i % 2 == 0:" print("i = {}".format(i))
29th Aug 2018, 5:01 AM
Ipang
+ 4
with "continue", it's in almost all languages the same keyword!
29th Aug 2018, 4:54 AM
BraveHornet
BraveHornet - avatar
+ 1
i=0 while True: i+=1 if(i == 50): break #stop loop else: continue #continue loop print(i)
29th Aug 2018, 8:09 AM
Sousou
Sousou - avatar
0
with for, I don't think you can. with while, you could reset the statement variable or have it in a function and break an recall the within the function with an if statement. or continue if you just want to start next loop cycle instead of starting the loop iteration / process again
29th Aug 2018, 4:16 AM
Markus Kaleton
Markus Kaleton - avatar