/* */ Click to Join Live Class with Shankar sir Call 9798158723

break statement in python

the break statement is widely used inside a for loop or while loop to come out of the loop. when break is executed the python interpreter jumps out of the loop to process the next statement in the program.


while condition:

    if  condition:

Note:-four space are python indentation

Q.) Program to demonstrate the break statement.
 while i<=10:
   if i==5
 1 2 3 4 

Program Explanation
  • Step 1:-
    the code is meant to print 1 to 10 using a while loop but it will actually print only number from 1 to 4 as soon as i becomes equal to 5 the break statement is executed and the control jumps to the statement following the while loop hence the break statement is used to exit a loop from any point within its body
    Hear We declared variables called i. The initialization statement is executed. i.e i=1
  • Step 2:-
    Then, the test expression is evaluated. i.e i is less than or equals to 10 or not (i <=10 ) hear condition is true so statements inside the body of the while loop are executed, and it will print the value of i i.e 1
  • Step 3:-
    After successful execution of statements inside the body of the loop, the if condition is check i.e i is equals to 5 or not if condition is false than goto step 4 else exit from loop
  • Step 4:-
    After successful execution of statements inside the body of the loop, the update expression (i.e i++) means it incremented by 1 now the value of i is 2
  • Step 5:-
    Again the test expression is evaluated. and repeat step 3 till the condition is false of if condition
  • Step 6:-
    when the condition is false means the value of i will be 5 then while loop gets terminated with break and the control comes out of the loop.