Decision Making
Decision Making
Python If Statement
The if statement in Python evaluates whether a condition is true or false. It contains a logical
expression that compares data, and a decision is made based on the result of the comparison.
if expression:
# statement(s) to be executed
If the boolean expression evaluates to TRUE, then the statement(s) inside the if block is executed.
If boolean expression evaluates to FALSE, then the first set of code after the end of the if block is
executed.
Discount = 0
amount = 1200
# Check he amount value
if amount > 1000:
discount = amount * 10 / 100
print("amount = ", amount - discount)
Nested if statement
Python supports nested if statements which means we can use a conditional if and
if...else statement inside an existing if statement. There may be a situation when you
want to check for additional conditions after the initial one resolves to true. In such a
situation, you can use the nested if construct. Additionally, within a nested if
construct, you can include an if...elif...else construct inside another if...elif...else
construct. Syntax of Nested if Statement The syntax of the nested if construct with
else condition will be like this − if boolean_expression1: statement(s) if
boolean_expression2: statement(s) Flowchart of Nested if Statement Following is the
flowchart of Python nested if statement
Loops in python
● For loop
This loop starts with the for keyword, followed by a variable that represents the
current item in the sequence.
The in keyword links the variable to the sequence you want to iterate over.
A colon (:) is used at the end of the loop header, and the indented block of code
beneath it is executed once for each item in the sequence.
Syntax:
For item in sequence:
Body of for
Example1:
Flowers = [“rose”,”jasmimin”,”lotus”]
For flower in flowers:
Print(“current flower :”,flower)
Output:
Current flower : rose
Current flower :jasmin
Current flower : lotus
Example 2:
For I in range (5)
Output
12345
Example 3:pgm to display multiplication Table
● While loop
With the while loop we can execute a set of statements as long as a condition is true.
Syntax:While Test_Expresion:
Body of while
Example
i = 1
while i < 6:
print(i)
i += 1
Out put
1
2
3
Control Structures
Python support the following control statements
1. Break
2. Continue
3. Pass
Break statement
It cab be used with both for and while loops.
The break statement in Python is used to exit or “break” out of a loop (either a for or
while loop) prematurely, before the loop has iterated through all its items or reached its
condition
Example
For num in range (1,6)
If num==4:
break
Output
1
2
3
Continue statement
.
The continue statement in Python is used to skip the current iteration of a loop
and move to the next iteration.
It is commonly used in both for and while loops when you want to bypass
specific conditions within the loop.
example
# Print all numbers except multiples of 3
for num in range(1, 11):
if num % 3 == 0:
continue
print(num)
output
4
6
7
8
9
10
Example 2
# Demo of continue in python
For letter in “abcd”:
If letter == “c” :
continue
Print (letter)
Output
a
b
D
Pass
In python pass is a null statement .the difference between pass and
comments statement in python is that,while the interpreter ignores a
comment entirely ,pass is not ignored. But nothing happens When it is
executed .it result in no operation..it is used as a place holder.
Example
X=10
If x>5:
Pass # place holder for future code.
Else:
Print(“x is 5 or less)
Output
No output