How to use loops in C


A statement or set of statements that is executed repeatedly is known as loop. The structure that repeats a statement is known as repetitive, iterative, or looping construct. Loops are basically used for two purposes that are: to execute a statement or number of statements for a specified number of times for example, a user may display his name on screen 10 times and to use a sequence of values e.g. a user may display a set of natural numbers from 1 to 10.

In C programming language we have three types of loops:

  1. While loop
  2. For loop
  3. Do-while loop


While loop:

It is the simplest loop of C programming language. This loop executes one or more statements while the given condition remains true. It is useful where the number of iterations is not known in advance.

SYNTAX:

The following is the syntax of while loop in C programming language:

while (condition)

         {

Statements;

}

Condition: the condition is given relational expression. It controls the iteration of loop. The statements are executed only if the condition is true.

Statement: it is the instruction that is executed when the condition is true. Two or more statements are specified in braces {}. It is called the body of the loop.


Working of While loop:

First of all, the condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in the body. After executing the statements it again moves to the start of loop and check the condition. This process continues as long as the condition remains true. When the condition becomes false, the loop is terminated. While loop terminates only when the condition becomes false if the condition remains true, the loop never ends. A loop that has no end point is known as infinite loop.


Flowchart:

Flowchart

EXAMPLE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int n;

n =1;

while (n <= 5)

{

printf (“Country\n”);

n ++;

}

OUTPUT:

Country

Country

Country

Country

Country


Do-while loop:

The do-while is an iterative control in C programming language. This loop executes one or more statements while the given condition is true. In this loop, the condition comes after the body of loop. The loop is important in a situation where a statement must be at executed at least once.

SYNTAX:

doLoop body

{

Statements;

}

while (condition);


Working of Do-while loop:

First of all, the body of loop is executed. After executing the statements in the loop body, the condition is evaluated. If it is true, the control again enters the body of loop and executes all statements in the body again. This process continues as long as the condition remains true. The loop terminates when the condition becomes false. This loop is executed at least once even if the condition is false in the beginning.

EXAMPLE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int n;

n = 1;

do

{

printf (“Country\n”);

n ++;

}

while (n <= 5);

}

OUTPUT:

Country

Country

Country

Country

Country


For loop:

‘for loop’ executes one or more statements for a specified number of times. This loop is also called counter controlled loop. It is the most flexible loop. That is why the most programmers use this loop in programs.

SYNTAX:

for (initialization; condition; increment/decrement)

{

statements;

}

Initialization:

It specifies the starting value of the variable.

Condition:

The condition is given as a relational expression. The statement is executed only if the condition is true.

Increment/decrement:

This part specifies the change in counter variable. If we use increment operator then the value of the variable is increased and if decrement operator is used then the value of the variable is decreased.

Statement:

Statement is the instruction that is executed when the condition is true.


Working of “for” loop:

The number of iteration depends on the initialization, condition and increment/decrement parts. The initialization part is only once executed when the control enters the loop. After initialization, the given condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in it. Then the increment/decrement part is executed that changes the value of counter variable. The control again moves to condition part. This process continues while the condition remains true. The loop is terminated when the condition becomes false.


Flowchart:Increment-decrement

Example:

# include <stdio. h>

# include <conio. h>

void main ()

{

int n;

for (n = 1; n <= 5; n ++)

{

printf (“Country\n”);

}

OUTPUT:

Country

Country

Country

Country

Country


Nested for loop:

A loop inside another loop is called nested loop. A ‘for loop’ inside another ‘for loop’ is called nested for loop. The following is the syntax of nested for loop:

for (initialization; condition; increment/decrement)

{

       for (initialization; condition; increment/decrement)

            {

                  statements;

            }

}

Consider the following half pyramid of *:

*

* *

* * *

* * * *

* * * * *

The following code produces the above shape by using nested for loop:

CODE:

# include <stdio. h>

# include < conio. h>

void main ()

{

int x, y;

for (x = 1; x <= 5; x ++)

{

     for (y = 1; y < = x; y ++)

           printf (“*”);

     printf(“\n”);

}

getch ();

}


Break statement:

In programming languages the break statement is used to break through a loop or a switch statement. The break statement is used to break the flow of the loop from the current position and the control is transferred out of the loop.


Break statement in switch statement:

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

Syntax:

The following is the syntax of the break statement in Java:

break;


Flowchart of break statement:

The following is the flowchart of break statement when it is used within a loop to exit from the loop or to break the flow of the loop moving the control out of loop:

Condition of loop

break statement with for loop:

Consider the following example in which we have used the break statement within the “for” loop:

CODE:

# include <stdio. h>

# include < conio. h>

void main () {

for (int x = 1; x <= 10; x++) {

if (x == 5) {

break;

}

printf (“Country”);

}

}

OUTPUT:

Country

Country

Country

Country

Country

In the above example for loop is used and in the “for” loop we used if condition. When the value of the counter variable x becomes equal to 5 then the loop is broken and the control is transferred out of the loop. In this way we got our output five times and not ten times.


Continue Statement:

The continue statement in C programming language is used to continue loop. The continue statement is used to continue the flow of the program and it skips the remaining code at a specified condition. When the continue statement is used inside the inner loop then only the inner loop continues.

The following is the syntax of the continue statement in C programming language:

Syntax:

continue;


Flowchart of continue statement:

The following is the flowchart of continue statement:

Remaining part of loop

Continue statement example:

Consider the following example in which we have used the continue statement that is based on a condition to continue the “for” loop:

CODE:

# include <stdio. h>

# include <conio. h>

void main () {

for (int j = 0; j < 10; j++) {

if (j == 4) {

continue;

}

printf (“%d”, j);

}

}

OUTPUT:

0

1

2

3

4

5

6

7

8

9

In the above example, we used a ‘for’ loop in which the counter variable is initialized at 0 and the loop is executed 10 times. When the counter variable becomes equal to 4 then the loop continues and in this way we have the output of numbers from 0 to 9.