C While loop

A while loop in C is a control flow statement used to execute a block of code repeatedly as long as a specified condition remains true. The condition is checked before each iteration, and if the condition is false initially, the loop is skipped entirely.

Syntax:


while (condition) {
    // Code to execute as long as the condition is true
}

Explanation:

condition: A boolean expression that is checked before each iteration. If it is true, the loop continues; if false, the loop terminates.

Example: Print the value from 1 to 5


#include <stdio.h>

int main() {
     int number = 1; // Initialize the number variable
        // While loop that runs until number is less than or equal to 5
        while (number <= 5)
        {
             printf("%d\n",number); // Prints the current value of number
             number++; // Increment count after each iteration
        }

    return 0;
}

Output:

1
2
3
4
5