Python programming: Python – break & continue

0
138
Python programming Python - break & continue
Python programming Python - break & continue

Break: Python – break & continue

Python – break & continue: Let’s see a for loop

for letter in "string":
    print(letter )
print ("The end" )

When the letters are printed one by one. The function of the break is that when we get a value, we get out of the loop. And breaks are used to get out. For example, when we get the n letter in the word string, we get out of the loop. And I’ll write to him like this:

for letter in "string":
    if letter  == "n" :
        break
    print(letter )
print ("The end" )

The above program will output up to the string. Whenever n is found, it exits the loop. And execute any code after the loop. After the loop here, our line is print (“The end”).

Continue: Python – break & continue

If a certain condition is true, then the code block inside the loop is used to not execute. Let’s replace the break of the previous program and print it with continue.

for letter in "string":
    if letter  == "n" :
        continue
    print(letter )
print ("The end" )

What it will do is, when the letter == “n” is true, then the next code will no longer execute. Goes back to the loop again. It will print the remaining letters except for n. When n is received, it starts executing from the beginning of the loop again without printing. And finally, print g. Now if we test with i instead of n:

for letter in "string":
    if letter  == "i" :
        continue
    print(letter )
print ("The end" )

Now we can see that all the letters except I have been printed. Let’s look at another example:

s = "This is a string"
for letter in s:
    print(letter, end='')

This program will print the letters of the string, the difference with the previous program is that it used to print on the new line, now it will print on the same line. Output this is a string

Now we are giving a condition that when s gets, it will continue. That means s won’t print. For him:

s = "This is a string"
for letter in s:
    if letter == "s": continue
    print(letter, end='')

Now let’s see the output This is a string, Wheres was, it skipped. In the above program, if we write a break instead of continue, then:

s = "This is a string"
for letter in s:
    if letter == "s": break
    print(letter, end='')

Now I get the output only after getting ” This is a string ” s out of the loop.

Let’s look at another simple example, write a program to print numbers from one to 10, where we don’t want to print 5. We will continue when we get 5.

count = 0
while count <=10 :
    count += 1
    if count == 5: continue
    print (count)

Now we want to break when we get 5. Then:

count = 0
while count <=10 :
    count += 1
    if count == 5: break
    print (count)

The above program will now print from 1 to 4. Because when he got 5, he got out of a while.

Note: More Info (Tech24InfoBD) (Python – break & continue)

LEAVE A REPLY

Please enter your comment!
Please enter your name here