0% found this document useful (0 votes)
8 views8 pages

Decision Making

The document explains decision-making structures in Python, including the use of if, if-else, and nested if statements to evaluate boolean expressions and execute corresponding code blocks. It also covers loops, specifically for and while loops, detailing their syntax and providing examples of how to use them. Additionally, it describes control statements like break, continue, and pass, illustrating their functionality within loops.

Uploaded by

Alan
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)
8 views8 pages

Decision Making

The document explains decision-making structures in Python, including the use of if, if-else, and nested if statements to evaluate boolean expressions and execute corresponding code blocks. It also covers loops, specifically for and while loops, detailing their syntax and providing examples of how to use them. Additionally, it describes control statements like break, continue, and pass, illustrating their functionality within loops.

Uploaded by

Alan
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/ 8

Decision making

Python's decision making functionality is in its keywords − if..elif...else. The if keyword


requires a boolean expression, followed by colon (:) symbol. The colon (:) symbol starts an
indented block. The statements with the same level of indentation are executed if the boolean
expression in if statement is True. If the expression is not True (False),
the interpreter bypasses the indented block and proceeds to execute statements at earlier
indentation level.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as
outcome. You need to determine which action to take and which statements to execute if
outcome is TRUE or FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the
programming languages −

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)

Python if else Statement


The if-else statement in Python is used to execute a block of code when the condition
in the if statement is true, and another block of code when the condition is false.

Syntax of if-else Statement

The syntax of an if-else statement in Python is as follows −


if boolean_expression:
# code block to be executed
# when boolean_expression is trueelse:
# code block to be executed
# when boolean_expression is false
If the boolean expression evaluates to TRUE, then the statement(s) inside the if
block will be executed otherwise statements of the else block will be executed.
Ifitt

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

Python loops allow us to execute a statement or group of statements multiple times.

​ Python loops allow us to execute a statement or group of statements multiple


times.

​ 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

num = int(input("Enter a number to display its multiplication table: "))

# Display multiplication table


print(f"\nMultiplication Table for {num}:") # f is the formatting string literal.
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output
Enter a number to display its multiplication table: 5

Multiplication Table for 5:


5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

●​ 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

Print i as long as i is less than 6:

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

fruits = ["apple", "banana", "cherry"]​


for x in fruits:​
print(x)​
if x == "banana":​
break
Print(num)
Output
Apple
Banana

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

You might also like