Decision Making Statements & Loops
Decision Making Statements & Loops
• Decisions in a program are used when the program has conditional choices to
execute a code block.
• In Python, decision making statements are those that decide whether a block
of statements has to execute or not based on a condition.
• Decision making statements are also called Conditional Statements.
➢ if
➢ if else
➢ elif
IF Statement
ELIF
➢ The if-elif statement is shortcut of if..else chain.
➢ For example, if we want to show a message 100 times, then we can use a loop
➢ There are 2 types of loops in Python:
• for loop
• while loop
Python for Loop
In Python, a for loop is used to iterate over sequences such as lists, tuples, string, etc.
For loops are used for sequential traversal.
In Python, there is “for in” loop which is similar to for each loop in other languages.
Let us learn how to use for in loop for sequential traversals.For example,
Python for loop with else
• A for loop can have an optional else block.
• The else part is executed when the loop is exhausted (after the loop iterates
through every item of a sequence).
For example,
WHILE LOOP
While loops are used in Python to iterate until a specified condition is met.
In python, a while loop is used to execute a block of statements repeatedly until a
given condition is satisfied. And when the condition becomes false, the line
immediately after the loop in the program is executed.
BREAK & CONTINUE
Python break Statement
When the loop encounters the value "date", the break statement is executed, and
the loop is terminated.
CONTINUE
Python continue Statement
The continue statement is used to skip the current iteration of the loop and the
control flow of the program goes to the next iteration.
EXERCISE PROGRAMS-IF-ELSE-ELIF
Practice questions on Decide if/else
1.
A company decided to give bonus of 5% to employee if his/her year of service is more than 5
years.
Ask user for their salary and year of service and print the net bonus amount.
2.
Take values of length and breadth of a rectangle from user and check if it is square or not.
3.
Take two int values from user and print greatest among them.
4.
A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
5.
A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
6.
Take input of age of 3 people by user and determine oldest and youngest among them.
7.
8.
A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
9.
Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she
has medical cause or not ( 'Y' or 'N' ) and print accordingly.
EXERCISE -LOOPS
Practice questions on LOOPS...
1.
Take 10 integers from keyboard using loop and print their average value on the screen.
2
Print multiplication table of 24, 50 and 29 using loop.
3
4
Print all elements of a list using for loop.
Write a program to print n natural number in descending order using a while loop.
6.