C Programming • C Basics, Operators, Loops
C Programming / Control Structures: while

Control Structures: while

Notes 2 C Basics, Operators, Loops

Definition: The while loop is an entry-controlled loop that repeatedly executes a block of statements as long as the given condition remains true.

Notes

While Loop in C

Definition: The while loop is an entry-controlled loop that repeatedly executes a block of statements as long as the given condition remains true.

💡 Example: If i <= 5, the loop can print the numbers from 1 to 5. The condition is checked before every iteration.

Syntax

while (condition)
{
    statements;
}

How while Loop Works

Initialize variable ↓ Check condition ↓ Condition TRUE? ↓ ↓ Yes No ↓ ↓ Execute body Exit loop ↓ Update variable ↓ Check condition again

Example

To print numbers from 1 to 5, initialize i to 1 and continue the loop while i <= 5.

#include <stdio.h>

int main()
{
    int i = 1;

    while (i <= 5)
    {
        printf("%d ", i);
        i++;                // move to the next number
    }

    return 0;
}

Output

1 2 3 4 5

Why is while Called an Entry-Controlled Loop?

A while loop checks its condition before executing the loop body. Therefore, if the condition is false at the beginning, the loop body will not execute.

💡 Example: If i = 10 and the condition is i <= 5, the condition is false initially, so nothing inside the loop is executed.

Real-World Example

A program may continue asking for information while a particular condition is true. For example, a system can keep processing items while items are still available.

💡 Example: While balance is greater than 0 → continue processing.

Practical Example — Printing Numbers from 1 to N

Problem Statement

Write a C program to print all integers from 1 to N using a while loop.

Learning Outcomes

  • Understand the use of the while loop.
  • Initialize and update a loop counter correctly.
  • Understand entry-controlled loop execution.

Hint

Initialize the counter to 1, print it, and increment it until it becomes greater than N.

Theory

A while loop checks its condition before each iteration. If the condition is true, the loop body executes; otherwise, the loop ends.

Program

#include <stdio.h>

int main()
{
    int n, i = 1;          // i is the loop counter, starting at 1

    printf("Enter N: ");
    scanf("%d", &n);

    while (i <= n)       // repeat while i has not exceeded n
    {
        printf("%d ", i);
        i++;               // move to the next number
    }

    printf("
");

    return 0;
}

Expected Output

Enter N: 10
1 2 3 4 5 6 7 8 9 10

Note

A common mistake is forgetting to update the loop variable. For example, forgetting i++ can create an infinite loop because the condition may never become false.

while Loop vs if Statement

while if
Used for repeated execution. Used for decision making.
May execute many times. Normally executes at most once.
Condition is checked before every iteration. Condition is checked once when the statement is reached.

Important Points for Exam

  • while is an entry-controlled loop.
  • The condition is checked before the loop body executes.
  • The loop may execute zero or more times.
  • The loop variable should be updated properly.
  • Forgetting the update can cause an infinite loop.

Quick Revision

🎯 Remember:

Initialize → Check → Execute → Update → Repeat

Important Exam Questions

Short Answer Questions

  1. What is a while loop in C?
  2. Why is while called an entry-controlled loop?
  3. Write the syntax of a while loop.
  4. What happens if the while condition is false initially?
  5. What is an infinite loop?
  6. Write one example of a while loop.

Long Answer Questions

  1. Explain the while loop with syntax and suitable example.
  2. Write a C program to print numbers from 1 to N using a while loop.
  3. Explain how an entry-controlled loop works with the help of a while loop.
🎥 Recommended Learning

Watch a beginner-friendly explanation of the while loop in C.

▶ Watch: while Loop in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for the while loop will be provided here.

🧠 Mind Map

Use the mind map for quick revision of initialization, condition, execution and update.