Unit - 2
Unit - 2
Statement Description
If Statement The if statement is used to test a specific condition. If the condition is true, a
block of code (if-block) will be executed.
If - else Statement The if-else statement is similar to if statement except the fact that, it also provides
the block of the code for the false case of the condition to be checked. If the
condition provided in the if statement is false, then the else statement will be
executed.
Nested if Statement Nested if statements enable us to use if ? else statement inside an outer if
statement.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for
the block level code. In Python, indentation is used to declare a block. If two statements are at the same
indentation level, then they are the part of the same block.
Generally, four spaces are given to indent the statements which are a typical amount of indentation in
python.
Indentation is the most used part of the python language since it declares the block of code. All the
statements of one block are intended at the same level indentation. We will see how the actual indentation
takes place in decision making and other stuff in python.
The if statement
The if statement is used to test a particular condition and if the condition is true, it executes a block of
code known as if-block. The condition of if statement can be any valid logical expression which can be
either evaluated to true or false.
The if-else statement
The if-else statement provides an else block combined with the if statement which is executed in the false
case of the condition.
The elif statement enables us to check multiple conditions and execute the specific block of statements
depending upon the true condition among them. We can have any number of elif statements in our
program depending upon our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
Loops and Control Statements (continue, break and pass) in Python
Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides
control statements like continue, break, and pass to manage the flow of the loops efficiently.
Table of Content
For Loops
While Loops
Control Statements in Loops
o break Statement
o continue Statement
o pass Statemen
For Loops
A for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range).
# Iterating over a list
a = [1, 2, 3]
for i in a:
print(i)
Output
1
2
3
While Loops
A while loop in Python repeatedly executes a block of code as long as a given condition is True.
cnt = 0
while cnt < 5:
print(cnt)
cnt += 1
Output
0
1
2
3
4
break Statement
The break statement is used to exit the loop prematurely when a certain condition is met.
# Using break to exit the loop
for i in range(10):
if i == 5:
break
print(i)
Output
0
1
2
3
Explanation:
The loop prints numbers from 0 to 9.
When i equals 5, the break statement exits the loop.
Continue Statement
The continue statement skips the current iteration and proceeds to the next iteration of the loop.
Output
1
3
5
7
9
Explanation:
Pass Statement
The pass statement is a null operation; it does nothing when executed. It's useful as a placeholder for code
that you plan to write in the future.
# Using pass as a placeholder
for i in range(5):
if i == 3:
pass
print(i)
Output
0
1
2
3
4
Explanation:
The pass statement does nothing and allows the loop to continue executing.
It is often used as a placeholder for future code.