Printing Number 1 to 10


Definition:

The while loop is an entry-controlled loop in C programming. It repeatedly executes a block of code as long as the specified condition is true. When the condition becomes false, the loop terminates and the control moves to the next statement after the loop.

How It Works?

  • Step 1: Initialize the loop control variable.
  • Step 2: The while loop checks the specified condition.
  • Step 3: If the condition is true, the statements inside the loop are executed.
  • Step 4: Update the loop control variable.
  • Step 5: The condition is checked again.
  • Step 6: The loop continues until the condition becomes false.

Syntax:

Initialization;

while(condition)
{
    // Statements

    Update;
}

Example Program


#include <stdio.h>
#include <conio.h>

main()
{

    int k = 1;

    clrscr();

    while(k <= 10)
    {

        printf("\n%d", k);

        k = k + 1;

    }


    getch();

}

Output

1
2
3
4
5
6
7
8
9
10

Practice Exercises

Task 1: Write a C program using the while loop to print numbers from 1 to 10.

Goal: Learn how to use the while loop for repeating a statement multiple times until a condition becomes false.
💡 Hint: Initialize a variable with value 10. Use the while condition to check whether the number is less than or equal to 20. Increase the value by 1 inside the loop.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{

    int k = 10;

    clrscr();

    while(k <= 20)
    {

        printf("\n%d", k);

        k = k + 1;

    }


    getch();

}

Output
10
11
12
13
14
15
16
17
18
19
20
Explanation: The program starts with the variable k initialized to 1. The while loop checks whether k is less than or equal to 10. If the condition is true, the current value of k is displayed and then increased by 1. The loop continues until the value reaches 10.