top of page

Flow Control

In which order, statements are going to execute, is decided by flow control.

​

Types of flow control statement:

  1. Conditional statements/Selection Statements (if, if-else, if-elif-else)

  2. Iterative Statements (loops for and while)

  3. Transfer Statements (break, continue, pass)

​

Now I am going to discuss these 3 types of flow control statements in detail.

Conditional Statements/Selection Statements:

Among multiple available options select one and  execute it, such type of statements are considered as Conditional or Selection statements. Various Conditional statements are:

  • if

  • if-else

  • if-elif-else

​

if syntax:

if condition:

   statements

Example:

Conditional
Conditional Statements Python
Conditional Statements Python

in the above example if the name equals to ‘mujtaba’ then the corresponding If condition is executed. 

 

If-else syntax::

if condition:

   statements

else condition:

    statements          

 Example:

Conditional Statements Python
Conditional Statements Python

If-elif-else syntax:

if condition:

   statements

elif condition:

   statements

else condition:

    statements

Example:

Conditional Statements Python
Conditional Statements Python

Note 1: We can take any number of elif conditions after if condition.

Note 2: else is always optional.

​

Possible combinations of if:

If

If else

If elif else

If elif

​

Some programs on conditional statements:

#Program to find biggest of given 2 numbers from the keyboard

Conditional Statements Python
Conditional Statements Python

#Program to find biggest of given 3 numbers from the keyboard

Conditional Statements Python
Conditional Statements Python

Note: In this example we are using eval function so that number can be any type int or float.

Conditional Statements Python

#Write  a program to check whether the given number is even or odd

Conditional Statements Python
Conditional Statements Python

Iterative Statements (loops for and while):

Some times we require a group of statement execute iteratively until the given condition is true.

for loop: also known as for-each loop

​

Syntax:

For each_element in sequence:

do some action

Sequence can be string, list, range tuple or set.

​

Example:

Iterative
Iterative Statements (loops for and while) Python

Example: Enter a string from keyboard and count number of characters in string.

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Example: Print numbers from 0 to 10

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python
  • for loop

  • while (in python there is no do while loop)

​

Example: Print numbers from 10 to 1 in descending order.

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Example: Print sum of numbers of list.

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Note: for loop is mostly recommended in python. If we don't know the number of Iteration in advance then while loop is used.

​

While Loop syntax:

while condition:

         body

​

Example: Print numbers from 1 to 10 using while loop.

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Example: Sum of first n numbers

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

When to use ‘for loop’ and ‘while loop’?

If the number of iterations are known in advance, ‘for loop’ will be used otherwise 'while loop' will be used.

​

Nested Loops:

loop inside a loop is known as Nested loops.

Ex: print the below pattern.

*

* *

* * *

* * * *

* * * * *

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Example: Print the below pattern.

1

2 2

3 3 3

4 4 4 4

Iterative Statements (loops for and while) Python
Iterative Statements (loops for and while) Python

Transfer  Statements:

  • break

  • continue

  • pass

​

break:  ‘break’ is used to break loop execution based on some condition.

 

Example:

Transfer
Transfer Statements Python
Transfer Statements Python

continue: We can use continue within loop to skip the current iteration and go to the next iteration

 

Example: Print all odd numbers up to 10 by using continue.

Transfer Statements Python
Transfer Statements Python

pass: pass is a keyword in python. In our programming syntactically if block is required, which won’t do anything, then we can use pass. It is an empty/null statement like other languages.

​

Example:

Transfer Statements Python
Transfer Statements Python
bottom of page