Control flow statements regulate the order in which a program’s code executes. They allow developers to manage sequential execution, decision-making, and repetition, enabling dynamic and efficient programming.
Key takeaways:
Control flow regulates the execution order of code using sequential statements, decision-making (e.g.,
if
,if-else
,nested if
,if-elif-else
), and loops (for
andwhile
).Selection statements like
if
andif-else
allow programs to execute specific code blocks based on conditions, while nested andif-elif-else
statements enable more complex decision-making.Loops like
for
andwhile
enable code blocks to execute repeatedly, making it easier to handle iterative tasks efficiently.
A program’s control flow is the order in which the program’s code executes. The control flow of a Python program is regulated by conditional statements, loops, and function calls.
Python has three types of control structures:
Sequential statements are statements whose execution process happens in a sequence. The problem with sequential statements is that if the logic has broken in any of the lines, the complete source code execution will fail.
## This is a Sequential statementa=20b=10c=a-bprint("Subtraction is : ",c)
In Python, selection statements are also known as Decision control statements or branching statements.
The selection statement allows a program to test several conditions and execute instructions based on which condition is true.
Some decision control statements are as follows:
if
if-else
nested if
if-elif-else
if
It helps us run a particular code only when a certain condition is met or satisfied. A if
only has one condition to check.
In the following code example, we’re checking the if
condition, e.g., if n
modulo 2
equals to 0
then the print
statement will execute.
n = 10if n % 2 == 0:print("n is an even number")
if-else
The if-else
statement evaluates the condition and will execute the body of if
if the test condition is True
, but if the condition is False
, then the body of else
is executed.
In the following code example, the else
body will execute as 5
modulo 2
is not equal to 0
.
n = 5if n % 2 == 0:print("n is even")else:print("n is odd")
if
Nested if
statements are an if
statement inside another if
statement.
In the following code example, the first if
checks if a
is greater than b
. The else
block contains a nested if
that checks if b
is greater than a
. If neither condition is true, it prints that a
and b
are equal. This is a simple use case for nested if
with two variables.
# Assign values to variables a and ba = 10b = 20# Compare the two numbers using nested if statementsif a > b:print("a is greater than b")else:if a < b:print("b is greater than a")else:print("a and b are equal")
if-elif-else
The if-elif-else
statement is used to conditionally execute a statement or a block of statements.
In the code provided, the initial if
statement checks whether 15 is equal to 12. If not, the elif
condition will verify that 15 is greater than 12. If this condition is true, the code block inside elif
will be executed. Otherwise, the code in the else
block will be executed if none of the preceding conditions are met.
x = 15y = 12if x == y:print("Both are Equal")elif x > y:print("x is greater than y")else:print("x is smaller than y")
If you're eager to deepen your understanding of Python and sharpen your problem-solving skills, our “Learn to Code: Become a Software Engineer” path is the perfect learning resource. With this structured path, you’ll go beyond fundamental concepts like generators and dive into advanced topics that will prepare you for a successful career in software engineering.
A repetition statement is used to repeat a group (block) of programming instructions.
In Python, we generally have two loops/repetitive statements:
for
loopwhile
loopfor
loopA for
loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set. We can execute a set of statements once for each item in a list, tuple, or dictionary.
In the following code example, the first example demonstrates how to use a for loop to iterate over a list and access its elements, while the second example shows how to iterate over a range of numbers.
print("1st example")lst = [1, 2, 3]for i in range(len(lst)):print(lst[i], end = " \n")print("2nd example")for j in range(0,5):print(j, end = " \n")
while
loopIn Python, while
loops are used to execute a block of statements repeatedly until a given condition is satisfied. Then, the expression is checked again and, if it is still true, the body is executed again. This continues until the expression becomes false.
The following code iterates from 0 to 4 and prints each value using a while
loop. It prints End
to signify the end of the program after the loop is completed.
m = 5i = 0while i < m:print(i, end = " ")i = i + 1print("End")
Haven’t found what you were looking for? Contact Us