Loops and Control Statements (continue, break and pass) in Python

PythonServer Side ProgrammingProgramming

In this article, we will learn about Loops and Control Statements (continue, break and pass) in Python 3.x. Or earlier.

Python offers to loop constructs i.e. for & while. Unlike other languages, the for loop is not constrained by any conditional before its execution. Here for loop uses range function for its execution and iteration.

Let’s have a look at their implementations −

The conditional while loop

Example

 Live Demo

i = 0
while (i < 4):
   print("Tutorialspoint")
   i=i+1

Output

Tutorialspoint
Tutorialspoint
Tutorialspoint
Tutorialspoint

The un-conditional for loop

Example

 Live Demo

for i in "Tutorialspoint":
   print(i,end=" ")

Output

T u t o r i a l s p o i n t

Example

 Live Demo

for i in range(1,5):
   print(i)

Output

1
2
3
4

Now let’s see the implementation of jump statements −

Continue statement

Example

 Live Demo

for i in 'Tutorialspoint':
   if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
      continue
   else:
      print ('Character :', i)

Output

Character : T
Character : t
Character : r
Character : l
Character : s
Character : p
Character : n
Character : t

Break statement

Example

 Live Demo

for i in 'Tutorialspoint':
   if i == 'a' or i == 'e' or i == 'i' or i == 'o' :
      Break
   else:
      print ('Character :', i)

Output

Character : T
Character : u
Character : t

The bypass statement or empty statement: pass

Example

 Live Demo

for i in 'Tutorialspoint':
   if i=='u' or i=='p':
      pass
   else:
      print ('char :', i)

Output

char : T
char : t
char : o
char : r
char : i
char : a
char : l
char : s
char : o
char : i
char : n
char : t

Conclusion

In this article, we learned about looping constructs, jump statements and bypass statements available in Python 3.x. Or earlier.

raja
Published on 29-Aug-2019 12:16:03
Advertisements