C – Loops

C – Loops

Loops in programming are essential constructs that allow developers to execute a block of code repeatedly until a specified condition is met. They eliminate the need to duplicate code, making programs more efficient, concise, and maintainable. Loops in programming are essential constructs that allow developers to execute a block of code repeatedly until a specified condition is met. They eliminate the need to duplicate code, making programs more efficient, concise, and maintainable.

// C program to illustrate need of loops
#include <stdio.h>
  
int main()
{
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
    printf( "Hello World\n");
      
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

There are two types of Loop C in Programming

Entry-Controlled Loops: These loops check if a condition is true before they start doing something. If the condition is false from the beginning, they might not do anything at all. For example, imagine you want to count from 1 to 5, but you’ll only do it if you have a reason to start counting.

Exit-Controlled Loops: These loops start doing something first and then check if a condition is true or false after they’ve done it at least once. So, no matter what, they’ll do something at least once. For instance, think of it like counting from 1 to 5, and you’ll start counting, no matter what, and check if you should stop after you’ve counted.

Loop Type And their Description

for loopfirst Initializes, then the condition is monitored, then it will execute the body and at last, the update is done.
while loop first Initializes, then the condition is monitored, and then it will lead to the execution of the body, and updating can be inside the body.
do-while loopdo-while first will execute the body and, later the condition check is done.

For Loop

The “for” loop is indeed a repetition control structure that allows programmers to execute a block of code a specific number of times. It provides a compact and convenient way to perform a series of steps repeatedly in a single line of code.

Syntax of For Loop

for (initialize expression; test expression; update expression)
{
    //
    // body of for loop
    //
}

Example

for(int i = 0; i < n; ++i)
{
    printf("Body of for loop which will execute till n");
}
  1. Initialization Expression: This part sets the initial value of the loop control variable. It is executed only once at the beginning of the loop. For example: int i = 1; initializes the loop counter i to 1.
  2. Test Expression (Condition): This part is a condition that is evaluated before each repetition of the loop. If the condition is true, the loop body is executed. If it’s false, the loop terminates. For example: i <= 9; checks if i is less than or equal to 9.
  3. Update Expression: After each iteration of the loop body, this expression is executed to update the loop control variable. It can involve incrementing, decrementing, or modifying the variable in some way. For example: i++; increments i by 1 after each repetition.
// C program to illustrate for loop
#include <stdio.h>
  
// Driver code
int main()
{
  int i = 0;
   
  for (i = 1; i <= 10; i++)
  {
    printf( "Hello World\n");  
  }
  return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

While Loop

  1. For” Loop: In a “for” loop, the number of iterations is typically known in advance because you specify the initialization, condition, and update expressions when setting up the loop. You have precise control over how many times the loop will run.
  2. “While” Loop: In a “while” loop, the number of iterations is not predetermined. Instead, the loop continues to execute as long as a specified condition is true. If the condition becomes false at any point, the loop terminates, and the control moves out of the loop.

Syntax

initialization_expression;

while (test_expression)
{
    // body of the while loop
    
    update_expression;
}
// C program to illustrate
// while loop
#include <stdio.h>
  
// Driver code
int main()
{
  // Initialization expression
  int i = 2;
  
  // Test expression
  while(i < 10)
  {
    // loop body
    printf( "Hello World\n");  
     
    // update expression
    i++;
  }
   
  return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

do-while Loop

In the do-while Loop, the test condition is executed at the end of the body. The Loop body will execute at least once irrespective of the test condition.

Syntax

initialization_expression;
do
{
    // body of do-while loop
    
    
    update_expression;

} while (test_expression);
// C program to illustrate
// do-while loop
#include <stdio.h>
  
// Driver code
int main()
{
  // Initialization expression
  int i = 2;
   
  do
  {
    // loop body
    printf( "Hello World\n");  
  
    // Update expression
    i++;
     
    // Test expression
  }  while (i < 1);  
   
  return 0;
}

Output

Hello World

In this program, it will evaluate (i<1) as false since i = 2. But still, as it is a do-while loop the body will be executed once.

Loop Control Statements 

break statement The break statement means to terminate the switch and loop statement. It will transfer the execution to the statement immediately following the loop or switch. 
continue statementGo statement transfers the control to the labeled statement.
goto statementGo statement will transfer the control to the labeled statement.

Infinite Loop

An infinite loop occurs when the test condition of a loop never becomes false, causing the loop’s body to execute repeatedly without end. This can lead to a program becoming stuck or unresponsive. However, this loop can be solved using Loop Control statements.

Using for Loop

// C program to demonstrate infinite
// loops using for loop
#include <stdio.h>
  
// Driver code
int main ()
{
  int i;
   
  // This is an infinite for loop
  // as the condition expression
  // is blank
  for ( ; ; )
  {
    printf("This loop will run forever.\n");
  }
   
  return 0;
}

Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Using while Loop

// C program to demonstrate
// infinite loop using while
// loop
#include <stdio.h>
  
// Driver code
int main()
{
  while (1)
    printf("This loop will run forever.\n");
  return 0;
}

Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Using the do-while Loop

// C program to demonstrate
// infinite loop using do-while
// loop
#include <stdio.h>
 
// Driver code
int main()
{
  do
  {
    printf("This loop will run forever.\n");
  } while (1);
   
  return 0;
}

Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

FAQ- C – Loops

Q1. What are the 3 types of loops in C?

Ans. while loop.
nested loop.
for loop.
do-while loop.

Q2. What is loop syntax?

Ans. The syntax of a for loop in C programming language – for ( init; condition; increment ) { statement(s); }

Q3. What are the loops in C?

Ans. , loops are used to execute a block of code multiple times, as long as a specified condition or set of conditions remains true. They provide a way to automate repetitive tasks and allow for efficient iteration through data structures, making it easier to work with data and perform various operations in programs.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment