C++

Do while loop C++

The C++ ‘do-while’ loop is used when we need to iterate any specific part of a program many times. Do-while loops are mostly used when there is no specific terminating condition in a program, but you have to execute the loop a minimum of one time. The body of the loop comes first, then the test expression. That’s why the loop in the do-while loop has executed a minimum of one-time even if the condition in the test expression is false for the first time. This article will highlight the do-while usage in the C++ programming language, its advantages, the conditions where we use the ‘do-while’ loop.

Prerequisites

To understand the ‘do-while loop’ phenomenon on the Linux operating system, you need to have Ubuntu running on your system. For this purpose, install Virtual Box and configure it. Now add the Ubuntu file to it. Before adding, the file can easily be installed via the internet through a website that will take some hours to be completed. Run that file, and after successful configuration, you can now use Linux flavor easily. During configuration, make sure that the user is created; this is necessary to create a user to access all Ubuntu features.

Note: The user must have the know-how of C++ programming so that these codes are easily understandable by them. We have used Ubuntu 20.04; all these codes explained here are executable on any version.

When should we apply the Do-while loop in the code?

We used many conditions while executing programs. Some conditions have ending points that allow the loop to be executed to a specific number. But sometimes, the number of iterations is not specified. The compiler executes the loop body first before reading the condition that is written after the loop body. This indicates that the loop body always returns a result. So when you are asked to apply a scenario in a program where there is no specific number of iterations mentioned, you must use a do-while loop.

Syntax

do{
    code
}
while(condition);

The condition is a basic test expression to limit the execution/ iteration of the loop to a specific value. The ‘{}’ represents the loop body that lies above the condition.

Working of do-while loop

First, the do-while loop is started by describing the body of the loop. In the end, the test expression or condition is mentioned. Now here two possibilities occur. An if-else statement has the same functionality as the test condition of the do-while loop. If the test statement is true, the compiler of C++ executes the body of the do-while loop. And in the second possibility, if a condition is false, then the C++ compiler executes the statements that are mentioned after the body of the loop. In the end, after the test expression, those statements are written that comes after the loop body.

Example 1

Consider an example; here, we have simply used a C++ code with a do-while loop. This code is written in any text editor of Linux. And for the output, we will use the Ubuntu terminal. A header file of iostream is used that allows reading and writing for the console.

# include <iostream>

Now inside the main function, the logic is applied. A variable x is declared a value of 1. Now the body of the do loop is started. The program first displays the value of x, and each value is displayed in the next line. That’s why we have used ‘endl’ in the statement. After that, the test expression is mentioned. It is the condition to maintain a value less than 7.

While (x<7);

Now, we will execute the code. Go to the terminal. For the compilation of code, we always need a compiler. For C++, we need a G++ compiler to compile the code. Where “sample1.c” is the file. ‘-o’ is used to save the output.

$ G++ -o sample1 sample1.c
$ ./sample1

You can see that values up to 6 are displayed. As we have applied a check to limit the values to 7.

Example 2

Here we have taken values from the user, and the sum is calculated from them. Two variables, a number, and a sum, are initiated by 0 value. Inside the ‘do loop’, a value is obtained from the user, and all these values continue adding until the test expression is false. The loop is closed. Here the condition is applied to check whether the number entered by the user is 0. If it is 0, then the loop is terminated. The ‘!=’ is not equal to the operator used here.

While (number != 0);

The output will continue asking the number from the user till the user enters non-zero positive values. From the output, you can see that the first 3 numbers user enters, and at the 4th time, he enters 0, so the total of all three values are displayed here.

Example 3

This is an example of a nested do-while loop. First, you need to discuss the concept of do-while. This is a do-while loop inside the outer do-while loop.

do{
    code
    do{
        code
    }while(condition);
    code
} while(condition);

The nested do-while loop works so that the initialization of the outer loop is done; after that, the statements inside it (inner do loop) are executed. The text expression of the inner loop is evaluated. The loop runs till the resultant condition is true. The inner loop keeps running up to the false condition. When it becomes false, the inner loop is exited, and then the compiler goes to the next line after the inner loop. Again the test statement is evaluated but after the outer loop. If it is true, execution continues. On false, the loop exited, and the control jumps to the statements written after the loop.

Now coming back to the example, here in the main program, we have taken a variable that is initialized as 1.  When the outer do loop starts, another “b” variable is initialized with 1. The inner do loop displays the current value of a variable and iterates the value of the “b” variable with 1.  The loop is closed, and the inner do-while loop while statement shows that the value of b should be <= 3. Then going back to the outer loop, that increments the value of a variable. And the loop is closed; the while loop contains the statement that shows that the value of a must be <=2.

The output is displayed in the terminal, you can see that according to the outer loop, the value is displayed up to 2 numbers, and according to the inner loop, each value is displayed 3 times.

Example 4

It is an example of the do-while infinitive; here, in the do loop, a statement is displayed, and it will continue displaying this message because the while statement doesn’t contain any condition.

When you execute the file, the resultant screen looks like the below image. To stop the execution, Press Ctrl+C.

Conclusion

This article consists of syntax, usage, and examples of a do-while loop in the C++ programming language. The do-while loop is program-dependent because it is only applied where no terminating situation is given at the start. We have aimed to describe this dilemma in such a way as to remove the ambiguity from the end-users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.