Python Control Flow Statements
This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look into different control statement structures like Sequential, Repetition, and Selection with examples of each structure written in Python programming language along with self-explanatory comments in the code that we will be writing.
Control Flow Statements
Programming Languages Control flow is the order in which the Program or the code we have written executes in a particular order. The Control in Programming languages differs in many ways; for instance, here in our Python Programming Language, the Control Flow is regulated by Function calls that the code does, loops like for loop, while loop, do-while loop, nested loops and also by Conditional statements.
Importance of Control Statements
The Control Flow Statements will let you give the power to have control over the flow of execution of the code that we write. In Python Programming Language or any programming language in general, the flow of an implementation of the code can be controlled by placing the looping, branching, decision making and adding some additional blocks to the code we write. It is essential to have the control flow statements because the code we write in a particular file, the flow of execution generally flows top to bottom. Still, with the help of control flow statements, we can now control certain decisions, perform tasks that require frequent repetition or jump from a specific part of a different section if needed.
The three most important types of Control Structures that we have in Python Programming Language are
- Sequential – it is the default mode.
- Repetition is generally used for looping, which means repeating a particular condition multiple times by the same piece of code.
- Selection – Its primary purpose is branching out code and making decisions.
Sequential Control Flow Statements
The Sequential Control Flow Statements are the by-default flow of execution statements. The flow of execution generally happens from top to bottom, and the problem with them is if the logic that we have written in the code breaks, then the complete source code file execution might break, which may cause a throwing error.
Code
## This is a Sequential statement example
a=20233
b=1033
c=a-b
print("Subtraction is of a-b is : ",c)
Output
Subtraction is of a-b is: 19200
Selection/Decision Control Flow Statements
In Python Programming Language, the Selection statements are also known as the branching or decision-making statements allowing us to make the code that we write go through the pass a variety of checks in between and will only execute if the condition is true
Selection Control Statements are:
If-else
Code
variety of checks in between Code
nn = 500
if nn % 2 == 0:
print("nn is even")
else:
print("nn is odd")
Output
nn is even
Simple if
Code
nn = 100
if nn % 2 == 0:
print("this is an even number")
Output
this is an even number
If-elif-else
Code
x = 1500
y = 122212
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
Output
x is smaller than y
Nested if
Code
x = 1500
y = 122212
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
Output
c is bigger here
Repetition Control Flow Statements
As we have already discussed in the introduction, the Repetition Control Flow Statements are used to repeat a specific code multiple times or stop writing a particular code more times; we prefer the repetition statement where the loop passes through the statement and does the work for us.
Types of repetitive statements
For loop
Code
list_example = [1, 2, 3, 4, 5]
for i in range(len(list_example)):
print(list_example[i], end = " ")
for j in range(0,10):
print(j, end = " ")
Output
1 2 3 4 5 0 1 2 3 4 5 6 7 8 9
While loop
Code
m = 105
i = 100
while i < m:
print(i, end = " ")
i = i + 1
print("Ending now")
Output
100 101 102 103 104 Ending now