Lec 6. Control Structures
Lec 6. Control Structures
Programming
Control
Structures
• Control flow is the order that instructions are executed in a program.
• A control statement is a statement that determines the control flow of
a set of instructions.
• Types of Control:
• Sequential control: Instructions are executed in the order that
they are written
• Selection control: Selectively executes the instructions.
Control E.g. Decision Control
• Iterative control: Repeatedly executes the instructions. E.g. Loops.
Structures
Selection Control or Decisions
(It is a control statement providing selective execution of instructions)
Decisions • if statements
in a • if else statements
Python • elif statements
program • nested if conditions
If Statement Syntax:
It is a selection control statement based on the if test expression:
value of a given Boolean expression statement(s)
Expression’s value can be True or False.
We may want to do something only when a certain condition is true.
If it results to False
• then the block is skipped and control transfers to the statements
after the block.
2.
5.
3.
Example: What will be the output ?
1. 4.
Output: Smaller
Output: 3 is a positive number.
2. This is always printed.
5.
Output: Finish
3.
Output: This is also always printed.
Output: yay
if...else Statement
a.k.a. Two way decisions
Example
• One unique aspect of Python is that the amount of
indentation of each program line is significant.
Header, Suite
and Indentation
Nested if statements (multi-way selection)
This is to implement
further checks.
Nested if statements: Example
Example: What will be the output?
Example: What will be the output?
if...elif...else Statement
• Loop Statements:
• While
• For
• Nested loop
While Statement (indefinite loop)
• A while statement is an iterative control statement that repeatedly executes a set of
statements based on a provided Boolean expression (condition).
Example 1
• Find all even numbers from 0 to n. where, n is given by user.
Example 2
• Print all even numbers between n to m. m should be greater than n.
Example 3
Write a program to take numbers from the user until he enter 0 as
input. then print sum of all entered number.
Example 4
• Write an efficient program to determine sum of N natural numbers
where N is given by user.
For Loop Loop to read
(definite loop) sequence
Syntax:
for iterating_var in sequence:
statements(s)
• Example:
Sequences
• Sequence of character - 'QWERTYUIOPASDFGHJKL’
• Sequence of words - ['abc','def','efg','ijk’]
• Sequence of numbers - [1,2,3,4,5,6,7,8,9]
• Sequence of mix data – [‘Suvi’, 4, “LKG”, “Bennett University”, 98.5
2. 5.
3.
For Loop: Answers to Previous Questions
1 2 3 4 5
Exercise: Write a program to find whether a given
number is prime or not
• Solution:
•Let one grain of wheat be placed on the first square
of a chessboard, two grains on the second square,
four grains on the third square, eight grains on the
fourth square, and so on until all square are filled in
chessboard. what will be the total weight in ton of
grains on whole 8×8 chessboard? If 15432 grains in
one kg and 907.18 kg in one ton then.
Problem
Exercise 2
• Loop inside a loop a is called nested loop.
• Example:
Nested Loop
Nested Loop: What will be the output?
1.
2. 4.
5.
3.
Find all prime numbers between given two numbers
Find first 100 prime numbers start from 2.
Find number is Strong or not
If the sum of the factorial of the digits in a number is equal to the original number, the
number is a strong number.
Accept the limit and print the strong numbers from 1 to
the given limit.
• An infinite loop is an iterative control structure
that never terminates (or eventually terminates
with a system error).
• Infinite loops are generally the result of
programming errors.
• For example: if the condition of a while loop can
never be false, an infinite loop will result when
executed.
Infinite loop
Loop Control Statements
• Break Statement: Terminates the loop statement and transfers execution to the
statement immediately following the loop.
• Continue Statement: Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
• Continue Statement: