Loop Structure

11 – Loop Structure in C Language

In this chapter, we will talk about Loop Structure.

Looping Structure

A type of Control Structure which a statement or set of statements is known as Looping Structure.
You may experience situations, when a block of code needs to be executed many times. In general, Statements are executed sequentially.

A looping statement allows to execute statement or a group of statements multiple times.

The general form of a loop statements in most of the programming languages is given below:

Types

C provides the following types of Loop Structure:

Sr.NoLoop TypeDescription
1for LoopThis loop executes statements for specified number of time.
2while Loopwhile loop executes statements until the given condition is true. It is used in the situation when the number of repetitions are not known in advance.
3do-while Loopdo-while loop executes statements until the given condition is true. In this loop, the condition comes after the body of the loop.
4nested LoopA loop within another loop is called nested loop.

Loop Control Statements

Loop Control Statements changes execution from its normal sequence.

C supports the following control statements:

Sr.NOControl StatementDescription
1break statementTerminates the execution of the loop
2continue statementShifts the control back to the beginning of the loop

(i) for Loop

The for loop executes statements for specified number of time. It is most frequently used loop by the programmers.

Syntax

It’s syntax is:

for (initialization;condition;increment/decrement)
{
Body of the loop;
}

Where,

Initialization: It is an expression which specifies the starting value of the counter variable.

Condition: The condition is given as a relational expression. The statement within the loop is executed only if the given condition is true. If the Condition is False, statement is not executed.

Increment/Decrement: This part of loop specifies the change in counter variable after each execution of the loop.

Body Of the loop: It is a statement or group of statements in braces { } to be repeated.

For Example:

Programs

Program number 1


1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<conio.h>
void main()
{
     int x;
     clrscr();
     for (x=1;x<=10;x++)
         printf("%d\t",x);
     getch();
}

Output

1 2 3 4 5 6 7 8 9 10

Program number 2


1
2
3
4
5
6
7
8
9
#include<stdio.h>
#include<conio.h>
void main()
{
     int num;
     for (num=10;num>=1;--num)
         printf("%d\t",num);
     getch();
}

Output

10 9 8 7 6 5 4 3 2 1

(ii) while Loop

This loop executes one or more statements until the given condition remains true. It is useful when the number of repetitions are not known in advance.

Syntax

The syntax of while loop is:

while(condition)
{
Body of the loop
}

Where,

Condition: The condition is given as a relational expression. The body of the loop is executed only if the given condition is true. If the Condition is False, then the body of the loop is not executed.

Body Of the loop: It is a statement or group of statements in braces { } to be repeated.

Programs

program number 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<conio.h>
void main()
{
     int n;
     clrscr();
     n=1;
     while(n<=5)
    {
          printf("Hello\n");
          n++;
    }
    getch();
}

Output :

Hello
Hello
Hello
Hello
Hello

program number 2


1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<conio.h>
void main()
{
     int num=10;
     clrscr();
     while(n!=0)
     {
           printf("%d\n",n);
     }
     getch();
}

Output

10
9
8
7
6
5
4
3
2
1

(iii) do-while Loop

The do-while loop executes statements until the given condition is true. It is used when the loop body must be executed at least once even the condition is false for the first time. In this loop, the condition comes after the body of the loop.

Syntax:

It’s syntax is:

do
{
Body of the loop;
}
While(condition);

where,

do: It is a keyword which indicates the beginning of the loop.

Condition: The condition is given as a relational expression. The statements of the body are executed only if the condition is true. If the Condition is False, then the statements of the body are executed only once because the condition comes at the end.

Programs

program number 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<conio.h>
void main()
{
     int n;
     clrscr();
     n=1;
     do
     {
           printf("%d\n",n);
           n++
     }
     while(n<=10);
    getch();
}

Output

1
2
3
4
5
6
7
8
9
10

Infinite Loop

It is an another form of Loop Structure. A loop becomes an infinite loop, if a condition never becomes false. Traditionally, for loop is used for this purpose. You can make an endless loop by leaving the condition expression empty.


1
2
3
4
5
6
7
8
9
#include<stdio.h>
void main()
{
     for( ; ; )
     {
       printf(" this loop will run forever.\n");
     }
     return 0;
}

When the conditional expression is absent, it is assumed to be true.

Note: You can terminate an infinite loop by pressing Ctrl + c Keys.

Subscribe our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.