0% found this document useful (0 votes)
67 views

PYTHON Unit 2

This document discusses control flow structures in Python, including decision making statements like if, if-else, nested if-else, and if-elif-else statements. It also covers loops in Python, specifically the while and for loops. The while loop repeats a block of code as long as a condition is true, and the for loop iterates over a sequence, executing a block of code for each item. The document provides syntax examples and sample code for each of these control flow structures.

Uploaded by

Pratikharsora48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

PYTHON Unit 2

This document discusses control flow structures in Python, including decision making statements like if, if-else, nested if-else, and if-elif-else statements. It also covers loops in Python, specifically the while and for loops. The while loop repeats a block of code as long as a condition is true, and the for loop iterates over a sequence, executing a block of code for each item. The document provides syntax examples and sample code for each of these control flow structures.

Uploaded by

Pratikharsora48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT 2: CONTROL FLOW STRUCTURE

____________________________________________________
 Decision making structure

 Decision making statements can direct a program to execute on the based of


instructions if a certain condition is True or another if it's False.
 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.

There are three types of decision making statements in python language, those are
1. If statements
2. If else statement
3. Nested if else statement
4. If elif else statement
5. Switch statement

1. If Statement

 If statements are one of the most important aspects of any programming


language, and that is true in Python programming language as well.

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 1
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
 If statements allow for conditions to be set within code so that it behaves
differently depending on whether or not certain conditions are true.

Syntax

Example

Output

2. If else statement

 From the name itself, we get the clue that the if-else statement checks the
expression and executes the if block when the expression is True otherwise it
will execute the else block of code.
 The else block should be right after if block and it is executed when the
expression is False.

Syntax

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 2
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
Example

Output

3. Nested if else statement


 In very simple words, Nested if statements is an if statement inside another if
statement.
 Python allows us to stack any number of if statements inside the block of
another if statements.
 They are useful when we need to make a series of decisions.

Syntax

Example

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 3
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________

Output

4. If elif else statement

 You might have heard of the else-if statements in other languages


like C/C++ or Java.
 In Python, we have an elif keyword to chain multiple conditions one after
another. With elif ladder, we can make complex decision-making statements.
 The elif statement helps you to check multiple expressions and it executes the
code as soon as one of the conditions evaluates to True.

Syntax

Example
Follow nested if example

5. Switch Statement
 Until version 3.10, Python never had a feature that implemented what the
switch statement does in other programming languages.
 So, if you wanted to execute multiple conditional statements, you
would've had to use the elif keyword.

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 4
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
 From version 3.10 upwards, Python has implemented a switch case feature
called “structural pattern matching”.
 You can implement this feature with the match and case keywords.

Syntax

Example

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 5
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
 Loops in Python

 In computer programming, loops are used to repeat a block of code.


 For example, if we want to show a message 100 times, then we can use a loop.
It's just a simple example; you can achieve much more with loops.

There are 2 types of loops in Python:

o While loop
o For loop

1. While Loop

 In python, a while loop is used to execute a block of statements repeatedly


until a given condition is satisfied.
 And when the condition becomes false, the line immediately after the loop in
the program is executed.

Syntax& Example

# Python program to illustrate while loop

count = 0

while (count < 3):

count = count + 1

print("Hello Geek")

2. For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 6
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated
programming languages.

With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.

Syntax

Example
# Python program to illustrate
# Iterating over range 0 to n-1

n =4
for i in range(0, n):
print(i)

Output
0

Try following more example of for loop

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 7
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________

Break statement

Continue statement

Range statement

for x in range(6):

print(x)

#0 1 2 3 4 5 6

for x in range(2, 6):

print(x)

#2345

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

print(x)

#258

_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 8

You might also like