Control Statements
➢The order in which statements are executed in a program is called the “Flow Of
Control”.
➢Left unregulated, a program proceeds through its statements from beginning to end.
➢When a statement has been executed, control is turned over to the next statement (
like a baton being passed in a relay race).
➢Thus, normally flow of control is sequential.
➢If we want the flow of control to be non-sequential, we can use control structures
(Control Statements).
Control Statements (Contd.)
➢ Control statements enable us to specify the flow of program control; i.e., the order in
which the instructions in a program must be executed.
➢ They make it possible to make decisions, to perform tasks repeatedly or to jump from
one section of code to another.
➢ There are two types of Control Statements in Python:
➢Conditional/ Selection Statements.
➢Looping Statements.
Conditional/ Selection Statements
➢ There come situations in real life when we need to make some decisions and based on
these decisions, we decide what should we do next.
➢ For example, if I pass in grade first, I will go for engineering.
➢ Similar situations arise in programming also where we need to make some decisions
and based on these decisions we will execute the next block of code.
➢ The Conditional/ Selection Statements are used to execute a statement or set of
statements depending if a particular condition is satisfied or not.
➢ If the condition evaluates to true, then a statement or a set of statements is executed
otherwise another set of statements is executed.
Conditional/ Selection Statements (Contd.)
➢ In Python, we have the following Conditional/ Selection Statements.
▪ if Statement
▪ if – else Statement
▪ If- elif - else Statement
if Statement
➢ The if statement instructs the computer to perform a certain action if the condition is
true.
➢ However, when the condition is false, no action will be performed.
➢ The if statement first tests an expression, which is known as a condition.
➢ The statements inside if statement will be executed only when the “condition” returns
true, otherwise those statements will be ignored for execution.
➢ Syntax for if statement is:
if condition:
Statement(s)
if Statement (Contd.)
➢ Where, condition is a Boolean or relational condition and Statement(s) is a simple or
compound statement.
➢ Examples:
➢ if a < b:
print (“a is the greater number”)
➢ if age >= 18:
print("Eligible to vote")
if else Statement
➢ The problem with if Statement is that if the condition evaluates to false, the program
will do nothing.
➢ In this case we can use an if else Statement.
➢ The if else statement executes one block of statements if the condition is True and
another block of statements if the condition is False.
➢ The syntax of the if else statement is as follows:
if condition:
statementblock1
else:
statementblock2
if else Statement (Contd.)
➢ Example of an if else statement is:
a=10
b=20
if a > b:
print (“a is the greater number”)
else
print (“b is the greater number”)
if elif else Statement
➢ Many a times there are situations that require multiple conditions to be checked and it
may lead to many alternatives.
➢ The if elif else statement is used in situations that require multiple conditions to be
checked.
➢ The syntax of the if elif else statement is as follows:
if condition:
statement(s)
elif condition:
statement(s)
if elif else Statement (Contd.)
elif condition:
statement(s)
.
.
.
else:
statement(s)
➢Number of elifs is dependent on the number of conditions to be checked.
➢If the first condition is false, then the next condition is checked, and so on. If one
of the conditions is true, then the corresponding indented block executes, and then
the if statement terminates.
if elif else Statement (Contd.)
➢Example: Check whether a number is positive, negative, or zero.
number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
Looping Statements
➢ Looping Statements are the statements that allow the programmer to execute a certain
set of statements repeatedly until a given condition is true.
➢ The statements in a loop are executed again and again as long as a particular logical
condition remains true.
➢ This condition is checked based on the value of a variable called the loop’s “control
variable”.
➢ When the condition becomes false, the loop terminates.
➢ It is the responsibility of the programmer to ensure that this condition eventually does
become false so that there is an exit condition and it does not become an infinite loop.
Types of Looping Statements
➢There are two types of Looping Statements in Python:
➢While Loop
➢For Loop
While Loop
➢ The while statement executes a block of code repeatedly as long as the control
condition of the loop is true.
➢ The control condition of the while loop is executed before any statement inside the
loop is executed.
➢ After each iteration, the control condition is tested again and the loop continues as long
as the condition remains true.
➢ When this condition becomes false, the statements in the body of loop are not executed
and the control is transferred to the statement immediately following the body of while
loop.
➢ If the condition of the while loop is initially false, the body is not executed even once.
While Loop (Contd.)
➢ Syntax of while Loop
while test_condition:
statements(s)
➢ Program to print first 5 natural numbers using while loop.
count = 1
while count <= 5:
print(count)
count += 1
For Loop
➢ The for statement is used to iterate over a range of values or a sequence (list, tuple, set,
dictionary, and string).
➢ The for loop is executed for each of the items in the range.
➢ These values can be either numeric, or, they can be elements of a data type like a
string, list, or tuple.
➢ With every iteration of the loop, the control variable checks whether each of the values
in the range have been traversed or not.
➢ When all the items in the range are exhausted, the statements within loop are not
executed; the control is then transferred to the statement immediately following the for
loop.
For Loop (Contd.)
➢ Syntax of the For Loop:
for <control-variable> in <sequence/ items in range>:
<statements inside body of the loop>
➢ Example:
count = [10,20,30,40]
for num in count:
print(num)
Output:
10
20
30
40
Indentation
➢ In most programming languages, the statements within a block are put inside curly
brackets.
➢ However, Python uses Indentation for block as well as for nested block structures.
➢ Leading whitespace (spaces and tabs) at the beginning of a statement is called
Indentation.
➢ In Python, the same level of indentation associates statements into a single block of
code.
➢ The interpreter checks indentation levels very strictly and throws up syntax errors if
indentation is not correct.
➢ It is a common practice to use a single tab for each level of indentation.