0% found this document useful (0 votes)
4 views12 pages

5_Decision Making

The document explains the concept of conditional statements in programming, particularly in Python, which are used to control the flow of a program based on specific conditions. It covers various types of conditional statements including if statements, if...else statements, nested if statements, and if...elif statements, providing examples for each. The importance of decision-making in programming is emphasized, highlighting how conditions are evaluated to determine the execution of code.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

5_Decision Making

The document explains the concept of conditional statements in programming, particularly in Python, which are used to control the flow of a program based on specific conditions. It covers various types of conditional statements including if statements, if...else statements, nested if statements, and if...elif statements, providing examples for each. The importance of decision-making in programming is emphasized, highlighting how conditions are evaluated to determine the execution of code.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Decision Making

Learning objective
• Conditional Statements
• If statement
• If….else statement
• Nested if statement
• If …..elif statement
Conditional Statements
• There are situations in real life when we need to do some specific
task and based on some specific conditions, we decide what we
should do next.

• Similarly, there comes a situation in programming where a specific


task is to be performed if a specific condition is True.

• In such cases, conditional statements can be used.

• In Python, conditional statements are used to control the flow of a


program based on certain conditions.
Conditional Statements
• Decision-making is as important in any programming language as
it is in life.

• Decision making in a programming language is automated using


conditional statements, in which Python evaluates the code to
see if it meets the specified conditions.

• The conditions are evaluated and processed as true or false. If this


is found to be true, the program is run as needed. If the condition
is found to be false, the statement following the If condition is
executed.
If statement
• An if statement consists of a Boolean expression followed by one
or more statements.
• If the expression is evaluated to TRUE, the statement gets
executed. But if it’s FALSE, nothing happens.

a = 15
if (a > 10): #you can also write as if a>10:
print("a is greater")
If…else statement
• An if statement can be followed by an optional else statement,
which executes when the Boolean expression is false.

x = 305
y = 405
if x > y:
print("x is greater")
else:
print("y is greater")
Nested if statement
• A nested if is an if statement that is the target of another if
statement.

• Nested if statements means an if statement inside another if


statement.

• Python allows us to nest if statements within if statements. i.e., we


can place an if statement inside another if statement.
Nested if statement
a = 56
if(a > 40):
if(a > 45):
print("a is greater than 45")
else:
print("a is less than 45")
else:
print("a is less than 40")
If…elif statement
• The elif statement allows you to check multiple expressions for
TRUE and execute a block of code as soon as one of the
conditions evaluates to TRUE.

• Similar to the else, the elif statement is optional.

• elif - is a keyword used in Python replacement of else if to place


another condition in the program.

• This is called chained conditional.


If…elif statement Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
If…elif statement Example
P = 15
Q = 10
if P > Q:
print("P is greater")
elif P == Q:
print("both are equal")
elif P < Q:
print("Q is greater")
You have Learnt:
• Conditional Statements
• If statement
• If….else statement
• Nested if statement
• If …..elif statement

You might also like