0% found this document useful (0 votes)
2 views6 pages

Unit - 2

This document covers Python's decision-making structures, including if, if-else, and nested if statements, emphasizing the importance of condition checking. It also explains indentation in Python, which is crucial for defining code blocks, and introduces loops (for and while) along with control statements (break, continue, pass) to manage loop execution. Examples illustrate the use of these concepts in practice.

Uploaded by

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

Unit - 2

This document covers Python's decision-making structures, including if, if-else, and nested if statements, emphasizing the importance of condition checking. It also explains indentation in Python, which is crucial for defining code blocks, and introduces loops (for and while) along with control statements (break, continue, pass) to manage loop execution. Examples illustrate the use of these concepts in practice.

Uploaded by

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

Unit -2

Python If-else statements


Decision making is the most important aspect of almost all the programming languages. As the name
implies, decision making allows us to run a particular block of code for a particular decision. Here, the
decisions are made on the validity of the particular conditions. Condition checking is the backbone of
decision making.

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

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.

# Using while loop

cnt = 0
while cnt < 5:
print(cnt)
cnt += 1

Output

0
1
2
3
4

Control Statements in Loops


Control statements modify the loop's execution flow. Python provides three primary control statements:
continue, break, and pass.

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.

# Using continue to skip an iteration


for i in range(10):
if i % 2 == 0:
continue
print(i)

Output

1
3
5
7
9

Explanation:

 The loop prints odd numbers from 0 to 9.


 When i is even, the continue statement skips the current iteration.

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.

You might also like