we have seen what are loops?
types of loops?
what is for loop? and range function also what is a while loop?
In a while loop, we are having 2 jumping statements
The break Statement
A break statement terminates the loop in which it is present.
Regardless of the following statements. that is the remaining statements in that particular loop won’t take into account the execution.
Execution resumes at the statement immediately following the body of the terminated statement loop.
In conclusion, A break statement skips the rest of the loop and jumps over to the statement following the loop
a=b=c=0 for i in range(1,11): a=int(input ("Enter number 1 :")) b=int(input ("Enter number 2 :")) if b == 0: print("Division by zero error!") break else: c= a/b print("Quotient",c) print("Program over !")
Enter number 1 :6 Enter number 2 :0 Division by zero error! Program over !
The Contiue Statement
The continue statement forces the next iteration of the loop to take place, skipping any code in between.it’s different from the break statement
The continue statement skips the rest of the loop statements and causes the next iteration of the loop to take place
for a in "word": if a == "r": continue print(a) print("The end")
w o d The end
Here we are continuing the loop if the string is “r” which will not be executing the rest of the code. so, we will get our output with all the letters except r
More operations on loops:
there are few different operations for our loops
- loop with else statement
- nested loops
Loops with Else statement
In python, loops can also have else statements like the same for of else statements
let’s see how to use else for the loops statements
Syntax for for loop else statement
for <variable> in <sequence> : statement1 Statement : else : statement(s)
Syntax for While loop else statement
while <test condition : statement1 statement2 : : else: statement(s)
How the else statement used for loops ??
The else part of the Python loop will execute when the loop terminates normally,
for example, when
the test-condition results will give false for a while loop or in case of for loop has executed the last value in the given sequence not when the break statement terminates the loop.
for a in range (1, 4) : if a % 8 == 0 : break print("Number is", end = ' ') print(a) else: print("Ending loop after printing all Numbers of sequence")
Number is 1 Number is 2 Number is 3 Ending loop after printing all Numbers of sequence
from the above code, the last line of the output is printed because the else part of giving for loop executed when the for loop was terminated
In case we gave %2 instead of %8 we will get the following as output
A loop may contain another loop in its body. This form of a loop is called a nested loop. But in a
nested loop, the inner loop must terminate before the outer loop.
for i in range(1, 6): for j in range (1, i): print("*", end =' ') print()
* * * * * * * * * *
In nested loops, a break statement will terminate the very loop it appears in.
If the break statement is inside the inner loop, then only the inner loop will terminate and the outer loop will continue.
If however, the break statement is in the outer loop, the outer loop will terminate.
[…] for the detailed explanation of break, statements CLICK HERE […]