Control statements in python


Control statements in python



 Python supports different loops to repeat the same task or to execute the same code several times but some time user want to skip some part of loop or come out from loop at that time ,user can use the Control statements .Control statements can  only appear in Loop  .

Python provide three main  control statements (Keywords)       

1.Continue 

2.Break 

3.Pass


1.Continue :

When the compiler encounters a continue statement, then the remaining part of the program will not execute .And the control is unconditionally transferred  to the loop-continuation portion of the nearest enclosing loop. Its syntax is very simple , just type keyword continue as shown below .

syntax :  continue 

Example: Program to print even number (to illustrate the concept of continue keyword )

for i in range(1,11):
if (i%2!=0):
continue
print(i , end="") (
use of (end ="" ) in print statement to show the outputs
                        in a single line )
Output :
2 4 6 8 10

Note that the program is to print all even values between 1 to 11 ,and when the condition for odd numbers becomes true continue statement is encountered . so the rest of the statement of for loop are skipped .

Control statements in python
 
 It means continue statement is somewhat opposite than break and force the compiler to go for the
 next iteration . continue statement is usually used to restart a statement sequence when an error 
occurs .

NOTE 

The continue statement is used to stop the current iteration of the loop and continues with the next one .



2.Break :

The break statement is used to terminate the execution of the nearest enclosing loop in which it is present. The break statement is widely used in for and while loop .when compiler encounters a break statement the control pass to the statement that follows the loop in which the break statement appears. Its syntax is very easy ..

syntax :  break 
   
Example : To illustrate the break statement 
i=1
while i<=8
     print(i,end="")
     if i==6:
          break
     i=i+1
print("\n Done")

Output : 1,2,3,4,5,6

Note that the program is to print the number from 1 to 8 but because of break statement co
Output is only up to 6.So we can conclude that the break statement is used to exit a loop from any point within its body. When the break statement is encountered  inside a loop ,the loop is immediately terminated and program control is passed to the next statement following to loop.

Control statements in python 
transfer control out of for the loop 

NOTE 

The Use of continue and break statement out of while and for loop causes an error .

3 Pass :

The pass statement is used when a statement is required syntactically but no commands or code has to be executed . It simply means null operation or No operation(NOP) statement .
Syntax is very easy 

SYNTAX : pass 


Example : to illustrate the statement " PASS "

 for a in range (1,10):
     if(a%2==0)
         pass
   else:
      print(a)


//Output
1,3,5,7,9 odd numbers 

In the above example , code is to print only odd number and we pass the if loop it means no statements in the  if loop .