CONTROL STRUCTURES IN PYTHON
BY
S.AKSHAYA
23211A05U0
CSE-E
SELECTION
STATEMENTS
CONTROL
LOOP STATEMENTS
STRUCTURES
UNCONDITIONAL
STATEMENTS
SELECTION STATEMENTS
• Selection statements also known as conditional statements.
• Selection statements are used to make decisions and execute different blocks of code based on
certain conditions.
• It is divided as follows:
Selection
statements
If If-else if-elif-else Nested if-else
IF STATEMENT
• Syntax
if condition:
# Code block to execute if condition is true
IF-ELSE STATEMENT
• Syntax:
if condition:
# Code block to execute if condition is true
else:
# Code block to execute if condition is false
IF-ELIF-ELSE STATEMENT
• Syntax:
if condition1:
# code block to execute if condition1 is True
elif condition2:
# code block to execute if condition1 is False and condition2 is True
else:
# code block to execute if both condition1 and condition2 are False
NESTED IF ELSE
• Syntax:
LOOP STATEMENTS
• In Python, loop statements allow you to execute a block of code repeatedly.
• They help in automating tasks that need to be performed multiple times.
Loop
Statements
for while Nested for
FOR LOOP
• It iterates over a sequence (like a list or a range) or any other iterable object. The loop continues
until the sequence is exhausted.
• Syntax:
WHILE LOOP
• It repeatedly executes a block of code as long as a specified condition is true.
• It's useful when you don't know in advance how many times the loop will need to run.
• Syntax:
NESTED FOR
• Nested for loops in Python allow you to iterate over multiple sequences within each other. This is
useful when you need to iterate over every combination of elements from two or more
sequences.
• Syntax:
UNCONDITIONAL STATEMENTS
• Unconditional statements are those that will always execute regardless of any conditions.
• They are used with loop conditions.
Unconditional
statements
continue break pass
CONTINUE STATEMENT
• The continue statement is used to skip the rest of the code block within a loop for the current
iteration and proceed to the next iteration.
• It's typically used when you want to skip certain iterations based on a condition.
PASS STATEMENT
• The pass statement is a null operation; nothing happens when it executes.
• It's typically used as a placeholder when syntactically a statement is required, but no action needs
to be taken.
BREAK STATEMENT
• The break statement is used to terminate the loop prematurely.
• It’s typically used when a condition is met, and you want to exit the loop immediately.