C Programs Tutorials | IT Developer
IT Developer

C Programming - C while Loop



Share with a Friend

C Programming - C while Loop

while Loop in C

The while loop is used in C programming when you want to repeat a block of code an unknown number of times, but only as long as a certain condition remains true. It is a pre-test loop, meaning the condition is evaluated before each iteration of the loop.

Syntax:

C

while (condition) {

    // Code to be executed

}

  • condition: The condition is checked before each iteration. If it evaluates to true (non-zero), the loop body executes. If it's false (zero), the loop terminates.
  • Code inside the loop: This block will keep executing as long as the condition is true.

How while Loop Works:

  1. Initialization: Before the loop starts, a variable (or variables) are initialized.
  2. Condition Evaluation: The condition is checked at the beginning of each iteration.
  3. Loop Body Execution: If the condition is true, the loop body executes.
  4. Update Step: After each iteration, variables are updated (this can be done inside the loop).
  5. Termination: When the condition becomes false, the loop terminates and the control is passed to the next statement after the loop.

Example:

C

#include <stdio.h>

int main() {

    int i = 0;

    // Loop that prints numbers from 0 to 4

    while (i < 5) {

        printf("i = %d\n", i);

        i++;  // Increment i

    }

    return 0;

}

Explanation:

  • i is initialized to 0.
  • The loop runs while i < 5 (the condition is checked at the beginning).
  • Inside the loop, i is printed and then incremented by 1.
  • Once i reaches 5, the condition becomes false, and the loop terminates.

Output:

i = 0

i = 1

i = 2

i = 3

i = 4

Key Points about while Loop:

  1. Pre-Test Loop: The condition is checked before the loop body executes, meaning that if the condition is false initially, the body may not execute at all.
  2. Infinite Loop: If the condition is always true, the while loop will run indefinitely. This can be avoided by ensuring the condition eventually becomes false.

Example of an infinite loop:

C

while (1) {

    // This will run forever unless there's a break or return statement

    printf("This is an infinite loop.\n");

}

  1. Condition is Evaluated at the Start: If the condition is false at the start, the loop will not run at all.
  2. Loop Body Execution: The statements inside the loop are executed as long as the condition is true.

Example with User Input:

Here’s a more interactive example where the while loop continues until the user enters a specific value.

C

#include <stdio.h>

int main() {

    int number;

    // Ask the user for input

    printf("Enter a number (0 to quit): ");

    scanf("%d", &number);

    // Loop runs until the user enters 0

    while (number != 0) {

        printf("You entered: %d\n", number);

        printf("Enter another number (0 to quit): ");

        scanf("%d", &number);  // User enters new number

    }

    printf("Exited the loop.\n");

    return 0;

}

Explanation:

  • The program asks the user to enter a number.
  • If the user enters 0, the loop terminates.
  • Otherwise, it prints the entered number and prompts the user to enter another number.

Sample Output:

Enter a number (0 to quit): 5

You entered: 5

Enter another number (0 to quit): 10

You entered: 10

Enter another number (0 to quit): 0

Exited the loop.

Advantages of while Loop:

  • Flexible: It is ideal when the number of iterations is not known in advance.
  • Simple Condition: It executes based on a simple condition.

Disadvantages of while Loop:

  • Potential for Infinite Loops: If the condition is not correctly managed or updated, the loop may never terminate.
  • Condition Checking Overhead: The condition is checked each time, which may not be as efficient as a for loop if the number of iterations is fixed.

Conclusion:

The while loop is a fundamental looping construct in C that is useful for repeating tasks while a condition holds true. It's particularly useful when you don't know in advance how many iterations the loop will need. However, care should be taken to ensure the condition eventually becomes false to avoid infinite loops.