Programming Exercises 002 - Break and Continue
Next Topic(s):
Created:
3rd of October 2024
09:57:08 AM
Modified:
3rd of October 2024
12:30:11 PM
Python Programming Exercises on break
and continue
Below are 20 Python programming exercises that help you understand the use of break
and continue
statements effectively. These exercises will also show how to use them both within and outside loops for control flow. Each exercise includes a hint to help you. Try to solve them on your own before looking at the solutions. Save your work in the specified files.
Exercise 1: Stop Printing at a Specific Value
Filename: e01_stop_at_value.py
Write a program to print numbers from 1 to 10, but stop the loop if the number 7 is reached.
Hint: Use a for
loop and a break
statement.
Exercise 2: Skip Even Numbers
Filename: e02_skip_even_numbers.py
Write a program to print all odd numbers between 1 and 20.
Hint: Use a for
loop and a continue
statement to skip even numbers.
Exercise 3: Find a Character in a String
Filename: e03_find_character.py
Write a program to search for the character 'e' in the string "Hello, Python!" and stop the loop when it is found.
Hint: Use a for
loop and a break
statement.
Exercise 4: Skip Negative Numbers
Filename: e04_skip_negative_numbers.py
Write a program to iterate over a list of integers and print only the positive values.
Hint: Use a for
loop and a continue
statement to skip negative numbers.
Exercise 5: Find Prime Numbers with Early Exit
Filename: e05_prime_numbers_break.py
Write a program to print prime numbers from 2 to 50. Stop the loop if a non-prime number greater than 30 is found.
Hint: Use a nested loop and a break
statement to exit when needed.
Exercise 6: Print Until Sum Exceeds Limit
Filename: e06_sum_limit.py
Write a program to add numbers from 1 upwards until the sum exceeds 100, then stop the loop.
Hint: Use a while
loop and a break
statement to exit when the condition is met.
Exercise 7: Skip Specific Values
Filename: e07_skip_specific_values.py
Write a program to print numbers from 1 to 10, but skip numbers 3 and 6.
Hint: Use a for
loop and a continue
statement.
Exercise 8: Find First Divisible Number
Filename: e08_find_divisible.py
Write a program to find and print the first number between 1 and 50 that is divisible by 9.
Hint: Use a for
loop and a break
statement.
Exercise 9: Loop Until Specific Input
Filename: e09_loop_until_input.py
Write a program to repeatedly ask for user input until the user types "quit".
Hint: Use a while
loop and a break
statement when the input is "quit".
Exercise 10: Skip Lowercase Letters
Filename: e10_skip_lowercase.py
Write a program to iterate through a string and print only the uppercase letters.
Hint: Use a for
loop and a continue
statement to skip lowercase letters.
Exercise 11: Stop on a Specific Value in a List
Filename: e11_stop_at_list_value.py
Write a program to iterate through a list of numbers and stop when you find the number 15.
Hint: Use a for
loop and a break
statement.
Exercise 12: Skip Empty Strings
Filename: e12_skip_empty_strings.py
Write a program to iterate over a list of strings and print only non-empty strings.
Hint: Use a for
loop and a continue
statement to skip empty strings.
Exercise 13: End Loop on User Condition
Filename: e13_end_on_condition.py
Write a program that keeps asking the user for numbers and prints them until the user enters a negative number.
Hint: Use a while
loop and break
when the input is negative.
Exercise 14: Skip Multiples of 5
Filename: e14_skip_multiples_of_5.py
Write a program to print numbers from 1 to 20, but skip all numbers that are multiples of 5.
Hint: Use a for
loop and a continue
statement to skip multiples of 5.
Exercise 15: Exit Loop on Specific Character
Filename: e15_exit_on_character.py
Write a program to iterate through the string "PythonProgramming" and stop when the character 'g' is found.
Hint: Use a for
loop and a break
statement.
Exercise 16: Count Only Non-Vowels
Filename: e16_count_non_vowels.py
Write a program to count and print the number of non-vowel characters in a given string.
Hint: Use a for
loop and a continue
statement to skip vowels.
Exercise 17: Break on Special Character
Filename: e17_break_on_special.py
Write a program that iterates over a string and stops when a special character (like '@', '!', etc.) is found.
Hint: Use a for
loop and a break
statement.
Exercise 18: Skip Zero in a List
Filename: e18_skip_zero.py
Write a program to iterate through a list of numbers and print all values except 0.
Hint: Use a for
loop and a continue
statement to skip zero.
Exercise 19: Exit on User Input
Filename: e19_exit_on_user_input.py
Write a program that asks for user input continuously until the user types "exit".
Hint: Use a while
loop and a break
statement when the input is "exit".
Exercise 20: Skip Characters in Range
Filename: e20_skip_characters.py
Write a program to print each character in a string, but skip characters from index 3 to 5.
Hint: Use a for
loop and a continue
statement with a conditional statement for the index.
Key Takeaways
These exercises will help you understand how to effectively use break
and continue
to control the flow of loops in Python. Practice each example, and understand how these control statements change the behavior of loops to gain mastery over conditional logic in your programs.
Solutions are available on the GITHUB page. [https://githu...]