Do while in python

Asked

Viewed 17,023 times

2

Is there a similar command to while de c and Java in python? I am entering the language now and already developing in java then I got this doubt

3 answers

10

No, but virtually every loop in most languages can be done in several ways.

Pseudocode:

do:
  fazCoisa()
  while condicaodesejadaparacontinuar

Writing in Py:

while True:
  fazCoisa()
  if not condicaodesejadaparacontinuar:
    break

That is, instead of making and loop if true, we reverse to always do (while True) until the desired condition is false (break to break the loop).

6

Not!

But you can Simular one of the while...

Language C:

int i = 1;

do{
  printf("%d\n", i);
  i = i + 1;
} while(i <= 3);

Python:

i = 1

while True:
    print(i)
    i = i + 1
    if(i > 3):
        break

2

Not quite.

While: The while command causes a set of instructions to be executed while a condition is met. When the result of this condition becomes false, the execution of the loop is stopped, as shown in the following example:

    contador = 0
    while (contador < 5):
           print(contador)
           contador   = contador + 1

    contador = 0
    while (contador < 5):
           print(contador)
           contador = contador + 1
    else:
           print("O loop while foi encerrado com sucesso!")

While - Else: At the end of while we can use the Else instruction. The purpose of this is to execute some instruction or block of code at the end of the loop,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.