2️⃣ continue

This statement continues the execution flow to the next iteration whenever the specified condition becomes True.

Scenario-1: continue statement using both loops

Lets print only odd numbers till the number 10 by using continue statement inside loop.

💻 Example 1:

1
2
3
4
5
6
7
8
9
# -- continue statement with while loop --

number = 1
while number < 11:
    if number%2 == 0:
        number += 1
        continue
    print(number)
    number += 1

✅ Output:

1
3
5
7
9

💻 Example 2:

1
2
3
4
5
6
# -- continue statement with for loop --

for num in range(11):
    if num%2 == 0:
        continue
    print(num)

✅ Output:

1
3
5
7
9

Scenario-2: Lets consider a list containing names.

condition: Dont print the names which are starting with ‘s’ character.

💻 Example 3:

1
2
3
4
5
6
name_list=['krishna','mahesh','sangam','shesh','malhari','shri']

for name in name_list:
    if name.startswith('s'):
        continue
    print(name)

✅ Output:

krishna
mahesh
malhari