Course Content
Python Loops Tutorial
Python Loops Tutorial
Loop Control Statements in a while Loop
The break and continue statements are powerful tools for controlling the flow of a while
loop:
break
: immediately exits the loop when a specific condition is met;continue
: skips the rest of the current iteration and moves to the next one.
Additionally, the pass
statement is used as a placeholder and does nothing when executed, which can be useful for incomplete logic or avoiding errors in empty blocks.
Example: Finding a City and Exiting the Loop
Let's use a while loop to search for a specific city in the travel_list
. If the city is found, the loop will terminate using break
.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Search for "Barcelona" while i < len(travel_list): if travel_list[i] == "Barcelona": print("Found Barcelona!") break print(travel_list[i]) i += 1
Explanation:
- The index
i
starts at0
, pointing to the first city in the list; - The
while
loop continues as long asi
is less than the length oftravel_list
; - If the current city is
"Barcelona"
, the message is printed, and the loop exits usingbreak
and"Munchen"
is never reached by the loop; - If
"Barcelona"
is not found, the current city is printed, andi
is incremented to move to the next city.
Example: Counting a Specific City Using continue
Now, let's count how many times "Barcelona"
appears in the travel_list
, skipping cities that don't match using continue
.
travel_list = ["Monako", "Barcelona", "Liverpool", "Barcelona", "Munchen", "Barcelona"] # Initialize variables i = 0 counter = 0 # Count occurrences of "Barcelona" while i < len(travel_list): if travel_list[i] != "Barcelona": i += 1 continue else: counter += 1 i += 1 print("Total occurrences of 'Barcelona':", counter)
Explanation:
- The index
i
starts at 0, andcounter
is set to 0 to track occurrences of"Barcelona"
; - The
while
loop runs as long asi
is less than the length oftravel_list
; - If the current city is not
"Barcelona"
, the loop skips the remaining code for that iteration usingcontinue
and incrementsi
; - For each occurrence of
"Barcelona"
,counter
is incremented, and the loop continues until all cities are checked.
Swipe to show code editor
Write a program using a while
loop and the continue
keyword to:
- Skip cities with names longer than
7
characters. - Print only the names of cities with short names (less than or equal to 7 characters).
Thanks for your feedback!
Loop Control Statements in a while Loop
The break and continue statements are powerful tools for controlling the flow of a while
loop:
break
: immediately exits the loop when a specific condition is met;continue
: skips the rest of the current iteration and moves to the next one.
Additionally, the pass
statement is used as a placeholder and does nothing when executed, which can be useful for incomplete logic or avoiding errors in empty blocks.
Example: Finding a City and Exiting the Loop
Let's use a while loop to search for a specific city in the travel_list
. If the city is found, the loop will terminate using break
.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Search for "Barcelona" while i < len(travel_list): if travel_list[i] == "Barcelona": print("Found Barcelona!") break print(travel_list[i]) i += 1
Explanation:
- The index
i
starts at0
, pointing to the first city in the list; - The
while
loop continues as long asi
is less than the length oftravel_list
; - If the current city is
"Barcelona"
, the message is printed, and the loop exits usingbreak
and"Munchen"
is never reached by the loop; - If
"Barcelona"
is not found, the current city is printed, andi
is incremented to move to the next city.
Example: Counting a Specific City Using continue
Now, let's count how many times "Barcelona"
appears in the travel_list
, skipping cities that don't match using continue
.
travel_list = ["Monako", "Barcelona", "Liverpool", "Barcelona", "Munchen", "Barcelona"] # Initialize variables i = 0 counter = 0 # Count occurrences of "Barcelona" while i < len(travel_list): if travel_list[i] != "Barcelona": i += 1 continue else: counter += 1 i += 1 print("Total occurrences of 'Barcelona':", counter)
Explanation:
- The index
i
starts at 0, andcounter
is set to 0 to track occurrences of"Barcelona"
; - The
while
loop runs as long asi
is less than the length oftravel_list
; - If the current city is not
"Barcelona"
, the loop skips the remaining code for that iteration usingcontinue
and incrementsi
; - For each occurrence of
"Barcelona"
,counter
is incremented, and the loop continues until all cities are checked.
Swipe to show code editor
Write a program using a while
loop and the continue
keyword to:
- Skip cities with names longer than
7
characters. - Print only the names of cities with short names (less than or equal to 7 characters).
Thanks for your feedback!