Hello, Voce can use it like this:
for c in range(1,10):
print('a,b,c')
if c % 5: # Condição
break
The if c == 5: must be within the cycle for.
If you want to skip only one cycle you can use the continue in place of break.
Extra :
As I saw in the comments above, you want to use a function when arriving at a certain value.
Attention : it would make no sense to interrupt the cycle for for a crease, 'cause you could use it this way:
for c in range(1,x):
print('a,b,c')
- Now the x will be the stopping condition. "Avoiding the use of the if" .
The question code can look like this:
def funcaoStack(): # Função de exemplo, que será chamada dentro do ciclo do FOR.
print('foo!!!')
def funcaoOverflow(): # Função de exemplo, que será chamada após o ciclo FOR.
print('saa!!!')
for i in range(10):
if i % 5: # Essa fução irá ser chamada quando i for igual a 5.
funcaoStack()
continue # Foi adicionado o "continue" para demonstração.
if i == 7: # Aqui o ciclo FOR será interrompido, ai continua o'que está no script.
break
print(i) # Por que o "print" está depois do "if" ?
# Pois para fazer sentido na resposta, primeiro se verifica as condições no ciclo.
funcaoOverflow()
it’s not better just to do
range(1, 5)
?– Guilherme Nascimento
range(0,10)
to break into5
unconditionally makes no sense– Isac
This looks like a XY problem. It would be better to try to explain what you are really trying to do and prefaced with real code, so that it is clear to everyone.
– Isac
Now the question has changed, which has made the situation worse. You have an answer and accept, so changing the question makes it invalid. , you cannot do this.
– Maniero