Skip to main content

Python Loops

Python Loops.
In programming, loops are sequence of instructions that does a specific set of instructions or tasks based on some conditions and continue the tasks until it reach certain conditions.
It is seen that in programming, sometimes we need to write a set of instructions repeatedly – which is a tedious task and the processing also takes time. So in programming we use iteration technique to repeat the same or similar type of tasks based on the specified condition.
Statements are executed sequentially; but there sometimes occur such cases where programmers need to execute a block of code several times. The control structures of programming languages allow us to execute a statement or block of statement repeatedly.

Types Of Loops In Python

Python provides three types of looping techniques:
Python Loops
LoopDescription
for LoopThis is traditionally used when programmers have a piece of code and wanted to repeat that ‘n’ number of times.
while LoopThe loop gets repeated until certain Boolean condition is met.
Nested LoopsProgrammers can use one loop inside another; i.e. they can use for loop inside while or vice – versa or for loop inside for loop or while inside while.

for Loop

The graphical representation of the logic behind for looping is shown below:
Figure – for loop Flowchart:
for loop flowchart
Syntax:
for iterating_var in sequence:
//execute your code
Example 01:
#!/usr/bin/python

for x in range (0,3) :
print 'Loop execution %d' % (x)
Output:
Loop execution 0
Loop execution 1
Loop execution 2
Example 02:
#!/usr/bin/python

for letter in 'TutorialsCloud':
print 'Current letter is:', letter
Output:
Current letter is : T
Current letter is : u
Current letter is : t
Current letter is : o
Current letter is : r
Current letter is : i
Current letter is : a
Current letter is : l
Current letter is : s
Current letter is : C
Current letter is : l
Current letter is : o
Current letter is : u
Current letter is : d

while Loop

The graphical representation of the logic behind while looping is shown below:
Figure – while loop Flowchart:
while loop flowchart
Syntax:
while expression:
//execute your code
Example:
#!/usr/bin/python

#initialize count variable to 1
count
=1

while count < 6 :
print count
count
+=1
#the above line means count = count + 1
Output:
1
2
3
4
5

Nested Loops

Syntax:
for iterating_var in sequence:
for iterating_var in sequence:
//execute your code
//execute your code
Example:
for g in range(1, 6):
for k in range(1, 3):
print '%d * %d = %d' % ( g, k, g*k)
Output:
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10

Loop Control Statements

These statements are used to change execution from its normal sequence.
Python supports three types of loop control statements:
Python Loop Control Statements
Control StatementsDescription
Break statementIt is used to exit a while loop or a for loop. It terminates the looping & transfers execution to the statement next to the loop.
Continue statementIt causes the looping to skip the rest part of its body & start re-testing its condition.
Pass statementIt is used in Python to when a statement is required syntactically and programmer do not want to execute any code block or command.

Break statement

Syntax:
break
Example:
#!/usr/bin/python

count
= 0

while count <= 100: print (count) count += 1 if count >= 3 :
break
Output:
0
1
2

Continue statement

Syntax:
continue
Example:
#!/usr/bin/python

for x in range(10):
#check whether x is even
if x % 2 == 0:
continue
print x
Output:
1
3
5
7
9

Pass Statement

Syntax:
pass
Example:
#!/usr/bin/python

for letter in 'TutorialsCloud':
if letter == 'C':
pass
print 'Pass block'
print 'Current letter is:', letter
Output:
Current letter is : T
Current letter is : u
Current letter is : t
Current letter is : o
Current letter is : r
Current letter is : i
Current letter is : a
Current letter is : l
Current letter is : s
Pass block
Current letter is : C
Current letter is : l
Current letter is : o
Current letter is : u
Current letter is : d

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...