‘do while’ loop
A “do-while” loop is a type of loop in programming that executes the statements inside the loop at least once before checking the condition, and continues to repeat the loop as long as the condition is true.
Syntax : do..while loop
Remember that, the difference between a while loop and a do..while loop is, do..while loop statements are always executed first and then the expression will be checked.
do..while loop says, repeat the execution of code inside the “do” body until the expression evaluates to false(0).
The syntax of the do..while loop is shown in Figure 1.
Please take note that, there is a semicolon at the end of the while(expression) and this semicolon is compulsory. Not giving a semicolon is a syntax mistake, so you’ll face a compilation error.
It is very easy to understand. First, these statements will be executed, which is there inside the “do” body, and then while(expression) will be evaluated. So, if this expression evaluation result is 0 (that is false), then the loop ends. That means control goes out of the body and continues with the rest of the code of the program.
Suppose, if this expression evaluation is true, then control loops back once again to the “do” body and it executes the statements once again. Like that. So, statements will be executed again and again until this expression becomes false.
The key difference between a do-while loop and other loops such as “while” or “for” loops is that the do-while loop is guaranteed to execute at least once, whereas the other loops may not execute at all if the condition is false from the start.
Flowchart of the do..while loop
First, statements will be executed, then expressions will be evaluated. If the expression is false, then the program continues with the code outside the loop body.
These are the points you should always remember.
- Statements are executed first.
- Expression is evaluated.
- If the expression evaluation result is true, execution loops back and the statements inside the “do” body executes again.
- If the expression evaluation result is false, while statement is terminated and program continues with the statement next to while statement.
- Statements are executed at least once.
That’s about the do..while loop.
And what you should note here is that statements are executed at least once. That’s the difference between a while loop and a do..while loop. In do..while loop, statements are executed at least once.
When to Use a “do-while” Loop:
do..while loop is rarely used while looping, because, we can always replace do..while loop with while loop or for loop. But, sometimes it is used. It is not like you know it is never used, but it is always encouraged to use do..while loop if it is necessary.
In embedded programming, we use do..while loop to write multiline ‘C’ macros in a header file. More on this, we will learn when we explore ‘C’ pre-processor directives.
In that case, I will show you how you can write a function like macros and multiline macros using do..while loop. In embedded system programming, you see do..while loop most of the time in header files, and we’ll discuss that when we reach there.
Again please take note that, there is a semicolon in a while expression and it is compulsory.
And in the next article, I will explain to you a very important loop that is for loop.
Get the Full course on Microcontroller Embedded C Programming Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1