CodeNewbie Community 🌱

Cover image for Python Loops: Using the Else Clause and Jump Statements
Aswin Barath
Aswin Barath

Posted on • Originally published at hackernoon.com

Python Loops: Using the Else Clause and Jump Statements

Have you ever felt like you are doing certain tasks again and again in life?
Well, I'm no expert in helping with your life!
Although, if you would ever face the same situation in a programming language I can help you with that.

In programming, we can repeat certain tasks using loops.
Let's go through them one by one.

for loop

  • for loop in python repeats over each item of a given sequence (a list or a string), in the order that they appear in the sequence.

Example:
Alt Text

Output:

I want to watch a movie directed by James Cameron
I want to watch a movie directed by Christopher Nolan
I want to watch a movie directed by Steven Spielberg
Enter fullscreen mode Exit fullscreen mode
  • But, let's say you are a programmer who needs to iterate over a sequence of numbers, given the limit you want. Well, then you can make use of the built-in function range() which generates arithmetic progressions: Alt Text

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

while loop

  • while loop in python repeats any given set of code until the give condition returns true.

Example:
Alt Text

Output:

0
1
1
2
3
5
8
Enter fullscreen mode Exit fullscreen mode

Alt Text

Basic jump statements in python

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

Alt Text

Else Clauses on Loops

Did you know that loop statements may have an else clause?
Well, it turns out that they can be used!

  • else clause will be executed when the loop completes through all of the iterations with for loop and when the condition becomes false with while loop.

Example:
Alt Text

Output:

Searching...
Searching...
Searching...
Searching...
Searching...
Searching...
Searching...
Searching...
Treasure not found :(
Enter fullscreen mode Exit fullscreen mode
  • But else clause will not be executed when the loop is terminated by a break statement.

Example:
Alt Text

Output:

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

Code along and have fun.

Top comments (0)