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.
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.
123456789101112travel_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
- The index
istarts at0, pointing to the first city in the list; - The
whileloop continues as long asiis less than the length oftravel_list; - If the current city is
"Barcelona", the message is printed, and the loop exits usingbreakand"Munchen"is never reached by the loop; - If
"Barcelona"is not found, the current city is printed, andiis incremented to move to the next city.
Now, let's count how many times "Barcelona" appears in the travel_list, skipping cities that don't match using continue.
12345678910111213141516travel_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)
- The index
istarts at 0, andcounteris set to 0 to track occurrences of'Barcelona'; - The
whileloop runs as long asiis less than the length oftravel_list; - If the current city is not
'Barcelona', the loop skips the remaining code for that iteration usingcontinueand incrementsi; - For each occurrence of
'Barcelona',counteris incremented, and the loop continues until all cities are checked.
Swipe to start coding
You are organizing a country database and need to filter out only the countries that start with the letter 'S'. However, to keep the list manageable, you decide to limit the selection to just 3 countries.
- Iterate through the
countrieslist using a while loop. - Skip any country that does not start with 'S'.
- Add only the first 3 valid countries to the
selectedlist.
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 5
Loop Control Statements in a while Loop
Swipe to show menu
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.
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.
123456789101112travel_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
- The index
istarts at0, pointing to the first city in the list; - The
whileloop continues as long asiis less than the length oftravel_list; - If the current city is
"Barcelona", the message is printed, and the loop exits usingbreakand"Munchen"is never reached by the loop; - If
"Barcelona"is not found, the current city is printed, andiis incremented to move to the next city.
Now, let's count how many times "Barcelona" appears in the travel_list, skipping cities that don't match using continue.
12345678910111213141516travel_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)
- The index
istarts at 0, andcounteris set to 0 to track occurrences of'Barcelona'; - The
whileloop runs as long asiis less than the length oftravel_list; - If the current city is not
'Barcelona', the loop skips the remaining code for that iteration usingcontinueand incrementsi; - For each occurrence of
'Barcelona',counteris incremented, and the loop continues until all cities are checked.
Swipe to start coding
You are organizing a country database and need to filter out only the countries that start with the letter 'S'. However, to keep the list manageable, you decide to limit the selection to just 3 countries.
- Iterate through the
countrieslist using a while loop. - Skip any country that does not start with 'S'.
- Add only the first 3 valid countries to the
selectedlist.
Solution
Thanks for your feedback!
single