DEV Community

Cover image for Control Flow In Python - Part 2
Introschool
Introschool

Posted on • Updated on

Control Flow In Python - Part 2

Subscribe to our Youtube Channel To Learn Free Python Course and More

The range() Function
The range function is used to get sequence of numbers. The syntax of range() function is range(start, stop, step). Here you can give start parameter, stop parameter and you can also give step parameter, by default value is 1 for step parameter.

In the range function, start and step parameter are optional. The stop parameter is the upper limit, It means that function generates numbers up to this number, it range doesn’t include this number.

You can use range function to print numbers in a particular range. You can also use range function to iterate over the list, tuple, string data structure.

# range function
for i in range(5):
    print(i)
'''
# Output:
0
1
2
3
4
'''

# print even numbers from 0 to 10
for i in range(0,10,2):
    print(i, end=', ')

'''
# Output:
0, 2, 4, 6, 8,
'''

# iteration over list.
lst = ['hello', 2, 4.3, 0]

for i in range(len(lst)):
    print(lst[i])

'''
# Output:
hello
2
4.3
0
'''
Enter fullscreen mode Exit fullscreen mode

While Loop
While loop executes statements and expression until the condition is true. See the while loop syntax below.

# While Loop


while <condition>:
    <statements>

# Example
i = 1
while i > 0:
  print(i)
  i -= 1

'''
Output:
10
9
8
7
6
5
4
3
2
1
'''
Enter fullscreen mode Exit fullscreen mode

You can see in the above example that while loop prints the variable i until the condition i > 0 was true, The moment i become 0, the loop gets terminated.

The while loop can continue for infinite times if you don’t terminate the loop. So check the condition and statement that you are putting in while loop. For example, if we wouldn’t decrement i, then this loop would have been an infinite loop because every time the condition would be satisfied.

The while loop requires already defined variables unlike for loop. If you didn’t define the variable that you are using in the loop, the interpreter will give error.

Pass, Break and Continue Statement
Now you have an idea that how the flow of the loops works? You can alter the flow by using these statements. They affect the flow of the loop in a different-different way.

Pass Statement
Pass statement doesn’t affect the flow, it just works as a placeholder. Suppose you write a condition, if the condition is satisfied it follows the body of . If you didn’t write any logic inside the body of , the python interpreter will give some error, to avoid that error you can write pass statement for now. By using the pass statement, the interpreter will run the loop as it is.

for i in range(10):
    if  i == 3:
        pass
    print(f'num:{i}')

print('out of the loop')

# Output
num:0
num:1
num:2
num:3
num:4
num:5
num:6
num:7
num:8
num:9

out of the loop
Enter fullscreen mode Exit fullscreen mode

Break Statement
Break statement, as you can get the idea from the name that it is used to break the loop. It generally used after if conditional statement. Once Python interpreter comes across break statement, it terminates the loop. See the example

for i in range(10):
    if  i == 3:
        break
    print(f'num:{i}')

print('out of the loop')

# Output
num:0
num:1
num:2

out of the loop
Enter fullscreen mode Exit fullscreen mode

You can see that loop gets terminated once it encounters i is equal to 3.

Continue Statement
Continue statement also affects the flow of the loop. It is generally used after conditional statement. If you want to skip over the part of the loop then you can use this statement. See the code below.

for i in range(10):
    if  i == 3:
        continue
    print(f'num:{i}')

print('out of the loop')

# Output:
num:0
num:1
num:2
num:4
num:5
num:6
num:7
num:8
num:9

out of the loop

Enter fullscreen mode Exit fullscreen mode

Here you can see that interpreter skips the part where i is equal to 3. So this is how you can use continue statement.

Top comments (0)