Python Loops
There may be a situation when you need to execute a block of code several number of times. In g eneral,
statements are executed sequentially: T he first statement in a function is executed first, followed by the second,
and so on.
Prog ramming lang uag es provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or g roup of statements multiple times and following is the
g eneral form of a loop statement in most of the prog ramming lang uag es:
Python prog ramming lang uag e provides following types of loops to handle looping requirements. Click the
following links to check their detail.
Loop T ype
Desc ription
while loop
Repeats a statement or g roup of statements while a g iven condition is true.
It tests the condition before executing the loop body.
for loop
Executes a sequence of statements multiple times and abbreviates the code
that manag es the loop variable.
nested loops
You can use one or more loop inside any another while, for or do..while
loop.
Loop Control Statements:
Loop control statements chang e execution from its normal sequence. When execution leaves a scope, all
automatic objects that were created in that scope are destroyed.
Python supports the following control statements. Click the following links to check their detail.
Control Statement
Desc ription
break statement
T erminates the loop statement and transfers execution to the statement
immediately following the loop.
continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating .
pass statement
T he pass statement in Python is used when a statement is required
syntactically but you do not want any command or code to execute.
Comments
Post a Comment