C - Loops



In computer programming, the term “loop” is used to denote a block of one or more statements that are repeatedly executed a specified number of times, or a certain desired condition is reached. In C, the keywords - while, do – while and for – are provided to implement loops. Looping constructs are an important part of any processing logic, as they help in performing the same process again and again. A C programmer should be well acquainted with implementing and controlling the looping construct.

Loops are a programming construct that makes a portion of code repeat a certain number of times or until the desired condition is met. Repetitive tasks are common in programming, and loops are essential to save time and minimize errors.

Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Given below is the flowchart of a loop statement in most of the programming languages −

Loop Architecture

The statements in a C program are always executed in a top to bottom manner. If we ask the compiler to go back to any of the earlier steps, it constitutes a loop.

Example

To understand the need of loops in a program, consider the following snippet:

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int a = 1;
   
   printf("a: %d\n", a);
   a++;
   
   printf("a: %d\n", a);
   a++;
   
   printf("a: %d\n", a);
   a++;
   
   printf("a: %d\n", a);
   a++;
   
   printf("a: %d\n", a);
 
   return 0;
}

Output

a: 1
a: 2
a: 3
a: 4
a: 5

The program prints the value of a, and increments it value. These two steps are repeated a number of times. If you need to print the value of a from 1 to 100, it is not desirable to manually repeat these steps in the code. Instead, we can ask the compiler to repeatedly execute these two steps of printing and incrementing till it reaches 100.

Example

You can use for, while or do-while keywords to do this. The program to print 100 values of a with while keyword is as follows −

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int a = 1;
   
   while (a<=100)
   {
      printf("a: %d\n", a);
      a++;
   }
 
   return 0;
}

Output

a: 1
a: 2
a: 3
a: 4
a: 5
a: 6
a: 7
a: 8
a: 9
a: 10
.....
a: 100

If a step redirects the program flow to any of the earlier steps, based on any condition, the loop is a conditional loop. The repetitions will stop as soon as the controlling condition turns false. If the redirection is done without any condition, it is an infinite loop, as the code block repeats forever.

An infinite or endless loop is a loop that repeats indefinitely because it has no terminating condition, , or the termination condition is never met or the loop is instructed to start over from the beginning. Although it is possible for a programmer to intentionally use an infinite loop, they are often mistakes made by new programmers.

To constitute a loop, the following elements are

  • Looping statement (while, do – while or for)
  • Looping block
  • Looping condition

The loops are generally of two types −

  • Counted loops
  • Conditional loops

If the loop is designed to repeat for a certain number of times, it is a counted loop. In C, for loop is an example of counted loop. If the loop is designed to repeat till a condition is true, it is a conditional loop. The while and do – while keywords help you to form conditional loops.

Looping statements

C programming language provides the following types of loops to handle looping requirements.

Sr.No. Loop Type & Description
1 while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

2 for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3 do...while loop

It is more like a while statement, except that it tests the condition at the end of the loop body.

4 nested loops

You can use one or more loops inside any other while, for, or do..while loop.

Each of the above loop types have to be employed depending upon which one is right for the given situation. We shall learn about these loop types in detail in the subsequent chapters.

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

C supports the following control statements.

Sr.No. Control Statement & Description
1 break statement

Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

2 continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

3 goto statement

Transfers control to the labeled statement.

The break statement and continue statements are having contrasting purpose. The goto statement acts as a jump statement if it causes the program to go to a latter statement, on the other hand it forms a loop it redirects the program to an earlier statement.

The Infinite Loop

A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty.

#include <stdio.h>
 
int main () {

   for( ; ; ) {
      printf("This loop will run forever.\n");
   }

   return 0;
}

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.

NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.

Advertisements