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

Control Structures

Uploaded by

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

Control Structures

Uploaded by

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

Unit 2

control structures
• Control structure : python's control structures dictate the order in which code is
executed, enabling decision-making and repetitive actions.

Control structures

1. Selection statements
2. Iterative statements
3. Jumping statements
1. Sequential Control:

• Statements are executed in the order they appear from top to


bottom. This is the default flow of a Python program.
• Ex:
• # Sequential execution
print("First statement")
x = 10
print(f"Value of x: {x}")
print("Last statement")
2. Selection (Conditional)
Control:
• Code blocks are executed based on whether a condition is true or false . It has 3 types
I.) if statement: Executes a block of code if a condition is true.
Syntax:
if ( condition):
…………………..
…………………..

Ex : age = 18
if age >= 18:
print("You are an adult.")
Indentation:
• Indentation in Python is used to define blocks of code.
• Indentation is achieved using whitespace (spaces or tabs) at the
beginning of each line.
if condition:
# Indented block of code
statement1
statement2
else:
# Indented block of code
statement3
statement4
Cont…
II.) elif statement: Executes one block if the condition is true and
another if it's false.
Syntax:
if ( condition):
…………………..
elif :
…………………..

Ex:
temperature = 25
if temperature > 30:
print("It's hot outside.")
elif :
print("It's not too hot.")
if-elif-else
Syntax:
if ( condition-1):
…………………..
…………………..
elif (condition-2):
…………………..
…………………..

else:
…………………..
…………………..

• else: Allows checking multiple conditions sequentially

• score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")
3. Repetition (Looping) Control:

• Code blocks are executed repeatedly until a certain condition is met or for a
specific number of iterations.
• for loop :iterates over a sequence(e.g list ,string, tuple)

• Syntax:
for variable_name in range(stop):
# Code to be executed in each iteration
range(stop)
Ex : for i in range(10)
range(start , stop)
Ex : for I in range(1,10)
range(start ,stop , step)
Ex: for I in range(10,0,-1)
Ex:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

for i in range(5): # Iterates from 0 to 4


print(i)
•while loop: Executes a block of code as long as a condition
remains true.
.…… .

Ex:
count=0
while count<3:
print(count)
count+=1
Loop control statements
1.)break: Terminates the current loop entirely
for i in range(10):
if i == 5:
break
print(i)
0/p:
0
1
2
3
4
2.)continue:
• Skips the rest of the current iteration and proceeds to the next iteration of the loop.
Ex:
for i in range(10):
if i % 2 == 0:
continue
print(i)
o/p:
1
3
5
7
9
Pass:
• It 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.

for i in range(5):
if i == 3:
pass
print(i)
o/p:
0
1
2
3
4

You might also like