Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If/Else in a while Loop | The while Loop
Python Loops Tutorial
course content

Contenido del Curso

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

bookIf/Else in a while Loop

We previously used the if/else structure in conjunction with for loops, and we can apply the same concept to while loops.

An if/else structure within a while loop functions when the while condition becomes False.

Examine the code below:

1234567891011
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
copy

How does the code work?

Tarea

Multiply all the negative numbers in the list.

  1. Initialize a while loop, utilizing i to interact with numbers.
  2. Establish the condition if an element is less than 0.
  3. Multiply the element and accumulate it in the counter.
  4. Update the value of i to change the list index.
  5. Display the value stored in the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
toggle bottom row

bookIf/Else in a while Loop

We previously used the if/else structure in conjunction with for loops, and we can apply the same concept to while loops.

An if/else structure within a while loop functions when the while condition becomes False.

Examine the code below:

1234567891011
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
copy

How does the code work?

Tarea

Multiply all the negative numbers in the list.

  1. Initialize a while loop, utilizing i to interact with numbers.
  2. Establish the condition if an element is less than 0.
  3. Multiply the element and accumulate it in the counter.
  4. Update the value of i to change the list index.
  5. Display the value stored in the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
toggle bottom row

bookIf/Else in a while Loop

We previously used the if/else structure in conjunction with for loops, and we can apply the same concept to while loops.

An if/else structure within a while loop functions when the while condition becomes False.

Examine the code below:

1234567891011
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
copy

How does the code work?

Tarea

Multiply all the negative numbers in the list.

  1. Initialize a while loop, utilizing i to interact with numbers.
  2. Establish the condition if an element is less than 0.
  3. Multiply the element and accumulate it in the counter.
  4. Update the value of i to change the list index.
  5. Display the value stored in the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

We previously used the if/else structure in conjunction with for loops, and we can apply the same concept to while loops.

An if/else structure within a while loop functions when the while condition becomes False.

Examine the code below:

1234567891011
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
copy

How does the code work?

Tarea

Multiply all the negative numbers in the list.

  1. Initialize a while loop, utilizing i to interact with numbers.
  2. Establish the condition if an element is less than 0.
  3. Multiply the element and accumulate it in the counter.
  4. Update the value of i to change the list index.
  5. Display the value stored in the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt