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:
Let us see the syntaxes and their graphical flow charts.
For loop flowchart
While Loop syntax
While Loop flowchart
Do-While Loop syntax
Do-While Loop syntax
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.
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.
While to For Loop conversions
To convert a while loop to a for loop we need to omit the initialization statement.
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.
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.
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.
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
Learn on Youtube