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

If/Else in a while LoopIf/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:

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.

¿Todo estuvo claro?

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

Contenido del Curso

Python Loops Tutorial

If/Else in a while LoopIf/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:

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.

¿Todo estuvo claro?

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