C While

Learn 'while' loop in C.

While Loop:

A 'while' loop in the C programming language is used to repeatedly execute a block of code as long as a specified condition is true. The general syntax of a while loop in C is as follows:

while (condition) {
    // Code to be executed as long as the condition is true

Here's a breakdown of how a 'while' loop works:

1. The 'condition' is evaluated first. If it is true, the code inside the loop is executed. If the condition is false initially, the code inside the loop will not execute at all.

2. After the code inside the loop is executed, the condition is checked again. If the condition is still true, the loop continues to execute, and the process repeats. If the condition becomes false, the loop terminates, and program control moves to the next statement after the loop.

Here's a simple example of a while loop that counts from 1 to 5:

c Copy Code
#include <stdio.h>

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


In this example, the loop will execute repeatedly as long as the 'count' variable is less than or equal to 5. It prints the value of 'count' and increments it in each iteration until 'count' becomes 6, at which point the condition becomes false, and the loop terminates.


Note: Be cautious when using 'while' loops to ensure that the condition eventually becomes false; otherwise, you may create an infinite loop that never terminates.

while (1) {...}

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.