0% found this document useful (0 votes)
3 views12 pages

Python Loop Control Statements Explained

Uploaded by

Rudraaksh Sethi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Python Loop Control Statements Explained

Uploaded by

Rudraaksh Sethi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LOOPS, CONTINUE,

BREAK STATEMENT
Session 9
Break, Pass, Continue statement
◦ Using loops in Python automates and repeats the tasks in an efficient manner.
◦ Sometimes, there may arise a condition where you want to exit the loop
completely, skip an iteration or ignore that condition. These can be done by loop
control statements.
◦ Loop control statements change execution from its normal sequence.

Python supports the following control statements.


◦ Continue statement
◦ Break statement
◦ Pass statement
Break Statement
• Break statement in Python is used to
bring the control out of the loop
when some external condition is
triggered.
• Break statement is put inside the
loop body (generally after if
condition).
Example
s=‘abcdefghijknmnms’
Output
for letter in s: ------------
a
print(letter) b
# break the loop as soon it sees 'e’ or 's' c
d
if letter == 'e' or letter == 's': e
Out of for loop
break

print("Out of for loop")


Example –Practical use of break statement
◦ #Program for prime number

N= eval(input(“enter a number”))
flag=0
For i in range(2, N+1) :
if N % i== 0:
flag=1
break #if we find a factor we can break out as it gets confirmed no is not prime
if flag==0:
print(“ Number is prime”)
else:
print(“ Nota prime number”)
Continue statement
Continue is also a loop control statement just like the break statement.
continue statement is opposite to that of break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
The continue statement forces the loop to continue or execute the next
iteration.
When the continue statement is executed in the loop, the code inside the loop
following the continue statement will be skipped and the next iteration of the
loop will begin.
Example
for i in range(1, 11):
# If i is equals to 6, continue to next iteration Output:
if i == 6:
1 2 3 4 5 7 8 9 10
continue
else:
print(i, end = " ") # otherwise print the value of i
Pass Statement
The pass statement is a null statement.
But the difference between pass and comment is that comment is ignored by
the interpreter whereas pass is not ignored.

The pass statement is generally used as a placeholder i.e. when the user does
not know what code to write. User simply places pass at that line.
Sometimes, pass is used when the user doesn’t want any code to execute.
So user simply places pass there as empty code is not allowed in loops, function
definitions, or in if statements. Using pass statement user avoids this error.
Example
a = 10
b = 20

if(a<b):
pass
else:
print("b<a")
Program to find factorial using while
loop
N= eval(input(“ Enter a number”))
Factorial = 1
i=1
While i<=N :
factorial = factorial * i
i= i+1
Print(“factorial –”, factorial)
GCD or HCF of 2 numbers
x=eval(input(“ Enter first integer”))
y=eval(input(“ Enter second integer”))
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
print(“ hcf is “,hcf)
row = int(input('Enter how many rows‘))
1
# Generating pattern
212 for i in range(1,row+1):
32123
# for space printing
4321234
for j in range(1, row+1-i):
print(' ', end=‘ ')

# for decreasing pattern


for k in range(i,0,-1):
print(k, end=‘’)

# for increasing pattern


for p in range(2,i+1):
print(p , end=‘ ')

# Moving to next line


print()

Common questions

Powered by AI

Loop control structures in Python, such as break, continue, and pass, automate tasks by allowing execution flow to be dynamically adjusted based on certain runtime conditions. They enable complex decision-making processes within loops by simulating conditional operations at various control points, thereby optimizing task execution and minimizing repetitive code blocks .

The Greatest Common Divisor (GCD) of two numbers can be calculated in Python using a simple loop that iterates through numbers up to the smaller of the two numbers. During each iteration, the loop checks if both numbers are divisible without remainder, updating the GCD if they are. The GCD is important in mathematics for simplifying fractions and understanding number relationships .

The "pass" statement in Python acts as a placeholder in code, allowing the program to run without executing any operations when a particular block is syntactically necessary. Unlike a comment, which is ignored by the interpreter, a "pass" is executed as a no-operation placeholder. For example, in an if-else construct, "pass" allows the code block for "if" to be empty without causing an error .

A loop facilitates calculating the factorial of a number by multiplying the number by every integer below it until reaching 1. The factorial is computed within a loop that runs from 1 to the number itself, updating a product variable in each iteration. This repeated multiplication in a loop structure effectively calculates the factorial .

Null statements like "pass" are significant for structuring code where syntactical placeholders are required and yet no operation is intended. Used primarily during development and iterative design phases, they maintain functional placeholders in loops or function definitions, allowing logical code expansion without syntactic errors, thus aiding modular and scalable code architecture .

The "continue" statement, when used in a loop iterating over numbers 1 to 10, causes the specific loop iteration to be skipped without terminating the entire loop. For example, inserting "continue" when "i" equals 6 results in numbers 1 to 5 being printed normally, skips 6, and then resumes with numbers 7 to 10, achieving selective bypassing of iteration .

Control structures in Python like break, continue, and pass enhance error handling by providing explicit flow-control mechanisms that can bypass or terminate sequences when encountering erroneous or special conditions. For example, break might exit a loop upon input validation error, while continue could skip processing invalid data entries in a batch operation, catering to robust user interaction frameworks .

The "break" statement in Python is used to exit a loop prematurely when a certain condition is met. It is usually placed inside the loop body and often follows an "if" statement. For example, in a practical scenario like checking for a prime number, once a divisibility factor is found, the "break" statement terminates the loop since the number can no longer be prime .

The "break" statement terminates the entire loop when it is executed, while the "continue" statement skips the current iteration and proceeds to the next one. For instance, using "break" while iterating through a string can stop the loop when a specific character is found, terminating further execution . On the other hand, using "continue" in a loop to iterate over numbers 1 to 10 will skip the number 6, continuing with subsequent numbers without termination .

Hierarchical patterns can be generated in Python using nested loops. One loop manages the levels or rows, while the inner loops construct the pattern per row. An example is a pyramid pattern where spaces and number sequences are managed by nested loops for proper alignment and symmetry, incrementing and decrementing through variable manipulations per iteration step .

You might also like