While loop in C

By | February 20, 2023

Prerequisite – Control Statements in C
In C, a “while” loop is a control structure used for repeating a set of statements as long as a certain condition is true. It is often used when the number of iterations is not known in advance, or when the number of iterations depends on the input data.

Syntax:
The syntax of a “while” loop in C is as follows:

while (condition) {
    // code to be executed
}
  • The condition is checked at the beginning of each iteration of the loop. If the condition is true, the loop continues to execute. If the condition is false, the loop terminates.
    Here’s an example of using a “while” loop in C to print the numbers 1 to 10:

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

Example-1: Print the numbers from 1 to 10

#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

Example-2: Calculate the sum of the first 10 natural numbers

#include <stdio.h>

int main() {
    int i = 1, sum = 0;

    while (i <= 10) {
        sum += i;
        i++;
    }

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

    return 0;
}

Output:

Sum = 55

Example-3: Find the maximum element in an array

#include <stdio.h>

int main() {
    int arr[5] = {3, 8, 1, 5, 2};
    int i = 0, max = arr[0];

    while (i < 5) {
        if (arr[i] > max) {
            max = arr[i];
        }
        i++;
    }

    printf("Maximum element = %d", max);

    return 0;
}

Output:

Maximum element = 8

Example-4: 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");

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

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

    return 0;
}

In this example, the “while” loop is used to read a list of numbers from the user and add them together until a negative value is entered. The loop condition checks if the value of num is greater than or equal to 0, and if it is, the number is added to the sum variable and another number is read from the user using the scanf function. If a negative number is entered, the loop terminates and the final value of sum is printed to the console.

When this program is run, the user is prompted to enter a list of positive numbers. The program reads each number entered by the user, adds it to the sum, and continues to prompt the user for more numbers until a negative value is entered. Once a negative value is entered, the sum is calculated and 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.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.