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 é a instrução while em python? (What is while statement in python?)

Em Português:

Em Python, uma instrução while é usada para criar um loop que executa um bloco de código enquanto uma condição especificada for verdadeira. Aqui está a sintaxe básica:

while condicao:
    # bloco de código a ser executado

Aqui está um exemplo de como funciona:

i = 1
while i < 6:
    print(i)
    i += 1

Neste exemplo, o bloco de código continuará imprimindo o valor de i e, em seguida, incrementará i em 1, enquanto i for menor que 6. Quando i atingir 6, a condição i < 6 se torna falsa e o loop para de ser executado. Lembre-se de incluir uma declaração dentro do loop que eventualmente tornará a condição falsa, ou o loop continuará indefinidamente, o que é conhecido como um loop infinito.

In English:

In Python, a while statement is used to create a loop that executes a block of code as long as a specified condition is true. Here’s the basic syntax:

while condition:
    # code block to be executed

Here’s an example of how it works:

i = 1
while i < 6:
    print(i)
    i += 1

In this example, the code block will keep printing the value of i and then increment i by 1, as long as i is less than 6. When i reaches 6, the condition i < 6 becomes false, and the loop stops executing. Remember to include a statement within the loop that will eventually make the condition false, or else the loop will continue indefinitely, which is known as an infinite loop.

References:

[1]“Python While Loop Statement Explained,” freeCodeCamp.org, Apr. 28, 2021. [Online]. Available: https://www.freecodecamp.org/news/python-while-loop-statement-explained/

[2]“Python While Loops (With Best Practices) – PythonHello,” PythonHello. [Online]. Available: https://www.pythonhello.com/fundamentals/python-while-loops

[3]“Python While Loop.” [Online]. Available: https://pythonexamples.org/python-while-loop-example/

[4]R. Python, “Python ‘while’ Loops (Indefinite Iteration),” Jul. 18, 2022. [Online]. Available: https://realpython.com/python-while-loop/

[5]“Python While Loops.” [Online]. Available: https://www.w3schools.com/python/python_while_loops.asp

Linguagens (languages) Python

Navegação de Post

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