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

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

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

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:

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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 4
toggle bottom row

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:

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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 4
toggle bottom row

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:

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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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?

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 4
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt