ADVERTISEMENT
ADVERTISEMENT

What are the different types of Looping Statements in C ?

In C, a looping statement or Iterative Statement is a control flow statement that allows a certain block of code to be executed repeatedly a specified number of times or until a certain condition is met. The looping turns difficult problems into simple ones. It permits us to change the program's flow so that we can repeat the same code a set amount of times rather of writing it again. For instance, if we need to display the first 20 natural numbers, we can do so within a loop that performs up to 20 iterations rather than by repeatedly using the printf statement.

Advantages of using loops in C

  1. Efficiency: Loops are used to execute a block of code repeatedly, which can save a lot of time and effort when performing repetitive tasks.

  2. Readability: Loops make code more readable by breaking down complex tasks into simpler, more manageable chunks. This makes it easier to understand and debug code.

  3. Control: Loops provide a way to control the flow of a program, allowing the programmer to specify the number of times a particular block of code should be executed or to stop the execution of the loop based on certain conditions.

  4. Conditional Statements: Loops can be used in conjunction with conditional statements, such as if and else, to create more powerful and flexible programs.

  5. Flexibility: Loops can be nested, allowing for the creation of more complex control flow structures.

  6. Reusability: Loops can be used to create reusable code blocks that can be used in multiple parts of a program, reducing the need to rewrite similar code.

  7. Ease of use: loops are quite simple and easy to use even for beginners.

Types of Loops in C

These  are the types of loops in C language that is given below:

  1. While Loop
  2. For Loop
  3. Do While Loop
  4. Nested Loop

While Loop Statements in C 

In C, the while loop is a control flow statement that allows a certain block of code to be executed repeatedly while a certain condition is true. The while loop has only one part, the condition.

The syntax for the while loop is as follows:

while (condition)
{
    // code to be executed
}

The condition is evaluated before each iteration of the loop, and if it is true, the code block inside the loop is executed. When the condition becomes false, the loop is terminated. This is typically used to test a variable against a limit value.

Here is an example of a while loop in C:

#include<stdio.h>
in main()
{
int i = 0;
while (i < 5)
 {
    printf("%d ", i);
    i++;
 }
return 0;
}

This loop will print the numbers from 0 to 4.

In the example, variable i is initialized to 0, and the while loop continues to execute as long as i is less than 5. The code inside the loop increments i each time, and when i is no longer less than 5, the loop exits and the program continues to execute the code after the while loop.

It's important to note that if the condition provided in the while loop is always true, the loop will run indefinitely, leading to infinite loop. This is why it's important to make sure the condition will eventually become false, and the loop will terminate

For loop Statement in C

In C, the for loop is a control flow statement that allows a certain block of code to be executed repeatedly for a specified number of times or until a certain condition is met. The for loop has three parts: the initialization, the condition, and the increment/decrement.

The syntax for the for loop is as follows:

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

The initialization statement is executed only once, before the loop starts. This is typically used to set a counter variable to an initial value.

The condition is evaluated before each iteration of the loop, and if it is true, the code block inside the loop is executed. If the condition is false, the loop is terminated. This is typically used to test the counter variable against a limit value.

The increment/decrement statement is executed after each iteration of the loop. This is typically used to increment or decrement the counter variable.

Here are some examples of for loop in C:

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

This loop will print the numbers from 0 to 4.

Do-While Loop Statement in C

In C, the do-while loop is a control flow statement that is similar to the while loop. The main difference is that the code block inside a do-while loop is guaranteed to be executed at least once, whereas in a while loop, the code block may not be executed at all if the condition is false at the start.

The syntax for the do-while loop is as follows:

do
{
    // code to be executed

}while(condition);

The code block inside the do-while loop is executed at least once and then repeatedly as long as the condition is true. Once the condition is false, the loop is terminated and the program continues to execute the code after the do-while loop.

Here is an example of a do-while loop in C:

int i = 0;
do
{
    printf("%d ", i);
    i++;

}while(i < 5);

This loop will print the numbers from 0 to 4, just like the while loop example given above.

In the example, i is initialized to 0, and the do-while loop first execute one time and then will check the condition. as long as i is less than 5, the loop continues to execute. The code inside the loop increments i each time, and when i is no longer less than 5, the loop exits and the program continues to execute the code after the do-while loop.

Just like the while loop, in do-while loop it's important to update the condition in the loop body otherwise it will also cause an infinite loop.

Nested Loop in C

In C, a nested for loop is a for loop that is used inside another for loop. Nested for loops are used to perform a certain task multiple times, where the outer loop controls the number of times the inner loop is executed.

The syntax for a nested for loop is as follows:

for (initialization1; condition1; increment/decrement1)
{
    // code to be executed in the first loop
    
   for (initialization2; condition2; increment/decrement2) 
    {
        // code to be executed in the second loop
    }
}

Here is an example of a nested for loop in C:

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

You can nest loops of any type, for example you can nest for loop inside while loop and vice versa. It's important to keep an eye on the number of loops and variables when using nested loops to avoid confusion and errors. Also, the variable of the outer loop should not be the same as the variable of the inner loop.

Nested loops can be used to perform various tasks like multi-dimensional array traversal, matrix manipulation, and many more. They can be used to implement many algorithms and data structures, such as search and sorting algorithms, tree traversals, and many more.


ADVERTISEMENT

ADVERTISEMENT