Loop in C with Examples: For, While, Do..While Loops

Loop in C with Examples: For, While, Do..While Loops

27 May 2024
Beginner
55.6K Views
12 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

Loop in C: An Overview

Are you interested in programming but don't know where to start? Have you ever heard the term loop? Looping is one of the key concepts behind programming, and learning how to use Loop in C can open up a new world of code. Gain the foundational skills from this C tutorial and move to the advanced C training thatincludes in-depth coverage of loop and other essential programming concepts.

Read More - Top 50 C Interview Questions and Answers

What are Loop in C?

Loops are a block of code that executes itself until the specified condition becomes false. In this section, we will look in detail at the types of loops used in C programming.

What is the Need for Looping Statements in C?

Here are some uses of loops in C:

  • Loops allow the user to execute the same set of statements repeatedly without writing the same code multiple times.
  • It saves time and effort and increases the efficiency.
  • It reduces the chance of getting errors during compilation.
  • Loop makes the code readable and easier to understand, especially when dealing with complex logic or large data sets.
  • It promotes code reusability.
  • Loops help traverse data structures like arrays or linked lists

Types of Loop in C

Let’s get into the three types of loops used in C programming.
  1. for loop
  2. while loop
  3. do while loop

  1. for loop in C

A for loop is a control structure that enables a set of instructions to get executed for a specified number of iterations. It is an entry-controlled loop.

for loop Flowchart

for loop in C

Syntax

for(initialization; test condition; update expression){
//code to be executed
}
  • Here, the initialization statement is executed first and only once.
  • The test condition is checked, if false the loop terminates
  • If the test condition is true, the body of the loop executes
  • The update expression gets updated
  • Again the test condition is evaluated
  • The process repeats until the test condition becomes false.

Example: For loop in C Compiler


// Program to print numbers from 1 to 10
#include <stdio.h> 

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

The above code prints the numbers from 1 to 10 using a for loop in C.

  • We know it will take 10 iterations to print 10 numbers so, we have used the for loop.
  • i is initialized to 0.
  • The condition i<10 will be checked. It is true, therefore i+1 i.e. 1 gets printed.
  • Then i increments to 1 again the condition, i<10 is evaluated.
  • The process will repeat until i become 10.

Output

1
2
3
4
5
6
7
8
9
10

  1. while loop in C

It repeatedly carries out a series of instructions till a condition is true. It is an entry-controlled loop. The while loop in C is used when we don’t know the number of iterations.
while loop Flowchart

while loop in c programming

Syntax

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

If the test condition inside the () becomes true, the body of the loop executes else loop terminates without execution. The process repeats until the test condition becomes false.

Example: while loop in C


// Print numbers from 1 to 10
#include <stdio.h>
int main() 
{
 int i = 1;
 while (i <= 10) {
 printf("%d\n", i);
 i++;
 }
 return 0;
}
  • In the above code, i is initialized to 1 before the start of the loop.
  • The test condition, i<=10 is evaluated. If true, the body of the loop executes.
  • If the condition becomes false in the beginning itself, the program control does not even enter the loop once.
  • The loop executes until i becomes 10.

Output

1
2
3
4
5
6
7
8
9
10

  1. do...while loop in C

It is an exit-controlled loop. It prints the output at least once before checking the condition. Afterwards, the condition is checked and the execution of the loop begins.
 do...while loop Flowchart

Syntax

do{
//code to be executed
}while(test condition);
  • The body of the loop executes before checking the condition.
  • If the test condition is true, the loop body executes again.
  • Again the test condition is evaluated.
  • The process repeats until the test condition becomes false.

Example: do...while loop in C


#include <stdio.h>
int main() 
{
 int i = 0;
 do {
 printf("%d\n", i+1);
 i++;
 }
 while (i < 10);
 return 0;
}
  • The above code prints numbers from 1 to 10 using the do while loop in C.
  • It prints 1 before checking if i less than 10.
  • It checks the condition i<10 and executes until i becomes 10

Output

1
2
3
4
5
6
7
8
9
10
Summary
Loops in C have a broad range of applications, from loop-driven algorithms to iterative problem-solving. As demonstrated, the syntax for using these loops is relatively straightforward, although their logic must be carefully explored to determine advantage and ease of use. Thanks to this design, developers can bring a wide range of creativity to their code. Take your proficiency in C programming to the next level by pursuing a C Certification, which will validate your expertise in loops and other crucial concepts.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this