Control
structures in Python
Control
structures control the direction of flow of the program. It determines how a
computer will respond when given certain conditions and parameters. The three
types of control structures in Python are Sequence, Selection (Branching), and
Repetition (Looping / Iteration) Structures.
Sequence
Structure:
The
sequence structure is the default structure. It contains a sequence of
statements placed one after the other and is executed in the same order.
Selection
Structure:
Selection
structure is used for choosing between two or more alternative blocks of code.
It is a binary decision based on whether an expression (also called as test
condition) evaluates to TRUE or FALSE. Given two alternative blocks, if the
test condition evaluates to true, one of the two branches is explored; if the
condition evaluates to false, the other alternative is taken. The commonly used
selection structures in Python are ‘if, ‘if – else’ and ‘if – elif – else’
if
structure: if the condition evaluates to true, the statements in the block will
be executed. If the condition evaluates to false the statements will not be
executed. Syntax:
if (test
condition) :
(statements)
if
– else structure: Binary condition where only one block of statements will be
executed. If the condition evaluates to true, the statements in the if-block
will be executed. If it evaluates to false, the statements in the else block
will be executed. Syntax:
if (test condition)
:
(statements)
else:
(other
statements)
if
– elif – else Structure: By using if-elif-else,
it is possible to combine several conditions. Only the statements following the
first condition that is found to be true will be executed. All other statements
will be skipped. Syntax:
if
(test condition 1) :
(statements)
elif
(test condition 2) :
(statements)
elif
(test condition 3) :
(statements)
...
else:
(statements)
Repetition
Structure:
Repetition
structure is used for looping, i.e. repeating a block of code multiple times in
a row. It is a construct where statements can be executed repeatedly until a
condition evaluates to TRUE or FALSE. It is represented by the ‘while’ and
‘for’ constructs in Python.
while
structure: The while construct consists of a block of code and a test
condition. The test condition is evaluated, and if it evaluates to true, the
code within the block is executed. This repeats until the test condition
becomes false. Because the while loop checks the test
condition before the block is executed, the while control structure is often
also known as an entry-controlled loop. If the test condition evaluates to
false the first time, the statement block may never be executed. Syntax:
while
(test-condition) :
// Statement
block
for
structure: While loop is used in situations where the number of times the loop
is to be executed is unknown. For structure in contrast is used in situation
where number of times the loop is to executed is deterministic. Syntax .
for <variable> in
sequence :
// statement block
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu