While Loop Continue and Break in Python

In this class, we discuss while loop continue and break in python.

For Complete YouTube Video: Click Here

While Loop in Python

Before going into the concept of a loop, we should have some basic understanding of conditional statements. Click here.

Let’s take some examples and understand while loop.

If someone asked you to display the numbers from one to five.

Up to the knowledge which we gained from our previous classes. We can code like below.

print(1)

print(2)

print(3)

print(4)

print(5)

But this is not the correct method to display numbers from 0ne to hundred.

In the above situation, we are using the same code repeatedly.

Whenever we have a situation to repeat the code with different inputs, we use the concept of a loop.

Example:

While Lop Continue and Break in Python1
Program1

From the above program.

The syntax of while statement. In comparison, keywords followed with the condition. The same way we used if statements.

If the condition is True, execute the block of code that belongs to the if statement.

The same way while the condition is True. Execute the block of code that belongs to the while. And recheck the condition until the condition is False.

The above program checks the condition display i value and increment the i value.

This loop executes five times, and each time displays the value of i.

Continue

Let’s take an example and understand the continue statement.

Someone asked you to display the numbers from ten to one. But don’t display the number five.

In this situation where we want to skip a loop iteration, we use the continue statement.

The below program helps you to understand the continue statement.

While Loop Continue and Break in Python2
Program2

From the above program.

We write a if statement t0 check i ==5.

if i==5 we should skip that loop and do not display the i value.

So inside the if statement, we are using the continue statement.

The continue statement will stop executing the remaining code present in the loop body.

It will continue with the next loop iteration.

Break

Break statement used to discontinue the loop whenever required.

Whenever a break statement is encountered. Python stops executing the loop.

Come out of the loop and continue executing the remaining statements after the loop.

Example:

While Loop Continue and Break in Python3
Program3

Stop executing the loop if i value is five.

The above program shows a break statement in the if conditional statement.

It works like that. If the i value is 5.

the program executes a break statement.

It comes out of the while loop and continues executing the remaining code after the while loop.

The above code displays the output.

10

9

8

7

6

Thank you

While with else

We can use else in a while loop.

If we write else block for a while loop statement.

The example program is shown below.

While Loop Continue and Break in Python4
Program 4

Whenever the while condition is False, it goes into else and executes the block of code in the else block.

Else block with break statement in while

The else block will not execute if the loop break statement executes.

Example:

While Loop Continue and Break in Python5
Program 5

The output of the above program.

10

9

8

7

6

Thank you.

It is not executing else block.