C Language for & while & do..while Loop

Loops are vital in programming, automating repetitive tasks to save time and resources. They enhance code efficiency, readability, and resource utilization by reusing instructions and variables. Loops empower developers to create concise, adaptable solutions for various challenges.

Types of Loops

There are several types of loops commonly used in C programming:

  1. For Loop : These loop are ideal when you know how many times you want the code to repeat. They have a defined starting point, an end point, and a step size.
  2. While Loop : While loop keep running as long as a specified condition evaluates to true. They're useful when the number of iterations is uncertain.
  3. Do-While Loop : Similar to while loop, but they execute the code block at least once before checking the condition.

for Loop

#include <stdio.h>

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

OUTPUT:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

explanation:

  • #include <stdio.h>: This line includes the standard input-output library, allowing the use of functions like printf.

  • int main(): This is the main function where the program's execution begins.

  • for (int i = 1; i <= 5; i++): This is the for loop construct.

  • int i = 1: This initializes a loop control variable i with the value 1 before the loop starts.

  • i <= 5: This is the condition. As long as i is less than or equal to 5, the loop will continue to execute.

  • i++: This is the increment statement. After each iteration, the value of i is incremented by 1.

  • printf("Iteration %d\n", i);: This line prints the current value of i in the format "Iteration X" where X is the value of i.

  • return 0;: The main function returns an integer value (usually 0 for successful execution) to the operating system.

while Loop

#include <stdio.h>

int main() {
    int i = 0; // Initialization

    while (i < 5) { // Condition
        printf("Iteration %d\n", i);
        i++; // Iteration (incrementing i)
    }

    return 0;
}

OUTPUT:

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

explanation:

  • #include <stdio.h>: This line includes the standard input-output library, allowing you to use functions like printf.
  • int main(): This is the main function where the execution of the program starts.
  • int i = 0;: Here, we initialize an integer variable i with the value 0. This sets the starting point for the loop.
  • while (i < 5) {: This is the start of the while loop. The loop will continue executing the code block as long as the condition i < 5 is true.
  • printf("Iteration %d\n", i);: Inside the loop, this line uses the printf function to print the current value of i, which represents the iteration number.
  • i++;: After printing the iteration number, we increment the value of i by 1 using the ++ operator. This is the iteration step, and it's crucial to ensure that the loop eventually terminates.
  • The loop continues to execute until the condition i < 5 becomes false.
  • return 0;: This line ends the main function and indicates that the program executed successfully. The value 0 is returned to the operating system.

Do-while Loop

#include <stdio.h>

int main() {
    int count = 1;

    do {
        printf("Iteration %d\n", count);
        count++;
    } while (count <= 5);

    return 0;
}

OUTPUT:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

explanation:

  1. The #include <stdio.h> line includes the standard input-output library, which allows you to use functions like printf() for output.

  2. The main() function is the entry point of the program.

  3. We declare an integer variable count and initialize it with a value of 1.

  4. The do..while loop starts with the do keyword, followed by the code block to be executed.

  5. Inside the loop's code block, we use the printf() function to print the current iteration number using the value of the count variable.

  6. After printing, we increment the count variable using the count++ statement.

  7. The while keyword is followed by the condition (count <= 5). This condition is checked after each iteration. If the condition is true, the loop continues; otherwise, the loop terminates.

  8. The loop's code block will be executed at least once, even if the condition is initially false.

  9. The loop continues to execute as long as the condition (count <= 5) remains true.

  10. Once the condition becomes false (when count becomes 6), the loop terminates.

  11. The return 0; statement ends the main() function and indicates successful program execution to the operating system.

Conclusion

In this example, all three loop types achieve the same goal of iterating and printing a sequence of numbers. However, they differ in terms of their initialization, condition checking, and termination behavior.