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

Course Content

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?

Task
test

Swipe to show code editor

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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?

Task
test

Swipe to show code editor

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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?

Task
test

Swipe to show code editor

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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?

Task
test

Swipe to show code editor

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt