Do While Loop

C Programming Tutorial : Do While Loop

Welcome to the "Codes with Pankaj Do While Loop in C Programming" tutorial! This tutorial will guide you through the concepts and practical applications of the do while loop in C.

Table of Contents


1. Introduction to Do While Loop

A do while loop in C programming is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as the given condition is true. Unlike the while loop, the do while loop checks the condition after executing the loop's body.

2. Syntax of Do While Loop

The basic syntax of the do while loop in C is as follows:

do {
    // code to be executed
} while (condition);
  • condition: An expression that evaluates to true or false.

  • The code within the loop is executed once before the condition is tested.

  • If the condition is true, the code within the loop is executed again. This process repeats until the condition becomes false.

3. Flowchart of Do While Loop

Below is a simple flowchart that illustrates the working of a do while loop:

4. Examples of Do While Loop

Let's look at a few examples to understand the do while loop better.

Example 1: Basic Do While Loop

This example demonstrates a basic do while loop that prints numbers from 1 to 5.

#include <stdio.h>

int main() {
    int i = 1;
    do {
        printf("%d\n", i);
        i++;
    } while (i <= 5);
    return 0;
}

Example 2: Sum of Natural Numbers

This example calculates the sum of the first 10 natural numbers using a do while loop.

#include <stdio.h>

int main() {
    int i = 1, sum = 0;
    do {
        sum += i;
        i++;
    } while (i <= 10);
    printf("Sum of first 10 natural numbers: %d\n", sum);
    return 0;
}

Example 3: User Input Loop

This example prompts the user to enter numbers and calculates their sum until the user enters 0.

#include <stdio.h>

int main() {
    int num, sum = 0;
    do {
        printf("Enter a number (0 to stop): ");
        scanf("%d", &num);
        sum += num;
    } while (num != 0);
    printf("Total sum: %d\n", sum);
    return 0;
}

5. Common Mistakes and Best Practices

Common Mistakes

  1. Forgetting to Update the Loop Variable: If you forget to update the loop variable, it may result in an infinite loop.

    int i = 1;
    do {
        printf("%d\n", i);
        // i++; // Uncomment this to fix the infinite loop
    } while (i <= 5);
  2. Using Incorrect Condition: Ensure that the condition will eventually become false to avoid infinite loops.

Best Practices

  1. Initialize Variables Properly: Always initialize the loop control variables before the loop.

  2. Update Loop Variables: Ensure that loop control variables are updated appropriately within the loop.

  3. Use Proper Conditions: Be cautious with conditions that can cause infinite loops.

6. Exercises

Try these exercises to practice do while loops in C:

  1. Exercise 1: Write a program to print the first 10 odd numbers using a do while loop.

  2. Exercise 2: Write a program to calculate the product of digits of a number using a do while loop.

  3. Exercise 3: Write a program that continuously asks the user to enter a positive number and stops when the user enters a negative number. The program should then print the count of positive numbers entered.


We hope this tutorial has helped you understand the do while loop in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!

For more tutorials, visit www.codeswithpankaj.com.

Last updated