Loops in C

Loops in C. Loops in C Tutorial. Learn Loops in C
Loops in C

What are Loops in C?

Loops in C are used to execute a block of code repeatedly until a certain condition is met. They are essential programming structures that allow a programmer to simplify repetitive tasks and minimize the amount of code needed to perform an operation.

Types of Loops in C

There are three types of loops in C:

  1. for loop
  2. while loop
  3. do-while loop

1. for loop

The for loop is a pre-test loop in which a block of code is executed repeatedly for a specific number of times. It is the most commonly used loop in C programming. The for loop consists of three parts: initialization, condition, and increment/decrement. The loop initializes a variable, sets a condition that must be true for the loop to continue, and increments or decrements the variable on each iteration.

Syntax:

for(initialization; condition; increment/decrement)
{
   // statements to be executed repeatedly
}

Example:

#include <stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 5; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 3 4 5

Explanation:

In this example, the for loop initializes the variable i to 1, sets the condition that the loop will continue until i is less than or equal to 5, and increments i by 1 after each iteration. The loop then prints the value of i each time it executes, resulting in the output shown above.

2. while loop

The while loop is a pre-test loop in which a block of code is executed repeatedly as long as a condition is true. It is useful when the number of iterations is unknown or when the condition is based on user input or some external factors.

Syntax:

while(condition)
{
    // statements to be executed repeatedly
}

Example:

#include <stdio.h>

int main()
{
    int i = 1;

    while(i <= 5)
    {
        printf("%d ", i);
        i++;
    }

    return 0;
}

Output:

1 2 3 4 5

Explanation:

In this example, the while loop executes the block of code as long as the condition i <= 5 is true. The loop prints the value of i each time it executes and increments i by 1 until i is equal to 6, at which point the loop terminates.

3. do while loop

The do-while loop is a post-test loop in which a block of code is executed at least once, and then repeatedly as long as a condition is true. It is useful when the block of code must be executed at least once, and the condition is checked after the block of code is executed.

Syntax:

do
{
    // statements to be executed repeatedly
} while(condition);

Example:

#include <stdio.h>

int main()
{
    int i = 1;

    do
    {
        printf("%d ", i);
        i++;
    } while(i <= 5);

    return 0;
}

Output:

1 2 3 4 5

Explanation:

In this example, the do-while loop executes the block of code at least once, then repeatedly as long as the condition i <= 5 is true. The loop prints the value of i each time it executes and increments i by 1 until i is equal to 6, at which point the loop terminates.

Rules to follow when using Loops in C:

  • Make sure the loop condition will eventually be false or the loop will continue indefinitely, causing an infinite loop.
  • Declare variables outside the loop or initialize them inside the loop to avoid unexpected results.
  • Use break and continue statements with caution, as they can affect the behavior of the loop and make the code harder to understand.

Modifications that can be made when using Loops in C:

  1. Nesting loops to perform complex operations
  2. Using the break and continue statements to control the flow of the loop
  3. Using the goto statement to jump to a specific point in the code
  4. Using the loop index as an array index to perform operations on arrays
  5. Modifying the loop increment or decrement to control the number of iterations or perform specific operations within the loop
  6. Using additional conditions in the loop condition to control the number of iterations or perform specific operations within the loop.

We will see each loop in detail in further chapters.

More: