0% found this document useful (0 votes)
6 views18 pages

Condtional Statements: IF If-Else Nested If

The document explains conditional statements in Python, including if, if-else, and nested if statements, which are used for decision making based on test expressions. It also covers the syntax and examples of while and for loops for iteration over code blocks. Indentation is emphasized as a key aspect of defining code blocks in Python.

Uploaded by

bioresearchpcod
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)
6 views18 pages

Condtional Statements: IF If-Else Nested If

The document explains conditional statements in Python, including if, if-else, and nested if statements, which are used for decision making based on test expressions. It also covers the syntax and examples of while and for loops for iteration over code blocks. Indentation is emphasized as a key aspect of defining code blocks in Python.

Uploaded by

bioresearchpcod
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/ 18

CONDTIONAL STATEMENTS

IF
IF-ELSE
NESTED IF

• Decision making is required when we want to


execute a code only if a certain condition is
satisfied.

• The if…elif…else statement is used in Python for


decision making.
IF STATEMENT
Python if Statement Syntax

if test expression:
statement(s)

• Here, the program evaluates the test expression and will


execute statement(s) only if the text expression is True.

• In Python, the body of the if statement starts with an


indentation and the first unindented line marks the end of
if.
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")

Output:
3 is a positive number
This is always printed
This is also always printed.
IF ELSE
Syntax of if...else

if test expression:
Body of if
else:
Body of else

• The if..else statement evaluates test expression and will


execute body of if only when test condition is True.

• If the condition is False, body of else is executed. Indentation


is used to separate the blocks.
# Program checks if the number is positive or negative And
#displays an appropriate message

num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Output:
Positive or Zero
If..elif…else
• The elif is short for else if.
• It allows us to check for multiple expressions.
• If the condition for if is False, it checks the condition of the
next elif block and so on.
• If all the conditions are False, body of else is executed.
• Only one block among the several if...elif...else blocks is
executed according to the condition.
• The if block can have only one else block. But it can have
multiple elif blocks.
Syntax of if...elif...else

if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive or Zero
Nested if statements
• We can have a if...elif...else statement inside
another if...elif...else statement. This is called nesting in
computer programming.
• Any number of these statements can be nested inside one
another. Indentation is the only way to figure out the level of
nesting.
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number") Output:
else: Enter a number: 5
print("Negative number") Positive number
Python Indentation
Nesting indicated by indentation
Python uses indentation to show block structure.
Indent one level to show the beginning of a block.

in Python would be:

if x:
if y:
f1()
f2()
The while statement
• The while loop in Python is used to iterate over a block of
code as long as the test expression (condition) is true.
• Used for repeating an action or set of actions
• We generally use this loop when we don't know beforehand,
the number of times to iterate.

Syntax of while Loop:

while test_expression:
Body of while
• In while loop, test expression is checked first. The body
of the loop is entered only if the test expression evaluates
to True. After one iteration, the test expression is
checked again. This process continues until the test
expression evaluates to False.

• In Python, the body of the while loop is determined


through indentation.

• Body starts with indentation and the first unindented


line marks the end.

• Python interprets any non-zero value


as True. None and 0 are interpreted as False.
Flowchart of
while Loop
Example: Python while Loop
Program to add natural numbers upto sum = 1+2+3+...+n

n = int(input("Enter n: ")) #To take input from the user


sum = 0 # initialize sum and counter
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter
Output:
print("The sum is", sum) # print the sum
Enter n: 10
The sum is 55
Python For Loops
• The for loop in Python is used to iterate over a sequence (list,
tuple, string) or other iterable objects. Iterating over a
sequence is called traversal.

Syntax of for Loop


for val in sequence:
Body of for

• Here, val is the variable that takes the value of the item inside
the sequence on each iteration.
• Loop continues until we reach the last item in the sequence.
The body of for loop is separated from the rest of the code
using indentation.
Flowchart of
for Loop
Example: Python for Loop

for x in "banana":
print(x)
b
a
n
a
n
a
# Increment the sequence with 3 (default is 1):

for x in range(2, 30, 3):


for x in range(6):
print(x)
print(x)
2
0 5
1 8
2 11
3 14
4 17
5 20
23
26
29
Thanks

You might also like