Home C Programming Language / Programs / do while loop in C Programming language
🚀 Programming Example

do while loop in C Programming language

👁 1,192 Views
💻 Practical Program
📘 Step Learning
When the test expression is false (nonzero), the do...while loop is terminated.

💻 Program Code

// Program to add numbers until user enters zero

#include <stdio.h>
int main()
{
    double number, sum = 0;

    // loop body is executed at least once
    do
    {
        printf("Enter a number: ");
        scanf("%lf", &number);
        sum += number;
    }
    while(number != 0.0);

    printf("Sum = %.2lf",sum);

    return 0;
}
                        

🖥 Program Output

Enter a number: 1.5
Enter a number: 2.6
Enter a number: 2.3
Enter a number: 5.6
Enter a number: 1.5
Enter a number: 0
Sum = 13.50
                            

📘 Explanation

The code block (loop body) inside the braces is executed once.

Then, the test expression is evaluated. If the test expression is true, the loop body is executed again. This process goes on until the test expression is evaluated to 0 (false).

When the test expression is false (nonzero), the do...while loop is terminated.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.