Do-While Loop in C Programming





The do-while loop in C is a control flow statement that executes a block of code repeatedly until a specified condition becomes false. Unlike the while loop, the do-while loop guarantees the execution of the block of code at least once, regardless of the condition's initial evaluation.

The do-while loop is particularly useful when you need to execute a block of code at least once and then repeat it based on a condition. It's commonly used when you want to perform an action and then check a condition for continuing the loop.



Syntex:
do {
    // Statements to be executed
} while (condition);

  • The block of statements within the curly braces {} is executed at least once.
  • After the execution of the block, the condition is evaluated.
  • If the condition is true, the loop continues to execute; otherwise, the loop terminates.


#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("Count: %d\n", i);
        i++;
    } while (i <= 5);

    return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5


The loop continues until the user enters a positive number.

#include <stdio.h>

int main() {
    int num;

    do {
        printf("Enter a positive number: ");
        scanf("%d", &num);
    } while (num <= 0);

    printf("You entered: %d\n", num);

    return 0;
}
Output:
Enter a positive number: -8
Enter a positive number: -6
Enter a positive number: 4
You entered: 4


This example demonstrates a menu-driven program where the user can select options until choosing to exit.

#include <stdio.h>

int main() {
    char choice;

    do {
        printf("Menu:\n");
        printf("1. Option 1\n");
        printf("2. Option 2\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf(" %c", &choice);

        switch (choice) {
            case '1':
                printf("Option 1 selected\n");
                break;
            case '2':
                printf("Option 2 selected\n");
                break;
            case '3':
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != '3');

    return 0;
}
Output:
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 2
Option 2 selected
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 1
Option 1 selected
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 3
Exiting...


C program that calculates the square of a number entered by the user and allows them to continue entering numbers as per their choice

#include <stdio.h>

int main() {
    char user_choice;
    int number;

    do {
        printf("Enter a number: ");
        scanf("%d", &number);
        printf("Square of %d is %d\n", number, number * number);
        printf("Would you like to enter another number? (y/n): ");
        fflush(stdin); // Clear input buffer
        scanf(" %c", &user_choice); // Notice the space before %c to consume the newline character
    } while (user_choice == 'y');

    return 0;
}
Output:
Enter a number: 5
Square of 5 is 25
Would you like to enter another number? (y/n): y
Enter a number: 2
Square of 2 is 4
Would you like to enter another number? (y/n): n


C program that calculates the number of characters in a string entered by the user.

#include <stdio.h>

int main() {
    char str[100];
    int count = 0, i = 0;
    printf("Enter a string: ");
    scanf("%s", str);
    do {
        count++;
        i++;
    } while (str[i] != '\0');
    printf("Number of characters: %d\n", count);
    return 0;
}
Output:
Enter a string: Program
Number of characters: 7


C program that calculates the factorial of a given number.

#include <stdio.h>

int main() {
    int num, factorial = 1, i = 1;
    printf("Enter a number: ");
    scanf("%d", &num);
    do {
        factorial *= i;
        i++;
    } while (i <= num);
    printf("Factorial of %d = %d\n", num, factorial);
    return 0;
}
Output:
Enter a number: 5
Factorial of 5 = 120


#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next, i = 1;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printf("Fibonacci Series: ");
    do {
        printf("%d ", first);
        next = first + second;
        first = second;
        second = next;
        i++;
    } while (i <= n);
    return 0;
}
Output:
Enter the number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
#include <stdio.h>

int main() {
    int n, i = 2;
    printf("Enter the upper limit: ");
    scanf("%d", &n);
    printf("Prime numbers between 2 and %d are: ", n);
    do {
        int is_prime = 1, j = 2;
        while (j <= i / 2) {
            if (i % j == 0) {
                is_prime = 0;
                break;
            }
            j++;
        }
        if (is_prime)
            printf("%d ", i);
        i++;
    } while (i <= n);
    return 0;
}
Output:
Enter the upper limit: 10
Prime numbers between 2 and 10 are: 2 3 5 7