Top
Download PDF

An Introduction to For, While and Do While Loops in Programming

In computer programming, a loop is used to execute a group of instructions or a block of code multiple times, without writing it repeatedly. The block of code is executed based on a certain condition. Loops are the control structures of a program. Using loops in computer programs simplifies rather optimizes the process of coding. Kids may come across the question ‘what is loop’ when they begin to learn computer programming languages like C or Java. Well, to understand ‘what is loop’ they will have to learn about the structure of various loops in detail.


The structure of a loop can be virtually divided into two parts, namely the control statement, and the body. The control statement of a loop comprises the conditions that have to be met for the execution of the body of the loop. For every iteration of the loop, the conditions in the control statement have to be true. The body of a loop comprises the block of code or the sequence of logical statements that are to be executed multiple times. There are two types of loops in Python, namely, For loop, and While loop. When a loop is written within another loop, the control structure is termed as a nested loop.


Therefore, when you use a loop in your program, you will not have to write the block of code (written in the body of the loop), over and over again in it. The block of code will be executed as many times as the control statement will hold true and the loop will be terminated when the conditions in the control statement become false. If the conditions are not clearly defined in the control statement, the loop will keep on executing. Such loops are termed as infinite loops. If no termination condition is provided in the control statement of a loop, then it automatically becomes an infinite loop.


Below is a detailed discussion of ‘what is loop’, and the various types of loops in Python. Hence, going through the below information will help kids to understand the concepts of loops in computer programming.


Types of Loops

The concept of ‘what is loop’ will be clearly understood when you get an idea of the syntax and function of various types of loops. There are basically two types of loops in most computer programming languages, namely, entry controlled loops and exit controlled loops.


Entry Controlled Loop

In an entry controlled loop, the control statement is written right at the beginning of the loop. This type of loop is also called a pre-checking loop. The conditions in the control statements are checked at first, and only if the conditions are true, the body of the loop is executed. If the condition turns out to be false, the lines of code in the body of the loop will not be executed.


Exit Controlled Loop

In an exit controlled loop, the control statement is written at the end of the loop structure. The lines of codes in the body of the loop are executed once before the condition is checked. Hence, this type of loop is also called a post-checking loop.   


FOR loop is an entry controlled loop, that is, the control statements are written at the beginning of the loop structure, whereas, do-while loop is an exit controlled loop, that is, the control statements are written at the end of the loop structure. 


For Loops

As discussed above, a For loop is an entry controlled loop. The general syntax of a For loop is given below.

for(initialization; condition; incrementation or decrementation)

{

Body of loop;

}

The variables required for the control statements can be initialized in the For loop itself. This variable initialized in the For loop is called the counter and is incremented or decremented with every iteration of the loop. The condition is a boolean statement that compares the value of the counter to a fixed value, at every iteration, and terminates the loop when the condition is not satisfied. The increment or decrement value is set in the loop.

An example of For loop is given below.

for(int i=1; i<10; i++)

{

print (i);

}

The above For loop will print the natural numbers 1 to 10 when executed. The variable ‘i’ is of integer type, and the condition will check if the value of ‘i’ is less than 10, at each iteration. After executing the body of the loop, the value of ‘i’ is incremented by 1, before the next iteration. In this way, the natural numbers 1 to 10 are displayed on the screen, on executing this loop.


While Loop

A while loop is an entry controlled loop. The condition checking is done at the beginning of the loop structure. The general syntax of the while loop is given below.

while(condition)

{

Body of the loop;

}


The condition checking is done before the execution of the body of the while loop. The block of code in the body of the While loop is executed only if the condition is true. The body of the loop gets executed as many times as the condition is true. After each iteration of the loop, the control moves back to the condition checking part at the beginning of the While loop. If the condition is not met, that is, if the boolean expression in the braces (), turns out to be false, the while loop is terminated.

An example of the While loop is given below.

int n=10;

while(n>0)   

{

print (n);

n--;

}


In the above example, the variable ‘n’ is initialized as an integer, and its value is assigned as 10. Every time the condition ‘n>0’ is met the While loop will be executed, and the value of n will be displayed on the screen. At every iteration, the value of n will be decreased by 1. The loop will be terminated when the value of ‘n’ becomes less than 1. The above While loop will display the numbers from 10 to 1.


Do - While Loop

A do-while loop is an exit controlled loop. The syntax of the do-while loop is similar to that of the while loop, with the exception of the condition checking. The condition is always checked at the end of the do-while loop. The general syntax of the do-while loop is given below.


do

{

Body of the loop;

} while(condition);


Unlike the entry controlled loops, the body of the do-while loop is executed before the condition is checked. Even if the condition is not true, the body of the loop will be executed for once. If the condition given in the braces () is true, the control is again moved back to the body of the loop. If the condition is false, the control is moved out of the loop and the loop is terminated.

An example of the do-while loop is given below.


int i=10;

do

{

print (i);

i--;

} while(n>0);


The above do-while loop will display the numbers 10 to 1 when executed. Now, consider the following example.

int i=0;

do

{

print (i);

i--;

} while(n>0);


The condition ‘n>0’ turns out to be false at the very first iteration of the above-given do-while loop. Yet, it will be executed once, and 0 will be displayed on the screen. Unlike the other loops, the do-while loop ends with the condition checking expression followed by a semicolon ‘;’.


Loops in Python

The commonly used loops in Python are For and While loops. The syntax and functions of the For and While loops in Python are the same as discussed above. When a For loop or a While loop is written within another loop, the structure is called a nested loop. For example,


for(int i=0; i<7;i++)

{

   for (int j=8; j>4; j--)

  {

      print (i);

      print (j);

}

}


The print statements in the above-given nested loop will be executed only when the conditions in both the loops are satisfied. Also, if there is only one line of code to be written in the body of a loop, it is not mandatory to put the brackets for it.


Example:

for(int n=5; n<0; n--)

print (n);


Some of the control statements supported in Python are ‘break’, ‘continue’, and ‘pass’. When a ‘break’ statement is encountered in a loop, the loop is terminated immediately, and the control moves to the code followed by the loop. When a ‘continue’ statement is encountered in a loop, the control is transferred to the condition checking part and the rest of the code in the body of the loop is skipped. The ‘pass’ statement in Python is a null statement. It is quite similar to a commented code, however, unlike the commented code, a pass statement is not ignored by the interpreter. For example, if we want to execute a block of code or any function at a later point in time, we can use the ‘pass’ statement for it. The block of code will not be executed when the ‘pass’ statement is executed. The pass statement has a result of no operation when executed.


Since loops make an important part of the Python programming, kids should learn the concepts of loops thoroughly, to write advanced programs. Good knowledge of loops will come in handy when kids will write programs to design fun interactive games in Python as well.

Share this with your friends
SHARE
TWEET
SHARE
SUBSCRIBE