bscodinglab

Looping

Looping or “iteration” is a programming construct that allows a block of code to be executed repeatedly as long as a certain condition is met. This is useful for situations where we need to perform a similar task multiple times or perform a task until a certain condition is satisfied. In C programming, there are three types of loops: the “while” loop, the “do-while” loop, and the “for” loop.

Here is a table that summarizes the three types of loops in C programming:

Loop Type Description Syntex
For Loop
Executes a block of code a fixed number of times.
for (initialization; condition; update) { /* block of code */ }
While Loop
Executes a block of code repeatedly as long as a certain condition is true.
while (condition) { /* block of code */ }
Do-While Loop
Executes a block of code repeatedly as long as a certain condition is true. The block of code is executed at least once, even if the condition is false.
do { /* block of code */ } while (condition);

1. While Loop

OutA while loop is a type of loop in programming that executes a block of code repeatedly as long as a specified condition is true.

In the example below, a while loop is used to print out the values of i from 0 to 4:

    Syntex :-

while (condition)

{

// code to be executed

}

     Example :-

let i = 0;

while (i < 5)

{

printf(i);

i++;

}

     Output :-   0 1 2 3 4

In each iteration of the loop, the condition i < 5 is evaluated. If it is true, the code within the curly braces is executed (in this case, printing the value of i to the console and incrementing its value by 1). The loop continues to iterate as long as the condition is true. Once i becomes equal to 5, the condition becomes false and the loop terminates.

2. Do-While Loop

A do-while loop is a type of loop in programming that executes a block of code at least once, and then repeatedly executes the block of code as long as a specified condition is true.

In the example below, a Do-while loop is used to print out the values of i from 0 to 4:

    Syntex :-

do

   {

       // code to be executed

   } while (condition);

     Example :-

let i = 0;

   do

   {

         printf(i);

         i++;

   } while (i < 5);

     Output :-   0 1 2 3 4

The code within the curly braces is executed at least once, and then the condition i < 5 is evaluated. If it is true, the loop continues to iterate (in this case, printing the value of i to the console and incrementing its value by 1). The loop continues to iterate as long as the condition is true. Once i becomes equal to 5, the condition becomes false and the loop terminates.

3. For Loop

In the C programming Lunguage, a for loop is a control flow statements used to repeatedly execute a block of code based on a specified condotion. it has  aspecified syntex that consist of three part “initialization, condition,increments/decriments” the gernal structure of for loop in C Language as Follow.

In the example below, Do-while loop is used to print out the values of i from 0 – 4:

    Syntex :-

For (Initialization; Condition;  Increment/Decrement)

   {

       // code to be executed

   }

     Example :-

let i ;

  for (i=0;i < 5;i++)

   {

         printf(i);

         i++;

   }

     Output :-   0 1 2 3 4

1 Initialization : is executed (one time) before the execution of the code block.

2 Conditions : defines the condition for executing the code block.

3 Incre/Decre : Increase or decrease a valueby 1 or Given value each time the code block in the loop has been executed.

4. Nested For Loop

It is also possible to place a loop inside another loop. This is called a nested loop.
The “inner loop” will be executed one time for each iteration of the “outer loop”:

In the example below, a Do-while loop is used to print out the values of i from 0 to 4:

    Syntex :-

for ( initialization; condition; increment ) 
{
   for ( initialization; condition; increment ) 
    {
       // statement of inside loop
    }
   // statement of outer loop
}

Example :-

#include <stdio.h>
int main()
{
      int arr[2][2];
      for(int i=0; i < 2; i++)
          {
              for(int j=0; j < 2; ++j)
                {
                      printf(“*\t”);
                 }
                printf(“\n”);
           }
        return 0;
   }
 Output :-  * *
            * *
  1. First, the ” i ” variable is initialized to 0 and then program control passes to the ” i<2 “.
  2. The program control checks whether the condition ” i<2 ” is true or not.
  3. If the condition is true, then the program control passes to the inner loop.
  4. The inner loop will get executed until the condition is true.
  5. After the execution of the inner loop, the control moves back to the outer loop, and  “i++” the value of i is increments  by 1.
  6. After incrementing the value of ” i “, the condition is checked again, ” i<2 “.
  7. If the condition is true, then the inner loop will be executed again.
  8. This process will continue until the condition of the outer loop is true.
Note :-  We are Also write the code of nested while loop and nested do-while loop like Outer loop having  another inner loop.

5. Infinite For Loop

An infinite loop in C is a loop that continues executing indefinitely without ever terminating. It occurs when the condition specified in the loop control statement always evaluates to true or when there is no condition provided, resulting in an endless repetition of the loop body.

Here’s an Syntex of an infinite loop in C:

    While loop :-

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

    For Loop :-

for ( ; ; )
{
     // code to be executed
}

In this case, the absence of a condition means that there is nothing to evaluate, resulting in an infinite loop.

It’s important to note that infinite loops can be intentional or unintentional. Intentional infinite loops are used in specific scenarios where continuous execution is desired, such as server programs or event-driven applications. However, in most cases, infinite loops are unintended and can cause programs to hang or become unresponsive.

To break out of an infinite loop, you typically use certain control flow statements within the loop body. For example: Break And Return.