Do-While loop in C

Updated

Prerequisite – Control Statements in C
The “do-while” loop is a loop statement in C that executes a block of code at least once, and then continues to execute the code as long as the loop condition is true.

Syntax:
The basic syntax of a “do-while” loop in C is:

do {
    // code to be executed
} while (condition);

Here, the code block within the “do” statement is executed at least once, and then the loop condition is checked. If the condition is true, the code block is executed again, and the loop continues until the condition becomes false.

Examples:
Here are some examples of using the “do-while” loop in C:

Example-1: Print the numbers from 1 to 10

#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("%d ", i);
        i++;
    } while (i <= 10);

    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

Example-2: Read and sum a list of numbers until a negative value is encountered

#include <stdio.h>

int main() {
    int num, sum = 0;

    printf("Enter a list of positive numbers (terminate with a negative number):\n");

    do {
        scanf("%d", &num);
        if (num >= 0) {
            sum += num;
        }
    } while (num >= 0);

    printf("Sum = %d", sum);

    return 0;
}

Output:

Enter a list of positive numbers (terminate with a negative number):
3
4
7
-1
Sum = 14

In this example, the “do-while” loop is used to read a list of numbers from the user and add them together until a negative value is encountered. The loop starts by executing the code block within the “do” statement, which prompts the user to enter a number and then checks if it is non-negative. If the number is non-negative, it is added to the sum variable. After each iteration of the loop, the loop condition is checked to see if the value of num is greater than or equal to 0. If num is greater than or equal to 0, the loop continues; if it is less than 0, the loop terminates and the final value of sum is printed to the console.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
Mithlesh Upadhyay Published 20 Feb 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read