While loop in Programming

Last Updated : 22 Oct, 2026

A while loop is a control structure that repeatedly executes a block of code as long as a specified condition remains true.

  • The condition is checked before each iteration, and the loop stops once the condition becomes false.
  • It is useful when the number of iterations is not known beforehand.
while_loop

Let's understand the working of a While Loop using an example.

Given a number n, print all numbers from 0 to n in increasing order in a single line using a while loop.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 10;
    // cin >> n;

    int i = 0;

    // Print numbers from 0 to n in increasing order.
    while (i <= n) {
        cout << i;

        // Print a space only if more numbers remain.
        if (i < n) {
            cout << " ";
        }

        i++;
    }

    return 0;
}
C
#include <stdio.h>

int main()
{
    int n = 10;
    // scanf("%d", &n);

    int i = 0;

    // Print numbers from 0 to n in increasing order.
    while (i <= n)
    {
        printf("%d", i);

        // Print a space only if more numbers remain.
        if (i < n)
        {
            printf(" ");
        }

        i++;
    }

    return 0;
}
Java
import java.util.Scanner;

class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // int n = sc.nextInt();
        int n = 10;
        int i = 0;

        // Print numbers from 0 to x in increasing order.
        while (i <= n) {
            System.out.print(i);

            // Print a space only if more numbers remain.
            if (i < n) {
                System.out.print(" ");
            }

            i++;
        }

        sc.close();
    }
}
Python
if __name__ == "__main__":
    # n = int(input())
    n = 10
    i = 0

    # Print numbers from 0 to n in increasing order.
    while i <= n:
        print(i, end="")

        # Print a space only if more numbers remain.
        if i < n:
            print(" ", end="")

        i += 1
C#
using System;

class GFG {
    static void Main(string[] args) {
        // int n = Convert.ToInt32(Console.ReadLine());
        
        int n = 10;
        int i = 0;

        // Print numbers from 0 to n in increasing order.
        while (i <= n) {
            Console.Write(i);

            // Print a space only if more numbers remain.
            if (i < n) {
                Console.Write(" ");
            }

            i++;
        }
    }
}
JavaScript
// const fs = require('fs');

// const input = fs.readFileSync(0, 'utf8').trim();
// let n = parseInt(input);

let n = 10;
let i = 0;

// Print numbers from 0 to n in increasing order.
while (i <= n) {
    process.stdout.write(i.toString());

    // Print a space only if more numbers remain.
    if (i < n) {
        process.stdout.write(" ");
    }

    i++;
}

Output
0 1 2 3 4 5 6 7 8 9 10

Explanation:
The code initializes i = 0 and runs a while loop while i <= n. In each iteration, the current value of i is printed, and then i is incremented by 1. The loop terminates when i becomes greater than n.

Use Cases of While Loop:

  • Input Validation: Repeatedly ask for input until the user provides a valid value.
  • Processing Data: Traverse arrays, lists, or collections until a condition is met.
  • Event Handling: Continuously monitor events like sensor data or network requests.
  • Implementing Algorithms: Used in algorithms such as searching, sorting, and mathematical computations.
  • State Machines: Execute logic repeatedly until a state change occurs.
  • Games and Simulations: Run the main game loop to update state, process input, and render output.
  • Batch Processing: Process multiple tasks (files, records, etc.) until all items are handled.
Comment