Continue Statements
In Python continue statement is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration immediately. It is particularly useful when certain conditions need to be bypassed without breaking the entire loop.
Below given are some of the python programs using Continue statements:
for num in range(1, 11):
if num % 2 == 0:
continue # Skip the rest of the loop for even numbers
print(num)
for i in "Python Programming":
# skipping the character equal to 'o'
if i == 'o':
continue
# printing the characters
print(i, end='')
numbers = [10, -3, 5, -8, 7]
# iterating through the elements of the list
for i in numbers:
# skipping a number less than 0
if i < 0:
continue
# printing the numbers
print(i)
if num % 2 == 0:
continue # Skip the rest of the loop for even numbers
print(num)
-----------------------------------------------------------------------------------------------------------------------------
# skipping the character equal to 'o'
if i == 'o':
continue
# printing the characters
print(i, end='')
-----------------------------------------------------------------------------------------------------------------------------
# iterating through the elements of the list
for i in numbers:
# skipping a number less than 0
if i < 0:
continue
# printing the numbers
print(i)
-----------------------------------------------------------------------------------------------------------------------------
words = ["apple", "", "banana", "cherry", ""]
# using for-loop to iterate through the words in the list
for word in words:
# skipping the empty string from the list
if word == "":
continue
# printing the words from the list
print(word)
# using for-loop to iterate through the words in the list
for word in words:
# skipping the empty string from the list
if word == "":
continue
# printing the words from the list
print(word)
---------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment
Please comment