Conditional Statements in Python | Control Flow
In this tutorial, we will learn about conditional statements or decision-making statements in Python with the help of general flowchart diagram.
A simple program written in Python consists of a set of statements that contain expressions. An expression is a statement (or logical line) that comprises an operand and operator.
When we execute a Python program, at a time, only one statement executes by the Python interpreter. We call these statements as sequential statements.
Sequential statements are those statements that execute from top to bottom one by one (sequentially). The flow of execution takes place from top to bottom in the same order in which they appear in the program.
However, if we want to change the flow of execution of a program, we can use control flow statements in Python.
Control flow statements in Python are those statements that change the flow of execution in a program according to requirements and needs of the user. It simply shows the program’s control flow.
The control flow is an order in which the program’s code executes. It provides better control to the programmer on the flow of execution in any program. These statements execute randomly and repeatedly within a program.
Control statements help to write better and complex programs in the Python programming language.
For example, a certain situation generally comes in the program, where we need to change the order of execution of a particular line of code based on specific condition or repeat a block of code until particular specified condition meet.
In such situations, we can use the control flow statements or simply control statements in Python programming language.
Types of Control Flow Statements in Python
Python programming language supports three kinds of control flow statements.
- Sequential control flow statements
- Conditional control flow statements
- Loop control flow statements
The further classification of control flow statements in Python has shown in the below figure.
Sequential Control Flow Statements
This refers to line by line execution in which Python executes a series of statements in a sequence appearing in the program. Basically, it uses the default mode because the program control moves line by line in the program. Let’s take an example based on it.
Example:
# Python program to calculate the area of a triangle.
x = 4
y = 5
z = 6
# Calculating the semi-perimeter.
s = (x + y + z) / 2
# Calculating the area of triangle.
area = (s * (s -x) * (s -y) * (s - z)) ** 0.5
print('Area of triangle = %0.2f'%area)
Output: Area of triangle = 9.92
In this example, Python starts to execute from the first statement of the program. Python executes sequentially each statement. When the last statement is executed, the execution of program is complete.
Conditional Statements in Python
A conditional control statement is also known as selection control statements, decision control statements, or branching statements. It is based on the condition or decision. It executes a block of statements depending on the outcome of a decision.
If the condition is true, then the block of statements will execute. If the condition is false, the block of statements will not execute. We also use it for checking and testing purpose.
Look at the below general form of a typical decision making structure in Python programming language.
In Python programs, we use conditional control flow statements to perform different actions based on different conditions. Python supports three conditional control flow statements. They are:
- if statement
- if-else statements
- Nested if statements
- if-elif-else statements
These conditional statements, known as decision making statements in Python because they execute a block of statements based on a decision.
They use the boolean expression for conditional test. The boolean expression may be either a single expression or multiple expressions.
When Python evaluates this boolean expression, it generates a boolean value either TRUE or FALSE as a result. Based on the result, the conditional control flow statement executes the block of statements.
Note: If you are already a programmer of C, C++, Java, etc., then please keep the following points in your mind while using conditional control statements or decision making in Python:
- In Python, it is not mandatory to use parenthesis for writing the conditions.
- Don’t use the curly braces to define a block because Python uses indentation.
- Always use a colon character(:) after writing the conditions.
We will learn about all decision making or conditional statements in Python one by one in further tutorials with the help of best example programs.
Loop Control Flow Statements
A loop control statement allows us to execute or repeat a statement or a group of code multiple times. We call it as looping or iteration in Python. Looping simply means repetition of a block of code depending on a condition test.
As long as the test condition is true, a block of code is repeated again and again. As soon as the test condition becomes false, the repetition stops.
Look at the below general form of a loop control flow structure in Python programming language.
A set of statements that are repeated again and again is called loop body. We can use the loop control statement when we need to execute a block of code several number of times according to a particular condition.
Types of loop control statements
Python programming language supports three types of loops to handle iteration requirements. They are:
- for loop
- while loop
- nested loops
For loop is a counting loop that repeats a block of statements a certain number of times.
While loop is a conditional loop that keeps repeating a set of statements as long as some test condition is true.
We will learn about all loops in Python one by one in further tutorials with the help of best example programs.
Unconditional Statements in Python
An unconditional statement in Python is a statement in which the control of execution jumps to another part of the code without carrying out any conditional test. It is also called unconditional execution in Python.
Python programming language supports two types of unconditional statements. They are as follows:
- Break statement
- Continue statement
We will learn about both unconditional statements in Python one by one in further tutorials with the help of the best examples.
In this tutorial, you have learned conditional statements in Python with the help of examples. I hope that you will have understood the basic key points of conditional statements or decision-making statements. Stay tuned with our next tutorial.
Thanks for reading!!!