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 flowchart
While Loop syntax
While Loop flowchart
Do-While Loop syntax
Do-While Loop syntax
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.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Free alt softwares