Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering While and Do‑While Loops in C: Practical Examples

Mastering While and Do‑While Loops in C

Learn how to use while and do‑while loops in C with clear explanations and hands‑on code examples.

Loops are fundamental in programming, allowing you to repeat a block of code until a specific condition is met. C offers three primary looping constructs: for, while, and do‑while. This guide focuses on the latter two, providing step‑by‑step explanations, flowcharts, and real‑world code samples.


While Loop

The while loop evaluates its condition before each iteration. If the condition is true, the loop body executes; otherwise, the loop terminates.

while (testExpression) {
    // loop body
}

How the While Loop Works

For a deeper understanding of logical conditions, review relational and logical operators.

Flowchart of a While Loop

Mastering While and Do‑While Loops in C: Practical Examples

Example 1: Printing Numbers 1 to 5

// Print numbers from 1 to 5
#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d\n", i);
        ++i;
    }
    return 0;
}

Output

1
2
3
4
5

Explanation: i starts at 1. The condition i <= 5 is true, so the loop prints i and increments it. The process repeats until i becomes 6, causing the condition to evaluate to false and the loop to terminate.


Do‑While Loop

The do‑while loop guarantees that the loop body runs at least once, checking the condition only after the first execution.

do {
    // loop body
} while (testExpression);

How the Do‑While Loop Works

Flowchart of a Do‑While Loop

Mastering While and Do‑While Loops in C: Practical Examples

Example 2: Sum Numbers Until Zero is Entered

// Program to add numbers until the user enters zero
#include <stdio.h>
int main() {
    double number, sum = 0;
    do {
        printf("Enter a number: ");
        scanf("%lf", &number);
        sum += number;
    } while (number != 0.0);
    printf("Sum = %.2lf", sum);
    return 0;
}

Sample Interaction

Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70

In this example, the user is prompted at least once. If the first input is non‑zero, the number is added to sum, and the loop continues. Entering zero terminates the loop and displays the final sum.

Understanding these loops is essential for effective C programming. Use while when you need to test a condition before each iteration, and do‑while when the body must execute at least once.


C Language

  1. Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
  2. C# foreach Loops: Iterate Arrays and Collections with Ease
  3. C++ While and Do‑While Loops – Mastering Repetition in Your Code
  4. Mastering Python's While Loop: Syntax, Examples, and Best Practices
  5. Mastering Python Loop Control: break & continue
  6. Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
  7. Mastering Loop and Exit Constructs in VHDL: A Practical Guide
  8. Master C Loops: For, While, and Do‑While Explained with Practical Examples
  9. While vs. Do‑While Loops: Clear Comparison with Practical Examples
  10. Mastering WHILE Loops in SINUMERIK 840D CNC Programming