Control Statements Python
Control Statements Python
statements
in Python
SAYAN SARKA R
BWU/BTA /2 2 /1 9 7
COURSE CO DE – ESCM- 3 0 3
SEM- 3 R D
Contents
1. Definition
2. Types
3. If statement
4. If-else statement
5. If-elif-else statement
6. While loop
7. For loop
8. Break statement
9. Continue statement
What is control statement?
In Python, control statements are used to manage the flow of a program by making decisions or
repeating certain blocks of code.
Types:
o Structure of code:
if condition:
# code to execute if the condition is true
If-else statement:
o The 'if-else' statement extends the if statement by providing an alternative block of code to
execute when the condition is false.
o Structure of code:
if condition:
# code to execute if the condition is true
else:
# code to execute if the condition is false
If-elif-else statement:
o The 'if-elif-else' statement allows you to check multiple conditions and execute different
blocks of code based on the first true condition.
o Structure of code:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
else:
# code to execute if none of the conditions are true
While loop;
o The 'while' loop is used for repeated execution of a block of code as long as a specified
condition is true.
o Structure of code:
while condition:
# code to execute while the condition is true
For loop:
o The ‘for’ loop is used for iterating over a sequence (such as a list, tuple, or string) or other
iterable objects.
o Structure of code:
for variable in iterable:
# code to execute for each element in the iterable
Break statement:
o The 'break' statement is used to exit a loop prematurely, regardless of whether the loop
condition is true or false.
o Structure of code:
while condition:
# code
if some_condition:
break
# more code
Continue statement:
o The 'continue' statement is used to skip the rest of the code inside a loop for the current
iteration and continue with the next iteration.
o Structure of code:
for variable in iterable:
if some_condition:
continue
# code to execute for each element in the iterable
Reference
1. Let us Python(4th Edition)- Yashavant Kenetkar
2. Geeks for geeks (https://www.geeksforgeeks.org/)
3. Google Bard (https://bard.google.com/?hl=en-GB)
Thank You