Break and continue statement

To change the direction or regular operation of a loop—which could be either a “for loop” or “while loop”—break and continue statements are employed. Break denotes an interruption, and continue denotes restarting after one, illustrating how these two functions are completely at odds with one another. As a result, both of these keywords are largely specified together, much like “if and else” or “try and except,” although unlike those expressions, they serve completely different purposes. Unlike “except,” which can only be used if there is a “try” or “otherwise” condition and if there isn’t a “if” statement present, they are rarely even used together. However, in the case of “break and continue,” they do not have any similar relationships. Despite not being primarily used together, they may be defined jointly.

When used, the break statement ends or exits the loop it is contained in, changing how loops normally behave. The code inserted following the body of the loop is the next block of code the compiler examines. Break only has the word “break” as its syntax. A break statement just leaves the block of code that was put after it inside the loop, making it easier to understand than a continue statement. The statement added after the loop takes over as the program’s new point of control.

Example Of Break Statement

i=0;

while(True):

    print(f”The value of i is : {i}”)

    i=i+1

    if(i>10):

        print(“Breaking the loop. “)

        break;

Output:

The value of i is : 0

The value of i is : 1

The value of i is : 2

The value of i is : 3

The value of i is : 4

The value of i is : 5

The value of i is : 6

The value of i is : 7

The value of i is : 8

The value of i is : 9

The value of i is : 10

Breaking the loop.

The continue statement similarly modifies a normally functioning loop’s execution, but unlike the break statement, it directs the compiler to the beginning of the code that has previously been run but is still inside the loop boundary. Although it is important to note that the continue statement only functions for one iteration, all code written after it is skipped. The continue statement will entirely depend on the condition of the decision statement, unless it is written using decision statements like if, else, etc. in certain cases. Like a break statement, its syntax is simple and straightforward and just requires the usage of the verb “continue.”

Example for continue statement:

i=0;

while(True):

    i=i+1

    if(i==5):

        continue

    if(i>10):

        break

    print(f”The value of i is : {i}”)

Output:

The value of i is : 1

The value of i is : 2

The value of i is : 3

The value of i is : 4

The value of i is : 6

The value of i is : 7

The value of i is : 8

The value of i is : 9

The value of i is : 10

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in