Course Content
Python Loops Tutorial
Python Loops Tutorial
If/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?

Task
Multiply all the negative numbers in the list.
- Initialize a
while
loop, utilizingi
to interact withnumbers
. - Establish the condition if an element is less than 0.
- Multiply the element and accumulate it in the
counter
. - Update the value of
i
to change the list index. - Display the value stored in the
counter
.
Everything was clear?
Section 2. Chapter 4