Loops

In decision making, we are executing a block of code only one time when certain condition meets. We might come across a situation to execute a block of code n number of times. Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out from the loop statements when the condition becomes false.

There are 3 types of loop control statements in C language. They are,

  1. for
  2. while
  3. do-while
b82c1 loop bmp 253x300

for loop:

Execute a sequence of statements multiple times and abbreviate the code that manages the loop variable.It is entry controlled loop.

syntax:

for(initialization;condition;increment)

{

………. body……….

}

program:

#include
int main ()
{
for(int i = 0 ; i < 5 ;i++ )
{
printf(“i = %d\n”,i);
}
return 0;
}

output:

i=0

i=1

i=2

i=3

i=4

While loop

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

syntax:

initialization;

while(condition)

{

…….

increment;

}

program:

#include
int main ()
{

int i =0;
while(i < 5)
{
printf(“i = %d\n”,i);

i++;
}
return 0;
}

output:

i=0

i=1

i=2

i=3

i=4

do…while statement:

Like a while statement, except that it tests the condition at the end of the loop body.It is exit controlled loop.Condition will be executed at least once,since condition checks at last.

syntax:

initialization;

do{

…..

increment;

}while(condition);

3 Replies to “Loops”

  1. I think that is one of the so much vital info
    for me. And i’m satisfied studying your article. However should remark on some common things, The website taste is wonderful, the articles is
    really nice : D. Just right job, cheers

  2. Greetings! This is my first visit to your blog!
    We are a collection of volunteers and starting a new initiative in a community in the same
    niche. Your blog provided us beneficial information to work on. You have done a marvellous job!

Leave a Reply

Your email address will not be published. Required fields are marked *