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

continue statement in python


the continue statement is used in a loop to go back to the beginning of the loop.it means, when continue is executed the next repetition will start. when continue executed the subsequent statements in the loop are not executed. the continue statement can not be used without an enclosing for or a while loop.

Syntax:-


while condition:

    if  condition:
       continue;
  statement


Note:-four space are python indentation


Q.) Program to demonstrate the continue statement.
 
 i=1;
 while i<=10:
   print(i);
   if i==2
      continue		      
   i=i+1   
 
 
Output
 1  3 4 5 6 7 8 9 10

Program Explanation
  • in the program the while loop repeats for 10 times from 1 to 10 every time i value is display by the loop when i value is 5 continue is executed that mekes the python interpreter to go back to the beginning of the loop thus the next statements in the loop are not executed as a result the numbers 5 are not display
  • Step 1:-
    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 2:-
    After successful execution of statements inside the body of the loop, the if condition is check i.e i is equals to 2 or not if condition is false than goto step 3 else step 4
  • Step 3:-
    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 4:-
    Again the test expression is evaluated. and repeat step 3 till the condition is false of if condition
  • Step 6:-
    when i value is 2 continue is executed that mekes the python interpreter to go back to the beginning of the loop thus the next statements in the loop are not executed as a result the numbers 2 are not display.

«  PREVIOUS PAGE NEXT PAGE »