-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Python provides three loop control statements to change the flow of loops:
break
– Exit the loop prematurely
continue
– Skip the current iteration
pass
– Do nothing, acts as a placeholder
These can be used inside for
or while
loops.
Used to stop the loop immediately, even if the condition is still true.
for i in range(10):
if i == 5:
break
print(i)
Output:0 1 2 3 4
Used to skip the current iteration and move to the next.
for i in range(5):
if i == 2:
continue
print(i)
Output:0 1 3 4
Used when a statement is required syntactically, but you don’t want to execute any code.
for i in range(3):
pass
pass
is often used as a placeholder in loops, classes, functions, etc.
i = 1
while i <= 5:
if i == 3:
break
print(i)
i += 1
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
numbers = [1, 2, 3, 4]
for n in numbers:
if n == 3:
pass
print(n)
Use break
, continue
, and pass
in both for
and while
loops to control flow based on conditions.
Q1. Write a Python program to print numbers from 1 to 10, but stop the loop when the number is 7 using break
.
Q2. Write a Python program to print all numbers from 1 to 10, but skip the number 5 using continue
.
Q3. Write a Python program using a while
loop and break the loop when the number equals 4.
Q4. Write a Python program to use continue
to skip even numbers from 1 to 10 and print only odd numbers.
Q5. Write a Python program to create a loop that prints numbers from 1 to 5 and use pass
inside the loop.
Q6. Write a Python program to print only the vowels from the string "PythonLoopControl"
using a loop.
Q7. Write a Python program to loop through numbers from 1 to 20 and break at the first number divisible by 9.
Q8. Write a Python program to loop through numbers from 1 to 10 and skip numbers divisible by 3 using continue
.
Q9. Write a Python program to use pass
to handle a missing condition in a loop (e.g., as a placeholder for future logic).
Q10. Write a Python program to build a login system that keeps asking for a password and breaks the loop when the correct password is entered.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms