Control Structures
Control Structures
Flow of control through any given program is implemented with three basic
types of control structures: Sequential, Selection and Repetition.
Note 2: At the end of each line, you don’t see any semicolon to
terminate the statement. And the code uses indentation to format
the code.
1. Sequential
Simple if
if-else
nested if
if-elif-else
Simple if: If statements are control flow statements that help us to run a
particular code, but only when a certain condition is met or
satisfied. A simple if only has one condition to check.
Example: 1
a=5
if(a>2):
Output:
Example 2:
a=5
if(a>2)
print("Number is greater than 2")
Output:
Error
Example 3:
a=5
if(a>2):
Output:
Error
Example 4:
a=5;
if(a>2):
Output:
Example 1:
a=5
if(a>2):
else:
Output:
Example:
a=5
b=10
if(a>2):
if(b>5):
print("a>2 and b>5")
else:
Output:
You can use any Python expression as the condition in an if or elif clause.
When
you use an expression this way, you are using it in a Boolean context. In a
Boolean context, any value is taken as either true or false.
Example:
mks=73
if(mks>=75):
print("Distinction")
print("Average")
else:
print("Poor performance")
Output:
Average
3. Repetition
for loop
while loop
for loop: A for loop is used to iterate over a sequence that is either a list,
tuple, dictionary, or a set. We can execute a set of statements once for each
item in a list, tuple, or dictionary.
Syntax:
Example 1:
for i in range(11):
print(i)
Output:
10
Example 2:
for i in range(1,11):
print(i)
Output:
9
10
Example 3:
for i in range(1,11,2):
print(i)
Output:
Example 4:
for i in range(2,21,2):
print(i)
Output:
10
12
14
16
18
20
Example 5:
colors=["red","green","blue"]
for i in colors:
print(i)
Output:
red
green
blue
Pass statement
for loops cannot be empty, but if you for some reason have a for loop with
no content, put in the pass statement to avoid getting an error.
Example:
for i in range(2,21,2):
pass
Output:
while loop: In Python, while loops are used to execute a block of statements
repeatedly until a given condition is satisfied. Then, the expression is
checked again and, if it is still true, the body is executed again. This
continues until the expression becomes false.
Syntax:
while expression:
statement(s)
First, expression, which is known as the loop condition, is evaluated. If the
condition is false, the while statement ends. If the loop condition is satisfied,
the statement or statements that comprise the loop body are executed.
When the loop body finishes executing, the loop condition is evaluated again,
to see if
another iteration should be performed. This process continues until the loop
condition is false, at which point the while statement ends.
The loop body should contain code that eventually makes the loop condition
false, or the loop will never end unless an exception is raised or the loop
body executes a break statement. A loop that is in a function’s body also
ends if a return statement executes in the loop body, as the whole function
ends in this case.
Example:
i=1
print(i)
i += 1
Output:
2
3
10
You might face a situation in which you need to exit a loop completely
when an external condition is triggered or there may also be a
situation when you want to skip a part of the loop and start next
execution.
Python provides break and continue statements to handle such situations
and to have good control on your loop.
Break
The break statement is used to terminate the loop (even if the condition is
tue) or statement in which it is present. The break statement in Python
terminates the current loop and resumes execution at the next statement,
just like the traditional break found in C.
Example:
for i in range(1,11,1):
if(i==5):
break
else:
print(i)
Output:
Continue
The continue statement in Python returns the control to the beginning of the
while loop. The continue statement rejects all the remaining statements in
the current iteration of the loop and moves the control back to the top of the
loop.
Example:
for i in range(1,11,1):
if(i==5):
continue
else:
print(i)
Output:
10