Python WHILE Loop & FOR Loop Control Flow Statements

Python while loop and for loop control flow statements

In this article we are going through Loop control flow statements in python. It is almost same as other programming language such as C, C++ or java etc.

Sometimes you must take control on program execution to run same set of code in a loop to get the desired output. To control the program execution, we have special utilities or statements called as “Control flow statements” 

The loop statements while or for allow us to execute a statement(s) over and over. A loop is controlled by a boolean expression that determines how many time statements can be executed.

Python has two primitive loop commands:
           1. While – loop
           2. For – loop

1. WHILE Loop

While condition is true keep running the loop and exit as soon as the condition becomes false. The conditional while loop syntax is the same as for if-else and elif statements.

Syntax:

while (conditional test):

    <statement1>

    <statement2>

    . . . .

    <last statement>

Example:

Python while loop statement code
Output:

Python while loop statement output

2. For Loop

Another loop statement – for, is best for when you determine in advance how many times you need to execute the block of statements placed in the loop.

It allows you to perform an operation on each element in a list or character in a string.

Syntax:

for in :

   

   

   


There are two types of for loops in python, first one is List – for loop and second one is          Range – for loop.

2.1 List – for loop

It’s very simplest method, all you need to do to specify complete list of items and it will be available in the loop.

Example:

python for loop statement code

Output:

python for loop statement output

Let’s take one more example-

for integer in [0, 1, 2]:

    print (“Integer :” + str(integer))

    print (“Integer Multplication :”+ str(integer * integer))

Output:

Integer :0

Integer Multplication :0

Integer :1

Integer Multplication :1

Integer :2

Integer Multplication :4

2.2 Range – for loop

For loop with range, gives you flexibility to use the appropriate range of values and important thing is you can control that range using available options along with for.

Syntax:

for var in range (start_pos, end_pos, steps):

    statement(s)

  • start_pos: Start position value should be excluded from range value when you specify value for steps otherwise it can be counted.
  • end_pos: End position must be excluded from range value.
  • steps: Steps value controls the range values.

Example:

python for loop range code
python for loop range output

Few more examples which will clear all your doubts if you have any!

Ex. 1

for i in range (1, 20, 2):

    print (“Range value :” + str(i))

Output:

Range value :1

Range value :3

Range value :5

Range value :7

Range value :9

Range value :11

Range value :13

Range value :15

Range value :17

Range value :19

Ex. 2

for i in range (20, 0, -2):

    print (“Range value :” + str(i))

Output:

Range value :20

Range value :18

Range value :16

Range value :14

Range value :12

Range value :10

Range value :8

Range value :6

Range value :4

Range value :2

 

Read More: Python break, continue and pass statements

Leave a Comment