C Programming

Do while in c

Loops in C are divided into two parts. One is the loop body, and the other is the control statement. Each loop is unique in its way. Do while loop is alike to a while loop in some aspects. In this loop, firstly, all the statements inside the body are executed. In case the condition is true, then the loop is again executed until the condition becomes false. In this guide, we will shed some light on the examples of do-while loops.

Syntax

do {
statements
………
}
while (expression);

Example 1

In this example of the do-while loop, we want to calculate the sum of the numbers. This can be done in two ways. Either the values are introduced in the program by yourself or ask the user to enter the values and then calculate the sum. Like for loop, we will also provide a limit up to which the numbers are to be entered. But in the for loop, we used to enter the constant number to start the loop smoothly and end at that number. If we want to apply a specific condition that may not be constant, the do-while loop is used in this situation. So in this example, we have applied a while condition to continue entering the values until the user enters 0.0. The loop’s body will show the message to enter the number, and then the sum is calculated. After the loop body, the condition is applied in the form of a while statement.

do{
Printf(“Enter a number:);
Scanf(%lf”, &number);
Sum += number;
}
While(number != 0.0)

The exclamation mark is used to negate the condition.

If we want to view the output of the above code, then we will use the following appended commands. In the Ubuntu system, this output obtaining method is done through the Ubuntu terminal.

The code compiler used in Linux for C is GCC. This will first compile the code, and then we will get the output.

$ GCC –o file3 file3.c
$ ./file3

By observing the output, you will see that the system prompts the message of “Enter a number” until the user has entered that value given in the condition that is 0.0. After that, the sum is calculated and is displayed in the next line.

Example 2

Now, moving towards another example. This example will use the do-while loop to calculate the multiples of a given number. But this time, the values are not required from the user. Besides, only the initial value is mentioned, and this value is incremented in every loop execution.

In the previous example, each value was entered by the user. Whereas in this case, there is no involvement of the user.

do{
Printf(%d/n” , 5*num);
Num++;
}
While(num<=7)

In this program, the condition is to increment to the number that must be less or equal to 7. It means the loop will execute 7 times, and we will get the result in 7 numbers. The initial value is 1; after one loop, it will be incremented and will become 2 and so on for further increments.

In comparison with the body of the do-while loop, for loop has increment statement inside the definition, whereas, while/ do-while loop has this statement inside the do-while body.

Moving to get the output, the same compilation and execution method will be used.

You can see that the result shows all the multiples of 5 up to the 7th number as the number is 5 in the formula.

Example 3

This is another illustration of the do-while loop. Similar to the previous example, interest will be calculated in this example. This is an example of daily life. Different values do interest calculation. The user will provide all these values at the run time when we execute the program. The value is stored in the variable. Hence the variable will be used in the formula as we cannot use the values/ numbers directly in the calculations if provided by the user.

do {
…….
Printf=(“interest rate= %.5f, (a*b*c)/100);
}

This line shows the calculations in the loop body. After the loop body is closed. While the statement is define

While ( ch == ‘y’);

This is the condition that shows that the system will continue requiring the number from the user until the user press the character y. this “y” stands for yes. When all values are given, and the interest is also calculated, the next line user will be asked whether he wants the system for again calculation or want to quit. So if the user presses y, it will continue. Otherwise, in the second condition, the loop will not execute.

The output is shown below in the cited image. GCC compiler, after compilation, executes the program, and the result is shown in the display.

The output shows that firstly, the user has pressed y so that the program will run again. When ‘n’ is pressed, the code will stop execution.

The dissimilarity between while loop and do-while loop.

The basic difference in both the loops is that in the while loop, the while statement is defined above the loop body, whereas in the do-while, the while statement is declared after the loop body.

To understand the difference in both the loops, we will shed some light on the given an example. As these are two loops, so we will consider two programs so that their separate working will make it easy to discriminate.

While loop

In the first program, we have used the while loop. In while loop declaration, it is initiated before the loop body. In this example, we print two lines, one inside the loop body and outside. The condition is that the loop will carry on till the variable value reaches 1, whereas this value was mention as 0 at the start.

While(i==1)

This condition will be true if the value is incremented. So it will reach 1 from 0. But as there is no value incrementation inside the loop body. The value will remain the same, that is 0. That’s why the loop will not run.

Do while loop

The concept is the same; two lines are to be printed just like the above example. The variable is also initiated as 0. At the same time, the condition is true until it reaches 1.

Do {
……..
} while ( i ==1)

The output method is the same. While seeing the output, first consider the while loop. This shows the line that was printed outside the loop is displayed, and the inside is not. This is because the condition is false.

Considering the do-while loop result, you can see that both the lines are displayed. This is because the do-while loop is executed at least once, even if its condition is false. In contrast, the condition is written and evaluated after the “do” loop is executed.

Conclusion

In this guide, we have discussed the dissimilarity between the while and do-while loop and their working. The functionality of the do-while loop is discussed in a detailed way through examples.

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.