In this post, you will learn python break and continue statements with the help of flowcharts and examples.

So let us start with the very basics.

What is a Break and Continue statement in Python

break and continue are known as jump statement because it is used to change the regular flow of the program or loop when a particular condition is met.

Let’s see both statements one by one in detail.

Break statement

The break statement is used to terminate the execution of the current loop when a particular condition is met.

Syntax: break

Flowchart for Break statement

From the Flowchart, we understand it if a particular condition is met then the break statement terminates the execution of the current loop.

let’s see break statement with the help of an example

Example

Example-1: break statement with for-loop

# Demo of break statement with for-loop
for i in range(1,11):
    if i==6:
        break
    print(i)

print('end of the for-loop')

Output:

1
2
3
4
5
end of the for-loop

From the output, we understood that if i == 6 then the break statement terminates the loop and jumps to the next statement that is print('end of the for-loop').

Example-2: break statement with while-loop

# Demo of break statement with while-loop
i = 0
while i <= 10:
    if i == 5:
        break
    print(i)
    i+=1

print('End of the while loop')

Output:

0
1
2
3
4
End of the while loop

Continue statement

The continue statement help in skipping a particular iteration of the current loop when a particular condition is met.

Syntax: continue

Flowchart for Continue statement

From the Flowchart, we understand it if a particular condition is met then the continue statement skips a particular iteration of the current loop.

let’s see continue statement with the help of an example

Example

Example-1: continue statement with for-loop

# Demo of continue statement with for-loop
for i in range(1,6):
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

From the output, we understood that if i == 3 then the continue statement skips a particular iteration of the current loop (that is 3).

Example-2: continue statement with while-loop

i = 0
while i<=4:
    i+=1
    if i == 2:
        continue
    print(i)

Output

1
3
4
5

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment