DEV Community

Phylis Jepchumba
Phylis Jepchumba

Posted on

Day 2: Introduction to Control Flow in Python

Day 2 of The 30 Days of Python.
Introduction to Control Flow In Python
  • What is Control flow?
  • Types of Control structures.

    • sequential
    • selection: If statement, if...else statement,if..elif...else, nested if,
    • Repetition: for loop, while loop

What is Control flow?
Control flow is the order in which a programs code executes based on values and logic.
Python programming language is regulated by three types of control structures:

  • Sequential statements.
  • Selection statement.
  • Repetition statement.
Sequential statements.

Are set of statements where the execution process will happen in sequence manner.
It is not commonly used because if the logic has broken in any line, then the complete source code execution will break.
To avoid this problem, selection and repetition statements are used

Selection Statements.

Are also called decision control statements or branching statements.
Selection statements allows a program to execute instructions based on which condition is true.
They include:

  1. if Statement.

If statement executes a block of code based on a specified condition.
Here is the syntax;

if condition:
    if-block
Enter fullscreen mode Exit fullscreen mode

The if statement checks the condition first, if the condition evaluates to True the statement is executed in the if-block. Otherwise, it ignores the statements.

Example

age= input('Enter your age:')
if int(age) >= 40:
    print("You are an adult")
Enter fullscreen mode Exit fullscreen mode

Output

Enter your age:46
You are an adult
Enter fullscreen mode Exit fullscreen mode

2.if…else statement
It evaluates the condition and will execute the body of if if the test condition is True, and else body if the condition is false
Syntax

if condition:
    if-block;
else:
    else-block;
Enter fullscreen mode Exit fullscreen mode

Example

age = input('Enter your age:')
if int(age) >= 40:
    print("You are an adult.")
else:
    print("You are a child.")
Enter fullscreen mode Exit fullscreen mode

Output

Enter your age:30
You are a child.
Enter fullscreen mode Exit fullscreen mode

3.if…elif…else statement.
Checks multiple conditions for true and execute a block of code as soon as one of the conditions evaluates to true.
If no condition evaluates to true, the if...elif...else statement executes the statement in the else branch.
The elif stands for else if.

Syntax:

if if-condition:
    if-block
elif elif-condition1:
    elif-block1
elif elif-condition2:
    elif-block2
...
else:
    else-block
Enter fullscreen mode Exit fullscreen mode

Example

age = input('Enter your age:')

your_age = int(age)

if your_age >= 70:
    print("Your are old")
elif your_age >= 40:
    print("Your young")
else:
    print("null")

Enter fullscreen mode Exit fullscreen mode

Output

Enter your age:80
Your are old
Enter fullscreen mode Exit fullscreen mode
Repetition statement.

Also called loops and are used to repeat the same code multiple times.
Python has two repetitive statements namely:

  1. for Loop Used to iterate over a sequence that is either a list, set, tuple or dictionary. Syntax
for index in range(n):
    statement
Enter fullscreen mode Exit fullscreen mode

From the above syntax; n is the number of times the loop will execute and index is the loop counter

Example

# list
numbers = [10,12,13,14,17]
# variable to store the sum
sum = 0
# iterate over the list
for index in numbers:
    sum = sum+index
print("The sum is", sum)
Enter fullscreen mode Exit fullscreen mode

Output

The sum is 66
Enter fullscreen mode Exit fullscreen mode
for index in range(4):
    print(index)
Enter fullscreen mode Exit fullscreen mode

Output

0
1
2
3
Enter fullscreen mode Exit fullscreen mode

2.While loop.
Python while statement allows you to execute a block of code repeatedly until a given condition is satisfied.
Syntax:

while condition:  
   body
Enter fullscreen mode Exit fullscreen mode

An example to print 6 numbers from 0 to 5:

max = 6
counter = 0

while counter < max:
    print(counter)
    counter += 1
Enter fullscreen mode Exit fullscreen mode

Output

0
1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode
Resources.

(https://docs.python.org/3/tutorial/controlflow.html)

(https://builtin.com/software-engineering-perspectives/control-flow)

Top comments (0)