DEV Community

loading...
Cover image for Basic jump statements in python

Basic jump statements in python

aswin2001barath profile image Aswin Barath ・1 min read

Jump statements can be used to modify the behaviour of conditional and iterative statements.

break and continue statements fall under the jump statements category.

break statement

  • The break statement, borrowed from C programming language, breaks out of the current loop when encountered.
  • So, any further iterations will not be executed as the break statement changed the flow of the program by coming out of the loop.

Example:
Alt Text

Output:

Searching...
Searching...
Searching...
Searching...
Found the treasure
Enter fullscreen mode Exit fullscreen mode

continue statement

  • The continue statement, borrowed from C programming language, continues with the next iteration of the loop when encountered.
  • So, any code following the continue statement in a loop will be skipped and the loop will continue the next iteration.

Example:
Alt Text

Output:

Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
Enter fullscreen mode Exit fullscreen mode

Code along and have fun.

Discussion (0)

pic
Editor guide