Continue Statement in Python
Continue Statement in Python
Continue statement in Python is used to skip a piece of code for the current iteration if the condition it is set for becomes true. When a continue statement is executed, rest of the loop body is skipped for the current iteration, and control goes to the next iteration. When a continue statement is used inside a nested loop, it will skip the innermost loop’s code when it is executed.
Continue with While Loop
# Python program to exemplify continue statement with while loop num = 0 while (num < 5): num += 1 if(num == 4): print('this iteration is skipped.') continue print(num)
In the program provided above, a variable named “num” is created; a while loop is then used to print its value after incrementing it as it is initialized to 0. When the value of the variable “num” becomes 4, condition related to continue gets true, and continue statement is executed skipping that iteration. The loop continues afterward normally, and exhausts when the loop condition becomes false.
Output:
1 2 3 this iteration is skipped. 5
Continue Statement with For Loop
# Python program to exemplify continue statement with for loop colors = ['red', 'blue', 'pink', 'yellow', 'black', 'orange'] for color in colors: if(color == 'yellow'): print('this iteration is skipped.') continue print(color)
In the program provided above, a list named “colors” is created. A for loop is then used to iterate over its elements. During iteration, name of elements is displayed. If the element is “yellow”, then that iteration is skipped and loop continues normally afterward.
Output:
red blue pink this iteration is skipped. black orange
Continue Statement with Nested Loop
# Python program to exemplify continue statement with nested loop # nested loop without continue statement numbers = [1, 2, 3] multipliers = [10, 100, 1000] print("nested loop without continue statement") for number in numbers: for multiplier in multipliers: print (number * multiplier)
In the program provided above, a nested loop is used without a continue statement. Outer loop is used for the list “numbers” and inner loop is used for the list “multipliers”. For every number in numbers, inner loop is executed and results in multiplication of number with every element of the list multipliers.
Output:
nested loop without continue statement 10 100 1000 20 200 2000 30 300 3000
# Python program to exemplify continue statement with nested loop # nested loop with continue statement numbers = [1, 2, 3] multipliers = [10, 100, 1000] print("nested loop with continue statement") for number in numbers: for multiplier in multipliers: if (multiplier == 100): continue print (number * multiplier)
In the program provided above, a nested loop is used with a continue statement. Outer loop is used for the list “numbers” and inner loop is used for the list “multipliers”. For every number in numbers, inner loop is executed and results in multiplication of number with every element of the list multipliers. If value of multiplier is 100, then that iteration of the inner loop is skipped.
Output:
nested loop with continue statement 10 1000 20 2000 30 3000