Data Analytics

Control Flow in Python: Using if statements and loops

Sumer Pasha

Sumer Pasha

Jan 12 - 0 min read

Control flow is a fundamental concept in programming that allows you to dictate the order in which statements are executed in your code. In Python, control flow is managed through the use of conditional statements (if statements) and loops.

Conditional statements

Conditional statements are used to guide a program's flow based on certain conditions. The main conditional statements include "if," "elif" (short for "else if"), and "else." These statements allow you to run different code blocks depending on whether a specified condition is true or false.

Python If Statement

The "if" statement stands as the most elementary decision-making construct. Its purpose is to determine the execution or omission of specific statements or a block of statements based on a given condition. The "if" statement is employed to assess a condition. Should the condition prove to be true, the indented code block beneath the "if" statement is executed. Conversely, if the condition is false, the code block is bypassed.

Syntax

 If (Conditional expression):
           Code line 1
           Code line 2

Flow Chart

1 (2).png

Example

2 (1).png

Python If-Else Statement

The "if" statement, in isolation, signifies that upon the condition being true, a designated block of statements will execute; conversely, if false, it will not. However, to introduce an alternative course of action in the event of a false condition, the "else" statement can be coupled with the "if" statement, enabling the execution of a distinct code block when the "if" condition is false.

Syntax

if (condition):
          # Code 
          # condition is true
else:
       # Code
       # condition is false

Flow Chart

3 (2).png

Example

4 (1).png

Python if-elif-else Statement

The "if" statements are processed in a top-down manner. Upon encountering the first true condition, the corresponding "if" statement's associated code is executed, and the subsequent conditions are disregarded. Should none of the conditions prove true, the concluding "else" statement is activated.
The "elif" statement facilitates the sequential examination of multiple conditions. Should the condition following an "if" statement prove false, the subsequent "elif" statement is appraised. In the event that its condition is true, the associated code block is executed, and any subsequent "elif" or "else" statements are bypassed.

Syntax

if (condition):
       Statement
elif (condition):
       Statement
.
.
else: 
       statement

Flow Chart

5 (1).png

Example

6.png

Nested-If Statement in Python

A nested "if" is an "if" statement positioned within the scope of another "if" statement. Essentially, nested "if" statements entail the inclusion of an "if" statement within another. Python affords the capability to nest "if" statements, enabling the placement of an "if" statement inside another.

Syntax

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here

Example

7.png

Loops in Python

For Loop

A for loop constitutes a control flow statement present in numerous programming languages, including Python, C, Java, and others. Its purpose is to iterate over a sequence, be it a list, tuple, string, or range, and execute a designated block of code for each item within the sequence.

Syntax

for variable in iterable:
          # code to be executed
  • iterable is a sequence (e.g., a list, tuple, string, or range).
  • variable that takes on the value of each element in the iterable during each iteration.

Flow chart 8.png

Example

9.png

Range Function

the range() function is often used in conjunction with loops to generate a sequence of numbers. The range() function returns an object that produces a sequence of numbers based on the specified parameters. It is commonly used in for loops.

Syntax

1) for i in range(stop):
              # code to be executed
2) for i in range(start, stop):
          # code to be executed

Example

11.png

While loop

A while loop is employed to iteratively execute a block of statements until a specified condition is met. Upon the condition becoming false, the program proceeds to execute the line immediately following the loop.
code to be executed while the condition is true
This block will keep executing until the condition becomes false

Syntax

While expression:
           Statement(s)

Flow Chart

12.png

Example

13.png

Loop Control Statements

Break Statement

The break statement is used to exit the loop prematurely, regardless of whether the loop condition is True or not.

Example

14.png

Continue Statement

The continue statement is used to skip the rest of the code inside the loop for the current iteration and move to the next iteration.

Example

15.png

Conclusion

Comprehending control flow in Python, achieved through the utilization of conditional statements and loops, proves essential for crafting efficient and adaptable programs. Mastery of these concepts empowers you to imbue your code with dynamic responsiveness to varying conditions and facilitates the iteration over sequences of data. This foundational knowledge is imperative for attaining proficiency in Python programming.

about the author

Sumer Pasha is a Digital Automation Engineer with Analogica India. He is a python developer and uses python to develop internal utilities for Analogica.