Python Looping:
-------------------
-Set of statement is repeated until condition is false
There are Two Types looping statements in Python
1. for loop
2. while loop - not support do while
1).for loop
-----------
- Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
Syntax:
-------
for <variable> in <sequence>:
Statements #Body
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2). while loop:
---------------
-Repeats a statement or group of statements while a given condition is TRUE.
Syntax:
-------
while <expression>:
Statements #Body
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nested Loops: (patter design ,table format)
-------------
i. Nested for loop
ii. Nested while loop
1. Nested for loop
-------------------
-for Loops defined within another Loop is called Nested for Loop.
-When an outer for loop contains an inner for loop in its body it is called Nested for Looping.
Syntax:
-------
for <expression>:
for <expression>:
Body #inner loop Statements
Body #Outer loop Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. Nested While Loop
--------------------
-while Loops defined within another Loop is called Nested while Loop.
Syntax:
-------
while <expression>:
while <expression>:
Body #inner loop Statements
Body #Outer loop Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Loop Control Statements
~~~~~~~~~~~~~~~~~~~~~~~~
1. break
2. continue
3. pass
1. break statement
---------------
-Terminates the loop statement and transfers execution to the statement
immediately following these loop.
Syntax:
------
while <expression>:
Statements #Body
if (cond) :
break
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. continue statement:
----------------------
-Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
Syntax:
-------
while <expression>:
Statements #Body
if (cond) :
continue
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. Pass statement
----------------
- In Python, pass keyword is used to execute nothing; it means, when we don't want to execute code,
the pass can be used to execute empty. It is same as the name refers to.
It just makes the control to pass by without executing any code.
If we want to bypass any code pass statement can be used.
Syntax:
------
for <variable> in <sequence>:
Statements #Body
if (cond) :
pass
Ex:
for i in range(1,5):
if i==3:
pass
print "This is pass block"
print i
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------
-Set of statement is repeated until condition is false
There are Two Types looping statements in Python
1. for loop
2. while loop - not support do while
1).for loop
-----------
- Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
Syntax:
-------
for <variable> in <sequence>:
Statements #Body
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2). while loop:
---------------
-Repeats a statement or group of statements while a given condition is TRUE.
Syntax:
-------
while <expression>:
Statements #Body
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nested Loops: (patter design ,table format)
-------------
i. Nested for loop
ii. Nested while loop
1. Nested for loop
-------------------
-for Loops defined within another Loop is called Nested for Loop.
-When an outer for loop contains an inner for loop in its body it is called Nested for Looping.
Syntax:
-------
for <expression>:
for <expression>:
Body #inner loop Statements
Body #Outer loop Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. Nested While Loop
--------------------
-while Loops defined within another Loop is called Nested while Loop.
Syntax:
-------
while <expression>:
while <expression>:
Body #inner loop Statements
Body #Outer loop Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Loop Control Statements
~~~~~~~~~~~~~~~~~~~~~~~~
1. break
2. continue
3. pass
1. break statement
---------------
-Terminates the loop statement and transfers execution to the statement
immediately following these loop.
Syntax:
------
while <expression>:
Statements #Body
if (cond) :
break
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. continue statement:
----------------------
-Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
Syntax:
-------
while <expression>:
Statements #Body
if (cond) :
continue
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. Pass statement
----------------
- In Python, pass keyword is used to execute nothing; it means, when we don't want to execute code,
the pass can be used to execute empty. It is same as the name refers to.
It just makes the control to pass by without executing any code.
If we want to bypass any code pass statement can be used.
Syntax:
------
for <variable> in <sequence>:
Statements #Body
if (cond) :
pass
Ex:
for i in range(1,5):
if i==3:
pass
print "This is pass block"
print i
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment