Python Flow-Control (Decision-Making) Statements

Python Flow-Control (Decision-Making) Statements#

In Python programming, decision-making statements are those that help in deciding the flow of the program.

Decision making is required when we want to execute a code only if a certain condition is satisfied.

The key thing to note about Python’s control flow statements and program structure is that it uses _indentation_ to mark blocks.

Hence the amount of white space (space or tab characters) at the start of a line is very important. This generally helps to make code more readable but can catch out new users of python.

  • Suppose you are given a number and are asked to check if it is an even number or not. How would you do it?

The first thought that would pop up your mind is this - “If the number is divisible by 2, then it is an even number, else it is an odd number”. That’s absolutely the right logic. But when this has to be coded, you will need the help of decision-making statements. To understand this, let’s now look at how they function.

Control Flow Statements#

The flow control statements are divided into three categories:

  1. Conditional statements

  2. Iterative statements

  3. Transfer/Control statements

Conditional statements#

In Python, condition statements act depending on whether a given condition is true or false. You can execute different blocks of codes depending on the outcome of a condition. Condition statements always evaluate to either True or False.

There are four types of conditional statements:

  1. if

  2. if-else

  3. if-elif-else

  4. nested-if

Iterative statements#

In Python, iterative statements allow us to execute a block of code repeatedly as long as the condition is True. We also call it a loop statements.

Python provides us the following two loop statement to perform some actions repeatedly

  1. for loop

  2. while loop

Transfer statements#

In Python, transfer statements are used to alter the program’s way of execution in a certain manner. For this purpose, we use three types of transfer statements.

  1. break statement

  2. continue statement

  3. pass statements