0% found this document useful (0 votes)
39 views

Decision Making Statements & Loops

Uploaded by

jincy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Decision Making Statements & Loops

Uploaded by

jincy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DECISION MAKING STATEMENTS

• 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.

Types of decision-making in python


There are three types of decision making statements in python language, those are

➢ if
➢ if else
➢ elif

IF Statement

➢ In computer programming, we use the if statement to run a block code only


when a certain condition is met.

➢ The if statement is used to test a specific condition.

➢ If the condition is true, a block of code (if-block) will be executed.


EXAMPLES:
IF ELSE STATEMENT
➢ The if-else statement is used to execute both the true part and the false part
of a given condition.
➢ If the condition is true, the if block code is executed and if the condition is
false, the else block code is executed.
EXAMPLES:

ELIF
➢ The if-elif statement is shortcut of if..else chain.

➢ The if...else statement is used to execute a block of code among two


alternatives.

➢ However, if we need to make a choice between more than two alternatives,


then we use the if...elif...else statement.
The syntax of the if...elif...else statement is:
LOOPS
➢ In computer programming, loops are used to repeat a block of code.

➢ 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

• The break statement is used to terminate the loop immediately when it is


encountered.
• You can use the break statement if you need to break out of a for or while
loop
• and move onto the next section of code.
In this example, the loop iterates through the list of fruits.

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.

Write a program to print absolute vlaue of a number entered by user. E.g.-


INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1

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

Write an infinite loop.


A inifinte loop never ends. Condition is always true.

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.

Write a program to display the first 7 multiples of 7.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy