While Loop

C Programming Tutorial : While Loop

Welcome to the "Codes with Pankaj While Loop in C Programming" tutorial ! This tutorial will guide you through the concepts and practical applications of the while loop in C.

Table of Contents


1. Introduction to While Loop

A while loop in C programming is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop checks the condition before executing the loop's body.

2. Syntax of While Loop

The basic syntax of the while loop in C is as follows:

while (condition) {
    // code to be executed
}
  • condition: An expression that evaluates to true or false.

  • If the condition is true, the code within the loop is executed.

  • If the condition is false, the loop terminates, and control passes to the statement following the loop.

3. Flowchart of While Loop

Below is a simple flowchart that illustrates the working of a while loop:

4. Examples of While Loop

Let's look at a few examples to understand the while loop better.

Example 1: Basic While Loop

This example demonstrates a basic while loop that prints numbers from 1 to 5.

#include <stdio.h>

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

Example 2: Sum of Natural Numbers

This example calculates the sum of the first 10 natural numbers.

#include <stdio.h>

int main() {
    int i = 1, sum = 0;
    while (i <= 10) {
        sum += i;
        i++;
    }
    printf("Sum of first 10 natural numbers: %d\n", sum);
    return 0;
}

Example 3: Infinite Loop

An example of an infinite loop. Be careful with such loops as they can cause your program to run indefinitely.

#include <stdio.h>

int main() {
    while (1) {
        printf("This loop will run forever.\n");
    }
    return 0;
}

5. Common Mistakes and Best Practices

Common Mistakes

  1. Forgetting to Update the Loop Variable: If you forget to update the loop variable, it may result in an infinite loop.

    int i = 1;
    while (i <= 5) {
        printf("%d\n", i);
        // i++; // Uncomment this to fix the infinite loop
    }
  2. Using Incorrect Condition: Ensure that the condition eventually becomes false to avoid infinite loops.

Best Practices

  1. Initialize Variables Properly: Always initialize the loop control variables before the loop.

  2. Update Loop Variables: Ensure that loop control variables are updated appropriately within the loop.

  3. Avoid Infinite Loops: Be cautious with conditions that can cause infinite loops.

6. Exercises

Try these exercises to practice while loops in C:

  1. Exercise 1: Write a program to print the first 10 even numbers using a while loop.

  2. Exercise 2: Write a program to calculate the factorial of a number using a while loop.

  3. Exercise 3: Write a program that reads integers from the user and stops when the user enters -1. The program should then print the sum of all entered numbers.


We hope this tutorial has helped you understand the while loop in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!

For more tutorials, visit www.codeswithpankaj.com.

Last updated