Loops in C

C programming language supports three types of loop statements and they are for loop, while loop and do-while loop. Each of the loop structure was designed for some different purposes. Let us see the syntaxes and their graphical flow charts.

For loop

for (
  <initial statement(s)>;
  <Condition expression>;
  <Repeat step(s)>
  )
{
  <Loop statement(s)>;
}

For loop flowchart

For loop Diagram

While Loop syntax

while (<Condition expression>)
{
  <Loop statement(s)>;
}

While Loop flowchart

While loop Diagram

Do-While Loop syntax

do
{
  <Loop statement(s)>;

}while (<Condition expression>);

Do-While Loop syntax

Do-While loop Diagram

Loop conversions

For loop was designed to execute for a define numbers of iterations and it has an initialization statement section to initialize the iteration counter. While loop or do while loop do not have any initialization section. User has to write an initialization statement before the starting of the loop. Do while loop and while loops are different in the way they check the condition. While loop checks the condition first before entering in to the loop. However do-while does not check the condition and enters the loop for the first iteration.

For to While Loop conversions

To convert a for loop to while loop we need to simply add the initialization statement before the while loop.

/* For loop */
int i;
for(= 0; i < 10; i++)
{
}

/* For loop converted to while loop */
int i = 0; /* <<< Initialization */
while(< 10)
{
  i++;
}

While to For Loop conversions

To convert a while loop to a for loop we need to omit the initialization statement.

/* While loop */
while(*str++ != NULL)
{
  length++;
}

/* While loop converted to for loop */
for(; *str != NULL; str++)
{
  length++;
}

Do While to For/ While Loop conversions

Do while loop is very good when you require to enter once into the iteration and then check the exit condition. For and while loop checks the condition first and then enters. So we need to add a statement to make the condition true before starting of the loop. This way for and while loop will enter in the first iteration like the do while loop.

/* Do while loop */
do
{
  status = check_connection();
} while(status == TIMEOUT);

/* Do While loop converted to for loop */
status = TIMEOUT; /* Condition is true */
for(; status == TIMEOUT; str++)
{
  status = check_connection();
}

/* Do While loop converted to for loop */
status = TIMEOUT;  /* Condition is true */
while(status == TIMEOUT)
{
  status = check_connection();
}

For/ While Loop to Do While conversions

Do while loop enters once into the iteration and then check the exit condition. Whereas for and while loop always checks the condition before. This is the reason why While/For loops and do-while loops are not much compatible. Do not try to convert for / while loop to do-while loop. This will add unnecessary extra code.

/* For loop */
int i;
for(= 0; i < 10; i++)
{
}

/* while loop */
int i = 0; 
while(< 10)
{
  i++;
}

/* For/While converted to Do while loop (not recomended)*/
int i = 0; 
do
{
  if(>= 10) break;
  i++;
} while(TRUE);

About our authors: Team EQA

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

#