+ 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?
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))
+ 4
with "continue", it's in almost all languages the same keyword!
+ 1
i=0
while True:
i+=1
if(i == 50):
break #stop loop
else:
continue #continue loop
print(i)
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