Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

[Ultimate Guide] What are loops in programming and how are they important?

Welcome everyone to yet another tutorial on C Programming language. In this tutorial we'll be learning about a great concept of loops. Most of the programming languages would use exactly the same syntax for loops, so understand this carefully. Loops provide with great utility and power and saves from a lot of overhead which may be there without them.

In this tutorial, we'll be discussing about various topics which revolves around concept of loops and understand them. They are :

Understand about Loops in Programming, for, while, do while


Basic Definition and Types of Loops

The basic concept behind "Loop" is to execute certain set of statements as far as a given condition is true, or in many cases a counter variable gets out of a specific range defined, or we use may use some Logical operators to control the flow in our loop. There are basically three types of loops in the C language. They are :

  • for loop
  • while loop
  • do while loop

Now, let's look at the syntax of each of these and how they can be used, in detail. To understand all these in a better way, let's consider a common and simple example to print numbers from 1 to let's say 500.

The most basic approach to carry out this task is to keep printing each line having different numbers from 1 to 500 which would look something like this :

#include<stdio.h>
void main()
{
  printf("1 n");
  printf("2 n");
  printf("3 n");
  printf("4 n");
  printf("5 n");
  printf("6 n");
  printf("7 n");
  . 
  .
  .
  .
  .
  .
  .
  printf("499 n");
  printf("500 n");
}

Now, let's try to improve upon this a little bit.
We can take a counter variable and after each step just update it's value, then it would be something like this :

#include<stdio.h>
void main()
{
  int counter = 1;
  printf("%d n",counter);
  counter++;
  printf("%d n",counter);
  counter++;
  printf("%d n",counter);
  counter++;
  printf("%d n",counter);
  counter++;
  .
  .
  .
  .
  .
  .
  .
  printf("%d n",counter);    /* Carry on this 
  counter++;                      500 times   */
}

In this simple approach although we used a counter variable to avoid writing different numbers and updating the value of counter is left to the computer. But, it lead to an increase in the Lines Of Code (LOC) which is considered as an important parameter to compare any software.

So, it would be good, if we could use this counter variable approach but reduce the LOC. We can do it by using loops, now let's carry this out using different loops.

Basic sections of the loops

All loops have three basic sections which are :

  • Initialization : we need to initialize our counter variable in this statement.
  • Test Condition : is the condition which needs to be true for the loop to keep running, as soon as it becomes false, the loop would stop executing, and the control would be directed to the next statement.
  • Updation : In this statement the value of counter variable is updated (either incremented or decremented )

For, While and Do While Loops :

Now, we'll look here how we can print numbers from 1 to 500 using for, while and do-while loops by first understanding their syntax and then understanding the flow of control of these loops.

Understanding and using for loop

Syntax :*

for ( initialization ; test condition ; updation )
{
  .
  .
  .
}

Now let's carry out the given task to print numbers from 1 to 500.

The for loop for this would be something like this, here we have a counter variable named i which would be incremented from 1 to 500.

for( i = 1 ; i<=500 ; i++ )
{
  printf("%d n", i );
}

We can also define the counter variable inside the loop, and then it would havescope of the loop as explained in tutorial on Scope and lifetime of variables.

for(int i = 1 ; i<=500 ; i++ )
{
  printf("%d n", i );
}

Understanding flow of  for loop

First time, when the control enters the loop, value of counter i is 1. The test condition would be checked, it would be true in this case, as 1 is less than or equal to 500. Hence control enters the loop and the statement to print value of i would be executed. We don't have more statements here, so control would go to updation, and update the value of i, it would be incremented.

Next the test condition would be again checked, here value of i is 2. So, test condition would be true in this case. Hence, control enters the loop and again print statement would be executed. This time value 2 would be printed. Once again, the control goes to updation statement, and value of i is incremented. It becomes 3 now, and again test condition would be checked.

This would continue till the value reaches 499, after updation, the value of i would be 500. The control again enters the loop, and value of 500 would be printed. Next updation takes place and value of i becomes 501, the test condition here becomes false, because, 501 is not less than or equal to 500. So, the loop would terminate, and the control would go to the next immediate next statement following the loop.

Understanding and Using while loop

Syntax:

initialization
while(test_condition)
{
 //all code
  updation 
}

In while loop, initialization is done even before control enters the loop, as shown. Updation is done as the last statement of the loop and test condition is given as shown.

Now let us print the numbers from 1 to 500 using while loop.

int i = 1;
while( i <= 500)
{
  printf("%d n",i);
  i++ ;
}

The initialization is need is just once, whatever loop it is, so in while loop, we have done initialization, before entering the while loop.

Understanding flow of  while loop

First of all the test condition is checked, which would be true, since 1 is less than or equal to 500, so control enters loop, and the print statement would be executed and updation statement also takes place. There is no other statement to be executed as end of the loop is reached. Before the control leaves the loop, the test condition would be checked, which would be again true as 2 is less than equal to 500.

This would continue until value is 500. Next, the value would be printed, and value is incremented by 1. So, condition is tested again, now, 501 would not be less than or equal to 500. Hence, the test condition becomes false and the control exits the loop executing the immediate next statement following the loop.

Understanding and Using do - while loop

Syntax:

initialization
do {
  //executing statements
  updation
} while(test condition);

Notice that in do while loop, we have a semi-colon at the end of while statement.

Let's now print the numbers from 1 to 500 using do while loop.

int i = 1;
do {
     printf("%d n",i);
     i++;
} while(i <= 500);

Understanding flow of do while loop

The do while loop has an interesting feature, which is it would execute at least once whether the condition is true or not.

In the above case, the control enters the loop, and execute the statements. So, the value 1 would be printed. Next updation is done in the value of i, and the condition is checked against value 2, so 2 is less than or equal to 500 and hence the control once again start executing from the first statement. Next the test condition is checked against the value of 3 and so on.

This would continue till value of 500, next after printing value of 500, the updation takes place, and then the condition would become false. The control would get out of the loop and be ready to execute the immediate next statement.

Difference between Entry Controlled and Exit Controlled loop

Entry Controlled loops are those in which the test condition is checked first, and then if the condition is true, the control enters the loop and all the statements inside the loops are executed. For example, for and while loop are entry controlled loop.

Exit Controlled loops are those in which the statements inside loops are executed first, and then the condition is tested. Even if the condition is false, the loop is executed at least once. For example, do-while loop is an exit controlled loop and it is executed at least once, whether the condition is true of false. If the condition is true, the loop is executed one more time, and if it's false, termination occurs, as we have discussed in detail above.

In the next tutorial, we'll learn about structures in the C language.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy