Loop Types

 The loop are used to repeat the execution of statement of block loop are used to perform a set of instruction repeatedly this involves repeating some partition of the program.

There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially. The first statement in a function is executed first, followed by the second, and so on. 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 and following is the general from of a loop statement in most of the programming languages:

 

There are four type of loop


Loop Type

Description

While loop

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

For loop

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

Do … while loop

Like a ‘while’ statement, except that it tests the condition at the end of the loop body.

Nested loops

You can use one or more loop inside any another ‘while’, ‘for’ or ‘do … while’ loop.


 While loop

The while loop is used when the number of iteration to be performed are not known in advance. A while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax -

            Initialization;

            While(condition){

                        True statement;

            }

            False statement;

 

Example of While loop

#include<stdio.h>

#include<conio.h>

void main(){

            int i=1,j,m;

            clrscr();

            while(i<=5){

                        m=1;

                        j=1;

                        while(j<=i){

                                    printf("%d",m++);

                                    j++;

                        }

                        printf("\n");

                        i++;

            }

getch();

}

Output

        1

        1 2

        1 2 3

        1 2 3 4

        1 2 3 4 5


Do … While

In the case the code within the block is executed at least one time the condition for iteration of the loop is checked at the end it is also known as entry control loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax -

            Initialization;

            Do{

                        statement;

            }

            While (condition);

 

Example of Do … While loop

#include<stdio.h>

#include<conio.h>

void main(){

            int i=1;

            clrscr();

            do{

                        printf("%d",i);

            }

            while(i++<=5);

getch();

}

Output

        1 2 3 4 5


For loop

The for loop is used to execute a statement a fixed number of times more than one statement can be placed in the block it has 3 part Initialization; loop condition; increment and decrement 

Syntax -

            For (Initialization; condition; increment/decrement){

                        statement;

            }

 

Example of For loop

#include<stdio.h>

#include<conio.h>

void main(){

            int i,m=1;

            clrscr();

            for(i=1;i<=7;i++){

                        printf("%d",m);

                        m=!m;

            }

getch();

}

Output

        1 0 1 0 1 0 1


Nested loops

A loop can be nested inside of another loop.

Syntax -

            For (Initialization; condition; increment/decrement){

                        For (Initialization; condition; increment/decrement){

                                    statement;

                        }

                        Statement;

            }

Example of Nested loops

#include<stdio.h>

#include<conio.h>

void main(){

            int i,j,m=1;

            clrscr();

            for(i=1;i<=5;i++){

                        m=1;

                        for(j=1;j<=i;j++){

                                    printf("%d",m++);

                        }

                        printf("\n");

            }

getch();

}

Output

        1

        1 2

        1 2 3

        1 2 3 4

        1 2 3 4 5

Post a Comment

0 Comments