Latest posts

10/recent/ticker-posts

'break' and 'continue' statements in python | break control statement in python | break control statement in python

Python programming is a highly used programming language in the whole world. It provides unique features for programmers to develop high-quality applications and system programs, such as Conditional, Looping, and control statements.

In this post, we will focus on two such control structures, namely 'break' and 'continue' statements.

Before we delve into the specifics of 'break' and 'continue', let's first understand what control structures are. Control structures are blocks of code that allow us to control the flow of a program. They are used to decide which code block to execute, how many times to execute a code block, and when to stop executing a code block.

The 'if' statement is an example of a control structure that allows us to execute certain code blocks based on a condition. Similarly, a 'for' loop allows us to execute a block of code multiple times based on a given range, while a 'while' loop allows us to execute a block of code repeatedly as long as a certain condition is true.

Now, let's look at how the 'break' and 'continue' statements can be used to modify the flow of a program in Python.

Break Statement :

The 'break' statement is used to prematurely exit a loop, regardless of the loop's condition. In other words, if a 'break' statement is executed within a loop, the loop will immediately terminate, and the program will continue executing from the next line after the loop.

It means it simply works as a bypass in a program, when any given condition becomes true, then it will bypass the block of code, where the 'break' statement is used.

Syntax :
looping statement:
    if(condition):
        break
    
    # code to be executed

Example :
for i in range(1,10):
    print(i)
    if i == 6:
        break
      
print("\nBreak statement used !")


So here is an example of a 'break' statement, where we have used a for loop to print the number from 1 to 10. In the loop block, we have used an 'if' statement to implement our 'break' statement with a condition (if the value of 'i' becomes 6, then it will terminate the loop). After that, we printed the value of i.
Because of this implementation, it will print the output like,
1
2
3
4
5
6

Break statement used !



Continue statement :

The 'continue' statement is used to skip over a particular iteration of a loop and move on to the next iteration. In other words, if a 'continue' statement is executed within a loop, the loop will skip over the current iteration and move on to the next iteration.

It means when the given condition where the continue statement is implemented becomes true, it will skip this iteration of the loop and moves forward to the next iteration.

Syntax :
looping statement:
    conditional statement:
        continue

    # Code to be executed

Example :
for i in range(1,21):
    if i % 2 != 0:
        continue
    
    print(i)


In this example, we again used a for loop to print the table of two. for that, we have used an 'if' statement, and in the 'if' statement we have passed a condition to implement a 'continue' statement. (when the value of 'i' is not perfectly divisible by 2, then it will skip this iteration and moves to the next iteration).
Because of this implementation, it prints the output like, 
2
4
6
8
10
12
14
16
18
20

So in this post, we have seen two control structures, which will be usable in looping statements.

We hope it will help you to learn more Python.
Thank you!




Also Visit


Post a Comment

0 Comments