Selective statements – Iterative statements - Function: definition, call, return
statement,parameters and arguments types (required, keyword, default, variable length),
local and global scope, function composition, recursion, lambda functions. Illustrative
programs: square root, gcd, sum an array of numbers, linear search, binary search.
2.1 INTRODUCTION :
Control structures are essential in Python programming as they direct the flow of
execution in a program. Without them, code would run line by line without any decision-making
capability, meaning that no choices or repetitive tasks could occur.
• Control structures enable programmers to manage how their code behaves based on
specific conditions.
• For example, an if statement allows the program to execute certain actions only if a
condition is true. This ability to make decisions enhances the flexibility and functionality
of programs.
• Additionally, control structures contribute to making code easier to read and maintain.
Ultimately, control structures in Python streamline the process of making decisions during
program execution, making them indispensable tools for developers.
Types of Control Structures :
Control flow refers to the sequence a program will follow during its execution.
Conditions, loops, and calling functions significantly influence how a Python program is
controlled.
There are three types of control structures in Python:
• Sequential - The default working of a program
• Selective - This structure is used for making decisions by checking conditions and
branching
• Iterative/ Repetition - This structure is used for looping, i.e., repeatedly executing a
certain piece of a code block.
Sequential :
Sequential statements are a set of statements whose execution process happens in a
sequence. The problem with sequential statements is that if the logic has broken in any one of the
lines, then the complete source code execution will break.
Code
,# Python program to show how a sequential control structure works
# We will initialize some variables
# Then operations will be done
# And, at last, results will be printed
# Execution flow will be the same as the code is written, and there is no hidden flow
a = 20
b = 10
c=a-b
d=a+b
e=a*b
print("The result of the subtraction is: ", c)
print("The result of the addition is: ", d)
print("The result of the multiplication is: ", e)
Output:
The result of the subtraction is: 10
The result of the addition is :30
The result of the multiplication is: 200
Selection/Decision Control Statements
The statements used in selection control structures are also referred to as branching
statements or, as their fundamental role is to make decisions, decision control statements.
A program can test many conditions using these selection statements, and depending on
whether the given condition is true or not, it can execute different code blocks.
There can be many forms of decision control structures. Here are some most commonly used
control structures:
j* If statement
* If..Else statement
* Nested If..else statement
* If...else Ladder.
Conditional (if):
The if statement contains a logical expression using which data is compared and a
decision
is made based on the result of the comparison.
Syntax:
if expression:
statement(s)
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if
,statement is executed. If boolean expression evaluates to FALSE, then the first set of
code after the end of the if statement(s) is executed.
if Statement Flowchart:
Fig: Operation of if statement
Example: Python if Statement
a=3
if a > 2:
print(a, "is greater")
print("done")
a = -1
if a < 0:
print(a, "a is smaller")
print("Finish")
Output:
C:/Users/SAEC/AppData/Local/Programs/Python/Python38-32/pyyy/if1.py
3 is greater
done
-1 a is smaller
, Finish
--------------------------------
a=10
if a>9:
print("A is Greater than 9")
Output:
C:/Users/SAEC/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py
A is Greater than 9
Alternative if (If-Else):
An else statement can be combined with an if statement. An else statement contains the
block of code (false block) that executes if the conditional expression in the if statement
resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else
Statement following if.
Syntax of if - else :
if test expression:
Body of if stmts
else:
Body of else stmts
If - else Flowchart :