Control Structure
According to the structure theorem, any computer program can be written
using the basic control structures . A control structure (or flow of
control) is a block of programming that analyses variables and
chooses a direction in which to go based on given parameters.
In simple sentence, a control structure is just a decision that the
computer makes. So, it is the basic decision-making process in
programming and flow of control determines how a computer
program will respond when given certain conditions and
parameters.
There are two basic aspects of computer
programming: data and instructions . To work with data, you need to
understand variables and data types; to work with instructions, you need to
understand control structures and statements.
Flow of control through any given program is implemented with three basic
types of control structures: Sequential, Selection and Repetition.
Whitespace and indentation
While working in programming languages such as Java, C#, or C/C++, you
know that these languages use semicolons (;) to separate the statements.
Python, however, uses whitespace and indentation to construct the
code structure.
Note 1: The number of spaces is up to you as a programmer, the
most common use is four, but it has to be at least one.
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.
By using indentation and whitespace to organize the code, Python code
gains the following advantages:
First, you’ll never miss the beginning or ending code of a block
like in other programming languages such as Java or C#.
Second, the coding style is essentially uniform. If you have to
maintain another developer’s code, that code looks the same as yours.
Third, the code is more readable and clear in comparison with
other programming languages.
A program’s control flow is the order in which the program’s code
executes.
The control flow of a Python program is regulated by conditional statements,
loops, and function calls.
Python has three types of control structures:
Sequential - default mode
Selection - used for decisions and branching
Repetition - used for looping, i.e., repeating a piece of code multiple
times.
1. Sequential
Sequential statements are a set of statements whose execution process
happens in a sequence. The problem with sequential statements is that if
the logic has broken in any one of the lines, then the complete source code
execution will break.
2. Selection/Decision control statements
In Python, the selection statements are also known as Decision control
statements or branching statements.
The selection statement allows a program to test several conditions
and execute instructions based on which condition is true.
Some Decision Control Statements are:
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):
print("Number is greater than 2")
Output:
Number is greater than 2
Example 2:
a=5
if(a>2)
print("Number is greater than 2")
Output:
Error
Example 3:
a=5
if(a>2):
print("Number is greater than 2")
Output:
Error
Example 4:
a=5;
if(a>2):
print("Number is greater than 2");
Output:
Number is greater than 2
if-else: The if-else statement evaluates the condition and will
execute the body of if if the test condition is True, but if the
condition is False, then the body of else is executed.
Example 1:
a=5
if(a>2):
print("Number is greater than 2")
else:
print("Number is less than 2")
Output:
Number is greater than 5
nested if: Nested if statements are an if statement inside another if
statement.
Example:
a=5
b=10
if(a>2):
if(b>5):
print("a>2 and b>5")
else:
print("a>2 but b<5")
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.
if-elif-else: The if-elif-else statement is used to conditionally execute a
statement or a block of statements.
Example:
mks=73
if(mks>=75):
print("Distinction")
elif(mks>50 and mks<75):
print("Average")
else:
print("Poor performance")
Output:
Average
3. Repetition
A repetition statement is used to repeat a group(block) of
programming instructions.
In Python, we generally have two loops/repetitive statements:
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:
for iterator_var in sequence:
statements(s)
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
print("For Loop Finished")
Output:
For Loop Finished
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
while i < 11:
print(i)
i += 1
Output:
2
3
10
Break and continue
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