Control Structures in C…!!!(Iteration or Looping)

In our earlier post we presented you about Decision Making Statements in C……..In this post we are going to learn about Looping statements or Iterative statements in C.The main advantage of loops is we can execute the same steps more than single item.For ex: if we want to print the hello world statement for 10 times we can’t write it 10 times,hence we use looping and make it print 10 times using a condition.There are three type of Iterative statements:
1.while.
2.do while.
3.for loop.
Let us learn about these statements in detail and those syntax:
1.while statement:while loop is also termed as entry controlled as it checks the condition in the starting.The syntax of the while statement is

while(condition 1)
 {
     statements to be executed;
  }

If the given condition is true,then only the statements present inside the while statement are executed.If the condition is not satisfied control goes to other statement.Consider an example of using while statement

int i=1;                //variable to control the while loop
while(i<=10)
 {
       printf("\nHello world");
       i++;            //Increment of the variable.
  }

The output of the above part is printing Hello world line for 10 times but by using a single statement.
2.do….while:It is termed as exit controlled because it checks the condition at the exit of the while….With do while we are going to execute the statements of the while for a minimum of one time…..and if condition is satisfied it re-executes those statements in loop.The syntax of the do…while is

do
 {
     statements to execute
 }while(condition);

Example illustrating the do…while is

int i=11;
 do
  {
      printf("\nHello world");
  }while(i<10);

The output of the above statement is a single line Hello world…since the condition has been failed it is not going to be executed again.
3.for statement:The main drawback of the while and do while statements is we need to initialize the variable outside the loop and increment the variable to control the loop.In for loop we can initialize the variable,check for the condition and increment the variable.The syntax of the for loop is:

for(variable initialize;condition of the variable;incrementing the variable)
 {
      statements to execute;;
  }

Let us make an example to demonstrate the use of the for loop

for(i=1;i<10;i++)
 {
       printf("\nHello world");
 }

The flow of the control while executing for loop is…….It firsts initialize the variable….checks for the condition…if the condition is true then the statements are executed….After the end of the statements the control goes to incrementing part of the variable…..after that it checks the condition and control passed to the statements in loop.

The output of the above program is Hello world line for 10 times.

With this we reached the end of control strucutres in C…….If you have any queries please post them in comments.

Advertisements

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in C and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s