Loop Crisis and Misunderstanding

Loop Crisis and Misunderstanding
0

#1

I’m writing a program in python which runs something along this format:-

found=False
for ...:
    if ...:
        found = True
        break
if not found:

Assuming that this is a list or a tuple or something which I want to iterate though, and now I want to restart the previous for loop from the point onwards from which the if statement was executed. How can I do this? Can anyone tell me?


#2

This doesn’t quite make sense. Given your pseudo code, the loop is going to exit when the if clause is true. If you don’t want it to exit, remove the break keyword.


#3

Yeah, sounds a bit odd. The easiest way would be to iterate over enumerated keys, so the attained index could remain stored and it would then be easy to start where you left off. Sounds ripe for refactoring though.


#4

Could you elaborate on your answer?
I want something in this format:-

found=False
for ...:
    if ...:
        found = True
        break
for.....:

Restart previous loop from last point.


#5
let i
for (i=0;i<=z,i++)
    if ...:
        found = True
        break
for. (i;i<=w,i++)....:

i will hold it’s value after the loop break simply restart the loop with i, however , I don’t get the logic, why break out of the loop in the first place if you are going to restart it right away, if you don’t want certain conditions to be met within a loop you can explicitly use continue but even that is not really necessary.


#6

It would help if you explained what problem you are trying to solve and why you’re determined to solve it that way. Is this for a school/uni assignment?


#8

Can you explain how I can use the continue statement in this instance?


#9

break completely exits the most inner loop that it is called in, whereas continue, goes to the next iterator of the loop that it is called in, i.e. if i=4 when continue was called, it disregards all the code below the continue statement and starts from the top of the loop with i=5


#10

I solved my loop problem. Thank you everyone for their helpful suggestions, especially @Dereje1 thank you very much :slight_smile: