Writing repetitive statements is easy in C and keywords like for, while, do-while are available for this. Let us discuss these three types of looping statements one by one.
For loop has this syntax-
For loop starts with a one-time initialization statement then a comparison to check whether to continue the loop or exit from it. It also has one step statement which will be executed at the end of each iteration and then it will again go for the comparison. This is good when we are dealing with array and accessing one element at a time in each iteration. For while has this syntax- While loop does not have any initial statement like for loop. It goes straightly to condition checking then executes statements and again comes to condition checking. Thus again it does not have any step statement.
While loop is good when we do not know when to end specifically like linked list iterations.
For do-while has this syntax-
Do-while loop is not different than while loop except for the fact the checking will be done at the end of the loop thus the first iteration goes without any condition checking. Do-while loop is mostly used for the situations where we need to check the condition at the end like a menu driven program where we display entire menu selections and wait for user's input the depending upon user's input we decide whether to stay in the loop of exit from it. Curly brackets are not needed if there is only one statement inside the loop. C has "continue" and "break" keyword to either continue or exit from the loop. We will discuss more on this in our next section.
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 Memory leaks