Conteúdo do Curso
Python Loops Tutorial
Python Loops Tutorial
Break/Continue in a while Loop
Now, we'll attempt the same task we previously accomplished with the for
loop—finding a number in the list and terminating the loop once it's located.
Examine the code below:
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
How does the code work?
Great!
Please be aware that the continue
statement skips a block of code within the loop for the current iteration only.
Now, let's count the occurrences of 11s in the code using continue
.
Examine the code below:
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
How does the code work?
Tarefa
Halt the loop when encountering a negative value.
- Initialize a
while
loop, employingi
to interact withnumbers
. - Increment the value of
i
. - Display a message along with the negative element.
- Employ
break
andcontinue
as needed.
Obrigado pelo seu feedback!
Break/Continue in a while Loop
Now, we'll attempt the same task we previously accomplished with the for
loop—finding a number in the list and terminating the loop once it's located.
Examine the code below:
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
How does the code work?
Great!
Please be aware that the continue
statement skips a block of code within the loop for the current iteration only.
Now, let's count the occurrences of 11s in the code using continue
.
Examine the code below:
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
How does the code work?
Tarefa
Halt the loop when encountering a negative value.
- Initialize a
while
loop, employingi
to interact withnumbers
. - Increment the value of
i
. - Display a message along with the negative element.
- Employ
break
andcontinue
as needed.
Obrigado pelo seu feedback!
Break/Continue in a while Loop
Now, we'll attempt the same task we previously accomplished with the for
loop—finding a number in the list and terminating the loop once it's located.
Examine the code below:
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
How does the code work?
Great!
Please be aware that the continue
statement skips a block of code within the loop for the current iteration only.
Now, let's count the occurrences of 11s in the code using continue
.
Examine the code below:
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
How does the code work?
Tarefa
Halt the loop when encountering a negative value.
- Initialize a
while
loop, employingi
to interact withnumbers
. - Increment the value of
i
. - Display a message along with the negative element.
- Employ
break
andcontinue
as needed.
Obrigado pelo seu feedback!
Now, we'll attempt the same task we previously accomplished with the for
loop—finding a number in the list and terminating the loop once it's located.
Examine the code below:
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
How does the code work?
Great!
Please be aware that the continue
statement skips a block of code within the loop for the current iteration only.
Now, let's count the occurrences of 11s in the code using continue
.
Examine the code below:
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
How does the code work?
Tarefa
Halt the loop when encountering a negative value.
- Initialize a
while
loop, employingi
to interact withnumbers
. - Increment the value of
i
. - Display a message along with the negative element.
- Employ
break
andcontinue
as needed.