Skip to content
Logo do Site
hackerbr.net

  • Início
    • Sobre (About)
  • Política de Privacidade (Privacy Policy)
  • Contato (Contact)
  • Linguagens (languages)
    • HTML
    • Python
  • Banco de Dados (Database)
  • Cybersecurity
  • Computação Forense (Computer Forensics)
  • Anki Web
  • Baralhos Anki
  • Computadores (Computers)
    • Sistemas Operacionais (Operating System)
    • Redes de Computadores (Computer network)
Logo do Site
hackerbr.net

março 19, 2024

O que é instrução break em Python? (What is break statement in Python?)

Em Português:

Em Python, a instrução break é usada para sair de um loop prematuramente quando uma condição específica é atendida. É comumente usada dentro de loops for e while para interromper o loop. Aqui está a sintaxe básica:

for item in iteravel:
    if condicao:
        break

E aqui está um exemplo com um loop while:

i = 0
while i < 10:
    if i == 5:
        break
    print(i)
    i += 1

Neste exemplo, o loop imprimirá números de 0 a 4. Quando i se torna 5, a instrução break é executada, o que interrompe o loop, e os números de 5 a 9 não são impressos. A instrução break é particularmente útil quando você está procurando por um item em uma coleção e deseja parar o loop assim que o item for encontrado.

In English:

In Python, the break statement is used to exit a loop prematurely when a certain condition is met. It’s commonly used inside for and while loops to break out of the loop. Here’s the basic syntax:

for item in iterable:
    if condition:
        break

And here’s an example with a while loop:

i = 0
while i < 10:
    if i == 5:
        break
    print(i)
    i += 1

In this example, the loop will print numbers from 0 to 4. When i becomes 5, the break statement is executed, which stops the loop, and the numbers 5 to 9 are not printed. The break statement is particularly useful when you’re searching for an item in a collection and you want to stop the loop once the item is found.

References:

[1]GfG, “break, continue and pass in Python,” GeeksforGeeks, Jul. 14, 2023. [Online]. Available: https://www.geeksforgeeks.org/break-continue-and-pass-in-python/

[2]A. Russo, “Understanding the Python &#39;break&#39; Statement: A Comprehensive Guide,” Nov. 03, 2023. [Online]. Available: https://www.gyata.ai/python/python-break/

[3]“Python break Keyword.” [Online]. Available: https://www.w3schools.com/python/ref_keyword_break.asp

[4]GfG, “Python break statement,” GeeksforGeeks, Jul. 19, 2022. [Online]. Available: https://www.geeksforgeeks.org/python-break-statement/

Linguagens (languages) Python

Navegação de Post

Previous post
Next post
©2025 hackerbr.net | WordPress Theme by SuperbThemes