In the previous lessons, you learned how to use the for loop and the while loop to repeat a block of code multiple times. Loops are powerful because they automate repetitive tasks, but sometimes you need more control over how a loop behaves.
For example, consider the following situations:
In these situations, the normal execution of a loop needs to be modified. Python provides three special statements for this purpose:
break – Terminates the loop immediately.continue – Skips the current iteration and moves to the next one.pass – Acts as a placeholder and does nothing.These statements are known as loop control statements. They allow you to control the flow of execution inside loops and conditional statements, making your programs more flexible and easier to manage.
After completing this lesson, you will be able to:
break, continue, and pass statements.break, continue, and pass.Loop control statements are special Python statements that modify the normal execution of loops.
Normally, a loop executes every iteration until its condition becomes false or all items in a sequence have been processed.
Loop control statements allow you to:
These statements improve program flexibility and make complex logic easier to implement.
break, continue, and pass?Consider a program that searches for a student’s name in a list.
Once the name has been found, there is no need to continue searching through the remaining items.
Similarly, when processing data, some records may be invalid. Instead of stopping the entire loop, you may simply want to skip those records.
During program development, you may also need to leave a block of code empty temporarily while continuing to build the rest of the application.
Python provides the break, continue, and pass statements to solve these situations.
break StatementThe break statement immediately terminates the nearest enclosing loop.
Once Python encounters a break statement, the loop stops immediately, even if more iterations remain.
break
for number in range(1, 11):
if number == 6:
break
print(number)
Output
1
2
3
4
5
When the value becomes 6, Python executes the break statement and immediately exits the loop.
continue StatementThe continue statement skips the remaining statements in the current iteration and immediately starts the next iteration.
Unlike the break statement, it does not terminate the loop.
continue
for number in range(1, 6):
if number == 3:
continue
print(number)
Output
1
2
4
5
The value 3 is skipped, but the loop continues executing the remaining iterations.
pass StatementThe pass statement is a null statement. It performs no action and simply allows the program to continue.
It is mainly used as a placeholder where Python expects a statement but no action has been implemented yet.
pass
for number in range(1, 6):
if number == 3:
pass
print(number)
Output
1
2
3
4
5
Although the pass statement is executed, it does nothing. The loop behaves normally.
Each loop control statement changes program execution in a different way.
| Statement | Effect |
|---|---|
break |
Stops the loop immediately. |
continue |
Skips the current iteration and continues with the next one. |
pass |
Does nothing and acts as a placeholder. |
Choosing the correct statement depends on how you want the loop to behave.
break, continue, and passbreak |
continue |
pass |
|---|---|---|
| Terminates the loop. | Skips one iteration. | Does nothing. |
| Execution moves outside the loop. | Execution moves to the next iteration. | Execution continues normally. |
| Used to stop processing. | Used to ignore selected iterations. | Used as a temporary placeholder. |
| Works in loops. | Works in loops. | Works in loops, functions, classes, and conditional statements. |
Consider the following program.
for number in range(1, 6):
if number == 4:
break
print(number)
print("Program Finished")
Execution Steps
break statement executes.Output
1
2
3
Program Finished
break when the loop should stop immediately after a condition is satisfied.continue when certain iterations should be skipped without stopping the loop.pass when writing placeholder code that will be implemented later.In this section, you learned what Python loop control statements are and why they are important. You explored the break, continue, and pass statements, learned their syntax, understood how each one affects loop execution, and compared their behavior. You also followed the execution flow of a program using the break statement and learned when each control statement should be used. In the next section, you will learn how to use these statements with for loops and while loops, use pass in functions and conditional statements, and avoid common beginner mistakes.
In the previous section, you learned the basic purpose of the break, continue, and pass statements. In this section, you will learn how these statements work inside for loops and while loops, how the pass statement is used in functions and conditional statements, and the common mistakes that beginners should avoid.
break Statement with a for LoopThe break statement immediately terminates the loop when a specified condition is met.
for number in range(1, 11):
if number == 7:
break
print(number)
Output
1
2
3
4
5
6
As soon as the value becomes 7, the break statement executes, and Python exits the loop.
break Statement with a while LoopThe break statement works exactly the same way inside a while loop.
count = 1
while True:
if count == 6:
break
print(count)
count += 1
Output
1
2
3
4
5
Although the loop condition is always True, the break statement stops the loop when count becomes 6.
continue Statement with a for LoopThe continue statement skips the remaining statements in the current iteration and moves directly to the next iteration.
for number in range(1, 8):
if number == 4:
continue
print(number)
Output
1
2
3
5
6
7
The value 4 is skipped, while the remaining values are processed normally.
continue Statement with a while LoopThe continue statement also works with while loops.
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
Output
1
2
4
5
The number 3 is skipped because Python immediately begins the next iteration.
pass Statement in LoopsThe pass statement allows you to create an empty code block without generating an error.
for number in range(1, 6):
if number == 3:
pass
print(number)
Output
1
2
3
4
5
The pass statement performs no action. Python simply continues executing the remaining statements.
pass Statement in FunctionsSometimes you may want to create a function but implement it later.
def display_message():
pass
The function is valid even though it contains no executable code.
This is useful during program development when creating the structure of an application.
pass Statement in Conditional StatementsThe pass statement can also be used inside if statements.
age = 20
if age >= 18:
pass
print("Program Completed")
Output
Program Completed
The program executes successfully even though the if block contains no actual code.
Consider the following program.
for letter in "Python":
if letter == "h":
continue
print(letter)
print("Loop Finished")
Execution Steps
P.P is printed.y.h, the continue statement executes.Output
P
y
t
o
n
Loop Finished
break Outside a Loopbreak
This generates a SyntaxError because break can only be used inside loops.
continue Outside a Loopcontinue
This also generates a SyntaxError.
continuecount = 1
while count <= 5:
if count == 3:
continue
count += 1
This creates an infinite loop because count never changes after reaching 3.
pass with continueThe pass statement does not skip an iteration. It simply does nothing.
break When continue Is RequiredRemember:
break ends the loop completely.continue skips only one iteration.break only when the loop should stop immediately.continue only when specific iterations should be ignored.pass as a temporary placeholder during development.continue inside a while loop.In this section, you learned how to use the break, continue, and pass statements with both for loops and while loops. You also explored how the pass statement can be used in functions and conditional statements, followed the execution flow of a program using continue, and reviewed common beginner mistakes and coding best practices. In the next section, you will apply these concepts by building practical programs such as number search systems, password verification, ATM menus, login attempts, skipping even numbers, and placeholder functions.
The break, continue, and pass statements are frequently used in real-world Python programs to control the flow of loops and program execution. They help developers stop loops early, skip unwanted iterations, and create placeholder code while applications are still under development.
In this section, you will build practical programs that demonstrate how these loop control statements are used in everyday programming.
break, continue, and passbreakSuppose you want to search for a number in a list. Once the number is found, there is no reason to continue searching.
numbers = [12, 25, 37, 48, 59]
search = 37
for number in numbers:
if number == search:
print("Number Found")
break
print("Checking:", number)
Output
Checking: 12
Checking: 25
Number Found
The break statement stops the loop immediately after finding the required number.
breakThe following program allows the user to enter the password repeatedly until the correct password is entered.
correct_password = "python123"
while True:
password = input("Enter Password: ")
if password == correct_password:
print("Access Granted")
break
print("Incorrect Password")
Sample Output
Enter Password: admin
Incorrect Password
Enter Password: hello
Incorrect Password
Enter Password: python123
Access Granted
The loop continues until the break statement executes.
An ATM menu usually remains active until the customer chooses the Exit option.
while True:
print("1. Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Exit")
choice = int(input("Enter Choice: "))
if choice == 4:
print("Thank You")
break
Sample Output
1. Balance
2. Deposit
3. Withdraw
4. Exit
Enter Choice: 4
Thank You
The ATM continues displaying the menu until the user selects Exit.
continueThe following program prints only odd numbers between 1 and 10.
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
Output
1
3
5
7
9
The continue statement skips every even number.
This program limits the number of login attempts.
correct_password = "python123"
attempt = 1
while attempt <= 3:
password = input("Enter Password: ")
if password == correct_password:
print("Login Successful")
break
print("Incorrect Password")
attempt += 1
Sample Output
Enter Password: admin
Incorrect Password
Enter Password: hello
Incorrect Password
Enter Password: python123
Login Successful
The loop terminates immediately after successful authentication.
passDevelopers often create functions before implementing their actual code.
def calculate_salary():
pass
print("Program Started")
Output
Program Started
The function exists but contains no executable statements.
The pass statement is also useful when creating an empty class.
class Student:
pass
print("Class Created")
Output
Class Created
The following program skips spaces while printing the characters of a sentence.
sentence = "Learn Python"
for character in sentence:
if character == " ":
continue
print(character)
Output
L
e
a
r
n
P
y
t
h
o
n
Consider the following program.
for number in range(1, 6):
if number == 3:
continue
print(number)
print("Loop Completed")
Execution Steps
continue statement executes.Output
1
2
4
5
Loop Completed
break Only When NecessaryTerminate a loop early only when no further iterations are required.
continueToo many continue statements can make program flow difficult to follow.
pass Only as a Temporary PlaceholderReplace pass with actual implementation once the code is ready.
Write clear and readable conditions when using loop control statements.
Always verify that the loop exits correctly and does not become an unintended infinite loop.
break when only one iteration should be skipped.continue without updating the loop variable in a while loop.pass statements in production code.break or continue statements, making the code difficult to understand.break, continue, and pass.In this section, you applied the break, continue, and pass statements to practical programming problems such as searching for values, password verification, ATM menus, login systems, skipping even numbers, ignoring spaces in strings, and creating placeholder functions and classes. You also explored execution flow, coding best practices, and common beginner mistakes. In the final section, you will review everything covered in this lesson through a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Functions.
In this lesson, you learned how the Python break, continue, and pass statements control the normal execution of loops and other code blocks. These statements provide greater flexibility by allowing you to stop a loop early, skip specific iterations, or create placeholder code while developing an application.
You explored the syntax and behavior of each statement, learned how they work with both for loops and while loops, and discovered that the pass statement can also be used in functions, classes, and conditional statements. Through practical examples such as number searching, password verification, ATM menus, login systems, and placeholder functions, you learned when each statement should be used.
Understanding these loop control statements will help you write cleaner, more efficient, and easier-to-maintain Python programs.
break statement immediately terminates the nearest enclosing loop.continue statement skips the current iteration and continues with the next iteration.pass statement performs no action and serves as a placeholder.break and continue can only be used inside loops.pass can be used inside loops, functions, classes, and conditional statements.break statement is useful when no further iterations are required.continue statement is useful for skipping unwanted data without ending the loop.break statement?The break statement immediately terminates the nearest enclosing loop.
continue statement?The continue statement skips the remaining statements in the current iteration and starts the next iteration.
pass statement?The pass statement is a placeholder that performs no action.
break be used outside a loop?No. Using break outside a loop results in a SyntaxError.
continue be used outside a loop?No. The continue statement can only be used inside loops.
pass statement be used?The pass statement can be used inside loops, functions, classes, conditional statements, and exception handling blocks.
continue statement terminate a loop?No. It only skips the current iteration. The loop continues with the next iteration.
pass instead of leaving a block empty?Python does not allow empty code blocks. The pass statement satisfies the syntax requirement while allowing implementation to be added later.
break statement.continue statement.break after successful login.pass statement.pass statement.break statement?continue statement differ from the break statement?pass statement?pass statement be used outside loops?break and continue be used inside nested loops?continue is used incorrectly inside a while loop?break, continue, and pass statements.Create a Python program that searches for a student's roll number using the break, continue, and pass statements.
Your program should:
for loop.break.0 appears in the list, skip it using continue.display_result() using the pass statement for future implementation.Enter Roll Number: 104
Checking Roll Number: 101
Checking Roll Number: 102
Checking Roll Number: 103
Student Found
Search Completed
Congratulations! You have completed the Python control flow section. You now understand how to use conditional statements, loops, and loop control statements to create efficient and flexible programs. These concepts form the foundation of Python programming and are used in almost every real-world application.
In the next lesson, you will begin learning Python Functions. You will discover how to create reusable blocks of code, define functions using the def keyword, pass arguments, return values, understand variable scope, and organize programs into modular, reusable components.