prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

Python Programming Part 07

Rajjit Oct 02nd, 2023 15 mins read
part-07

TOPIC: Python Decision Control Statements

Decision Control Statements:
A control statement is a statement that determines the control flow of a set of instructions, that is it decides the sequence in which the instructions in a program are to be executed. It can either have one or more instructions. The three fundamental methods of control flow in a programming language are sequential, selection and iterative control.
A python program executes in a sequential control manner, where the flow looks like it starts from the first line and ends at the last line. This method is known as the Sequential Control Flow.
Now, let's learn how we can execute a selected set of statements using the Selection control. It allows the programmers to build a program that determine which statements of the code should be executed and which should be ignored.

SELECTION/CONDITIONAL statements
The control statements check a condition and then go to the directed statement. That is, the condition is checked and then goes on to the given section of the code. Some of the conditional statements are:

  • if statement
  • if-else statement
  • if-elif-else statement

if statement
An "if" statement is a programming construct that allows for conditional execution of code based on the evaluation of a specified condition. If the condition is true, a specific code block is executed; otherwise, it is skipped, enabling decision-making within the program's flow.

The following are the syntax and the flowchart of an 'if statement':

if-else if-else

if-else statement
An "if-else" statement is a programming construct used for conditional execution of code. It evaluates a specified condition, and if the condition is true, one code block is executed; if the condition is false, an alternative code block is executed. It provides a way to handle two different scenarios or outcomes based on the condition's truth value.

The following are the flowchart and the syntax of an 'if-else statement':

if-else if-else

if-elif-else statement
An "if-elif-else" statement is a programming construct used for multiple conditional branches. It starts by evaluating the first condition; if it's true, the corresponding code block is executed, and the rest of the branches are skipped. If the first condition is false, it moves to the next "elif" (else if) condition, and so on, until it finds a true condition or reaches the "else" block, which serves as a default if none of the conditions are met. It provides a way to handle multiple scenarios or outcomes based on the first true condition encountered.

The following are the flowchart and the syntax of an 'if-elif-else statement':

if-else if-else
As a note, there is also a statement called 'Nested if statements'
Nested if statements are a programming structure where one if statement is placed inside another. This is done to create a hierarchy of conditions, where the inner if statement is evaluated only if the outer if statement's condition is true.
Example codes are given below Video tutorial:

Code as discussed in the video:

# Python Decision Control Statements also conditional statements

# if statement
'''
    syntax: 
        if (expressions):
            statement1
            ....
            statementn
        statementx
'''
# x = 10
# if(x>0):
#     x = x+1
# print(x)

# if-else statement
'''
    syntax: 
            if(expressions):
                statement1
            else:
                statement2
'''
# user_age = int(input("Enter your age: "))
# if(user_age >= 18):
#     print("You are eligible to vote")
# else:
#     print("You are not eligible to vote")

# if-elif-else statement
'''
    syntax:
            if(expression1):
                statement1
            elif(expression2):
                statement2
            else:
                statement3
'''
# num = int(input("Enter a number: "))
# if(num == 0):
#     print("The number is zero")
# elif (num > 0):
#     print("the number is a positive number")
# else:
#     print("the number is a negative number")

You can copy and run this code