Restarting a Loop from the Top

python: restarting a loop

You may want to consider using a different type of loop where that logic is applicable, because it is the most obvious answer.

perhaps a:

i=2
while i < n:
if something:
do something
i += 1
else:
do something else
i = 2 #restart the loop

how to restart for loop in python ?

What about this:

x = [1,2,3,4,5,6]
restart = True
while restart:
for i in x:
# add any exit condition!
# if foo == bar:
# restart = False
# break
if i == 4:
break
else:
print i

Python - Way to restart a for loop, similar to continue for while loops?

I'm not sure what you mean by "restarting". Do you want to start iterating over from the beginning, or simply skip the current iteration?

If it's the latter, then for loops support continue just like while loops do:

for i in xrange(10):
if i == 5:
continue
print i

The above will print the numbers from 0 to 9, except for 5.

If you're talking about starting over from the beginning of the for loop, there's no way to do that except "manually", for example by wrapping it in a while loop:

should_restart = True
while should_restart:
should_restart = False
for i in xrange(10):
print i
if i == 5:
should_restart = True
break

The above will print the numbers from 0 to 5, then start over from 0 again, and so on indefinitely (not really a great example, I know).

Is it possible to restart a for loop to its first iteration?

Not with fast enumeration, no (with the exception of goto), however if you use the indexed-access approach, you could:

NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; i++)
{
bool someflag = false;
id object = array[i];
if (isgood(object))
{
someflag = true;
//restart to first object in loop
i = 0;
}
if(someflag)
{
doStuffWithObject(object);
}
}

Restarting the while loop

Use the keyword continue to go to the next iteration of the loop. For example:

while(true)
{
// ...

if(!condition) continue; // this will go to the beginning of the while loop.

// ...
}

Can you restart the current iteration of a Python loop?

You could put your try/except block in another loop and then break when it succeeds:

for row in rows:
while True:
try:
something
break
except Exception: # Try to catch something more specific
pass

How can I restart while loop after certain condition (if) satisfied in c++?

Your question isn't very detailed so it's a little hard to tell what exactly do you want.

If you want the while loop to go to the next iteration after any error has fired, you should use the continue statement:

while( something )
{
if( condition )
{
//do stuff
continue;
}
else if( condition 2 )
{
//do other stuff
continue;
}
<...>
}

If nothing else than these ifs is inside the loop, and the conditions are integer values, you should consider using switch instead:

while( condition )
{
switch( errorCode )
{
case 1:
//do stuff;
break;
case 2:
//do other stuff;
break;
<...>
}
}

If you want to completely restart the cycle... well this is a little harder. Since you have a while loop, you can just set the condition to it's starting value. For example, if you have a loop like this:

int i = 0;
while( i < something )
{
//do your stuff
i++;
}

then you can "reset" it like this:

int i = 0;
while( i < something )
{
//do your stuff
if( something that tells you to restart the loop )
{
i = 0;//setting the conditional variable to the starting value
continue;//and going to the next iteration to "restart" the loop
}
}

However, you should be really careful with this, it's easy to get an infinite loop accidentally.

C# restart a while loop in the middle

You need to use the word continue

    if (IpValidation(ip) == false)
{
Console.WriteLine("Ugyldig IP og eller Subnet mask!\n");
continue;
}

This will skip the rest and go to the top of your loop.

Restarting the loop using continue keyword

You never ask for y again. It is always using the first input. Try asking again inside the loop like this:

from random import randint

print("Lets start the game!! ")

x = randint(1, 10)

print(x)

y = input("Enter your guess: ")

z = True

while z:
y = input("Enter your guess: ")
if x != int(y):
continue
else:
print("Correct Guess")
break


Related Topics



Leave a reply



Submit