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. We are discussing how to convert one loop to another.

Before understanding the conversions we must know loops. Please read this first:

How to write repetitive statements in C? How to write loop statements?

Write syntax of for-loop, while-loop and do while loop?

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

Design of the loops

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.

convert for while do-while loop flow chart

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 252. Your C learning is 0.00% complete. Login to check your learning progress.

Further readings

How to write repetitive statements in C? [for, while, do while example, flowchart]
Repetitive tasks in programming. Repetitive statements and loop statements. How to write loop statements. For while do-while loop, syntax and example code.

Write C programming syntax for, while, do while loops.
How to write loop statements in C? How to write a for loop, while loop, do while loop? Study the SYNTAX, FLOW DIAGRAM, with CODE, VIDEO examples.

When is do-while loop preferred over while loop, Give example?
Understand importance and need of do-while loop. How preferred over while loop, Use of do while over while loop. Give example do while vs while usecase

Write infinite loop statements for 'while', 'do-while' and 'for'?
What is an infinite loop in programming? Need of infinite loop. Write infinite loop statements in C. Example of infinite loop 'for' 'while', 'do-while'.

How to break a loop? How to skip loop iterations with continue? [example and flowchart]
Understanding C continue and break keyword with flowchart and practical example source code. Where to put break or continue in for loop, while loop, do while loop

#