Loop Control Statements
When working with loops, the break and continue statements help manage the flow of iteration:
break
: exits the loop prematurely when a condition is met;continue
: skips the current iteration and moves to the next one, allowing selective execution;pass
: is a placeholder that does nothing when executed.
Using break
Keyword
Imagine searching for a specific city in a list. If we want to stop searching as soon as we find the city "Barcelona"
, we can use the break
statement.
123456789travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Searching for a specific city for city in travel_list: if city == 'Barcelona': print('Found Barcelona!') break else: print(city, 'is not Barcelona')
- The loop iterates through each city in the
travel_list
; - When it encounters
"Barcelona"
, thebreak
statement is executed, and the loop stops immediately; - Cities after
"Barcelona"
(like"Munchen"
) are not processed.
Using continue
Keyword
Let's now count the cities in the travel_list
that have names shorter than 8 characters while skipping others.
12345678910travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] short_name_count = 0 for city in travel_list: if len(city) >= 8: continue # Skip cities with names 8 or more characters long short_name_count += 1 print('Number of cities with names shorter than 8 characters:', short_name_count)
- The loop iterates through each city in the
travel_list
; - If the length of the city's name is 8 characters or more, the
if
condition evaluates toTrue
, and thecontinue
statement is executed. This skips the rest of the code for that iteration; - For cities with names shorter than 8 characters, the counter
short_name_count
is incremented by 1; - After the loop finishes, the final count is printed, showing how many cities have names shorter than 8 characters.
The pass
Keyword
The pass statement in Python is a placeholder that does nothing when executed. It's often used as a temporary placeholder for code you plan to write later, allowing the program to run without errors. Later, you can replace pass
with actual logic.
12345678travel_list = ['Monaco', 'Luxembourg', 'Liverpool', 'Barcelona', 'Munich'] already_visited = ['Barcelona', 'Monaco'] for city in travel_list: if city in already_visited: pass else: print('Going to visit', city)
Swipe to start coding
You're planning your next adventure and want to prioritize visa-free travel to make the journey smoother. To keep things manageable, you decide to limit your list to only 10 destinations.
- Iterate through the countries list. Skip the countries that require a visa.
- Add only visa-free countries to
travel_list
. - Stop adding once
travel_list
contains 10 countries.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5Awesome!
Completion rate improved to 5
Loop Control Statements
When working with loops, the break and continue statements help manage the flow of iteration:
break
: exits the loop prematurely when a condition is met;continue
: skips the current iteration and moves to the next one, allowing selective execution;pass
: is a placeholder that does nothing when executed.
Using break
Keyword
Imagine searching for a specific city in a list. If we want to stop searching as soon as we find the city "Barcelona"
, we can use the break
statement.
123456789travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Searching for a specific city for city in travel_list: if city == 'Barcelona': print('Found Barcelona!') break else: print(city, 'is not Barcelona')
- The loop iterates through each city in the
travel_list
; - When it encounters
"Barcelona"
, thebreak
statement is executed, and the loop stops immediately; - Cities after
"Barcelona"
(like"Munchen"
) are not processed.
Using continue
Keyword
Let's now count the cities in the travel_list
that have names shorter than 8 characters while skipping others.
12345678910travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] short_name_count = 0 for city in travel_list: if len(city) >= 8: continue # Skip cities with names 8 or more characters long short_name_count += 1 print('Number of cities with names shorter than 8 characters:', short_name_count)
- The loop iterates through each city in the
travel_list
; - If the length of the city's name is 8 characters or more, the
if
condition evaluates toTrue
, and thecontinue
statement is executed. This skips the rest of the code for that iteration; - For cities with names shorter than 8 characters, the counter
short_name_count
is incremented by 1; - After the loop finishes, the final count is printed, showing how many cities have names shorter than 8 characters.
The pass
Keyword
The pass statement in Python is a placeholder that does nothing when executed. It's often used as a temporary placeholder for code you plan to write later, allowing the program to run without errors. Later, you can replace pass
with actual logic.
12345678travel_list = ['Monaco', 'Luxembourg', 'Liverpool', 'Barcelona', 'Munich'] already_visited = ['Barcelona', 'Monaco'] for city in travel_list: if city in already_visited: pass else: print('Going to visit', city)
Swipe to start coding
You're planning your next adventure and want to prioritize visa-free travel to make the journey smoother. To keep things manageable, you decide to limit your list to only 10 destinations.
- Iterate through the countries list. Skip the countries that require a visa.
- Add only visa-free countries to
travel_list
. - Stop adding once
travel_list
contains 10 countries.
Solution
Thanks for your feedback!
single
Awesome!
Completion rate improved to 5
Loop Control Statements
Swipe to show menu
When working with loops, the break and continue statements help manage the flow of iteration:
break
: exits the loop prematurely when a condition is met;continue
: skips the current iteration and moves to the next one, allowing selective execution;pass
: is a placeholder that does nothing when executed.
Using break
Keyword
Imagine searching for a specific city in a list. If we want to stop searching as soon as we find the city "Barcelona"
, we can use the break
statement.
123456789travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Searching for a specific city for city in travel_list: if city == 'Barcelona': print('Found Barcelona!') break else: print(city, 'is not Barcelona')
- The loop iterates through each city in the
travel_list
; - When it encounters
"Barcelona"
, thebreak
statement is executed, and the loop stops immediately; - Cities after
"Barcelona"
(like"Munchen"
) are not processed.
Using continue
Keyword
Let's now count the cities in the travel_list
that have names shorter than 8 characters while skipping others.
12345678910travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] short_name_count = 0 for city in travel_list: if len(city) >= 8: continue # Skip cities with names 8 or more characters long short_name_count += 1 print('Number of cities with names shorter than 8 characters:', short_name_count)
- The loop iterates through each city in the
travel_list
; - If the length of the city's name is 8 characters or more, the
if
condition evaluates toTrue
, and thecontinue
statement is executed. This skips the rest of the code for that iteration; - For cities with names shorter than 8 characters, the counter
short_name_count
is incremented by 1; - After the loop finishes, the final count is printed, showing how many cities have names shorter than 8 characters.
The pass
Keyword
The pass statement in Python is a placeholder that does nothing when executed. It's often used as a temporary placeholder for code you plan to write later, allowing the program to run without errors. Later, you can replace pass
with actual logic.
12345678travel_list = ['Monaco', 'Luxembourg', 'Liverpool', 'Barcelona', 'Munich'] already_visited = ['Barcelona', 'Monaco'] for city in travel_list: if city in already_visited: pass else: print('Going to visit', city)
Swipe to start coding
You're planning your next adventure and want to prioritize visa-free travel to make the journey smoother. To keep things manageable, you decide to limit your list to only 10 destinations.
- Iterate through the countries list. Skip the countries that require a visa.
- Add only visa-free countries to
travel_list
. - Stop adding once
travel_list
contains 10 countries.
Solution
Thanks for your feedback!