Python Break, Continue and Pass
Example 1:
for i in range(1, 6):
if i == 3:
break # Exits the loop when i equals 3
print(i, end=’ ’)
#Output: 1 2
Here, it breaks out of the loop as i becomes 3. So, no further iterations will execute.
Example 2:
Write a program to search for a specific laptop in a list of laptops. If the brand “apple” laptop is found, print a message saying “Yes, we want an Apple company laptop” and then exit the loop using a break statement.
laptops = [‘windows’, ‘samsung’, ‘apple’, ‘dell’, ‘asus’]
laptops = [‘windows’, ‘samsung’, ‘apple’, ‘dell’, ‘asus’]
for laptop in laptops:
if laptop == ‘apple’:
print(‘Yes, we want an Apple company Laptop’)
break #break out of loop
#Output: Yes, we want an Apple company Laptop
Explanation:
laptops = [‘windows’, ‘samsung’, ‘apple’, ‘dell’, ‘asus’]
This line creates a list named laptops with the five string values: “windows”, “samsung”, “apple”, “dell”, and “asus”.
for laptop in laptops:
This line starts a for loop that goes through each element in the laptops array. For each iteration, the current element is stored in the variable laptop.
1. The laptop is “windows”. The condition laptop == “apple” is false, so the loop continues to the next item.
2. The laptop is “samsung”. The condition laptop == “apple” is false, so the loop continues to the next item.
3. The laptop is “apple”. The condition laptop == “apple” is true, so the code inside the if statement runs:
print(‘Yes, we want an Apple company Laptop’)
It prints “Yes, we want an Apple company laptop”.
break
The break statement stops the loop execution and comes out of loop.
2. continue statement
The continue statement is used to skip the current iteration of the loop and move to the next iteration. It doesn’t terminate the loop but skips the rest of the code inside the loop for the current iteration.
Example 1:
for i in range(1, 6):
if i == 3:
continue # Skips the current iteration when i equals 3
print(i, end=’ ‘)
# Output: 1 2 4 5 6
In this example, the number 3 is skipped, and the loop continues with the next iteration.
Example 2:
Write a program to search for a specific laptop in a list of laptops. If the brand “Apple” laptop is found, print a message saying, “We don’t want an Apple company laptop” and then continue to the next brand using a continue statement.
laptops = [‘windows’, ‘samsung’, ‘apple’, ‘dell’, ‘asus’]
for laptop in laptops:
if laptop == ‘apple’:
print(“We don’t want an Apple company Laptop”)
continue
#Output: We don’t want an Apple company Laptop
laptops = [‘windows’, ‘samsung’, ‘apple’, ‘dell’, ‘asus’]
This line creates a list named laptops with the five string values: “windows”, “samsung”, “apple”, “dell”, and “asus”.
for laptop in laptops:
This line starts a for loop that goes through each element in the laptops array. For each iteration, the current element is stored in the variable laptop.
1. The laptop is “windows”. The condition laptop == “apple” is false, so the loop continues to the next item.
2. The laptop is “samsung”. The condition laptop == “apple” is false, so the loop continues to the next item.
3. The laptop is “apple”. The condition laptop == “apple” is true, so the code inside the if statement runs:
print(“We don’t want an Apple company Laptop”)
This prints “We don’t want an Apple company Laptop”
continue
continue statement will skip the current iteration and again go to next iteration
and execute the loop till the end.
for i in range(1, 6):
if i == 3:
pass # Placeholder for future code (Does nothing)
print(i, end=’ ‘)
# Output: 1 2 3 4 5
In this example, the pass statement does nothing, so the loop continues as usual.
Key Differences:
break: Exits the loop completely.
continue: Skips the current iteration and moves to the next one.
pass: Does nothing, acts as a placeholder for future code.
Course Video
Course Video English:
Task:
1. Imagine you’re searching for a specific fruit in the market like you want an apple from this list [mango , orange ,watermelon ,apple , grapes] . If you get any apple fruit you can break the fruit list.
For example : First you must create list for fruits and then use that list in loop to check in if condition which fruit you want from list once found, then break the loop same as below
2.You visit a clothing boutique and use the concept of break to stop searching once you find the black t-shirt you’re looking for.
For example: First you must create list for clothes and then use that list in loop then check in if condition which cloth you want from list once find then break the loop same as below
3. Imagine a school or college organizing a racing event where participants are required to have a height greater than 60 inches to qualify for the race. Imagine the heights of six participants is : 60 inches, 62 inches, 58 inches, 65 inches, 55 inches, and 68 inches if height is less than 60 then players don’t participate in racing.
For example : First you must create integer list for height data and then use that list in loop then check in if condition which height is less than 60 and use continue to skip that participant and show like below
4. Who are the people in your life who inspire you the most ? from which you not inspired skip that person by using continue
For example : First create string array with values “brother, father ,mother ,sister , teacher” then use loop and check in loop using if condition the names from you didn’t inspire skip them and output like below
5. Find First Even Number:
Use a for loop to iterate from 1 to 20.
Inside the loop, use an if-else statement to check if the number is even.
Use a break statement to stop the loop once the first even number is found.
Output: 2
6. Skip Multiples of 3:
Use a for loop to iterate from 1 to 15.
Inside the loop, use an if-else statement to check if the number is a multiple of 3.
Use a continue statement to skip printing multiples of 3.
Output: 1 2 4 5 7 8 10 11 13 14
7. Prime Numbers Until Limit:
Use a while loop to print prime numbers starting from 1.
Inside the loop, use an if-else statement to check if the number is prime.
Use a break statement to stop the loop when the prime number exceeds 10.
Output: 2,3,5,7
8. Sum Until Negative:
Use a while loop to continuously ask the user for a number.
Use an if-else statement to check if the entered number is negative.
Use a break statement to exit the loop if a negative number is entered.
9. Guessing Game with Attempts:
Use a while loop to implement a number guessing game with a maximum of 5 attempts.
Use an if-else statement to check if the guessed number is higher, lower, or equal to the target number.
Use a break statement to exit the loop if the correct number is guessed within 5 attempts.
10. Menu with Exit Option:
Use a while loop to display a menu with options.
Use a switch statement to handle different menu selections.
Use a break statement inside the switch case to exit the loop when the user chooses the exit option.
11. Print Fibonacci Sequence:
Use a for loop to print the Fibonacci sequence up to 100.
Use an if-else statement to check if the next number exceeds 100.
Use a break statement to stop the loop when the sequence exceeds 100.
12. Filter Negative Numbers:
Use a while loop to iterate through an array of integers.
Use an if-else statement to check if the current number is negative.
Use a continue statement to skip processing negative numbers.
13. Days of the Week:
Use a for loop to iterate through an array of days of the week.
Inside the loop, use a switch statement to print “Weekday” for Monday to Friday and “Weekend” for Saturday and Sunday.
Use a break statement to exit the loop if the day is “Sunday”.
14. Temperature Conversion Until Zero:
Use a while loop to continuously ask the user for a temperature in Celsius.
Use an if-else statement to convert the temperature to Fahrenheit.
Use a break statement to stop the loop if the entered temperature is 0.
Task Video
YouTube Reference :
It is used to exit a loop prematurely when a certain condition is met.
break: Exits the loop.,continue: Skips the current iteration and moves to the next.,pass: Does nothing and is used as a placeholder.
Use the break statement inside a loop to terminate it early.
A debugging tool to pause program execution at a specific line.
break: Stops a loop.,return: Exits a function and optionally returns a value.
Skips the rest of the current loop iteration and moves to the next one.
It is the continue keyword used within loops.
Yes, it is considered a jump statement as it transfers control to the next loop iteration.
A function calling itself to solve smaller instances of a problem.
The process of repeatedly executing a block of code while a condition is met, using constructs like for or while loops.