Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C
  4. Understanding Loops in C: for, while, and do-while

Understanding Loops in C: for, while, and do-while

Date- Mar 09,2026 150
c programming loops

Overview of Loops in C

Loops are fundamental programming constructs that allow you to execute a block of code repeatedly based on a specified condition. They are essential for tasks that require repetition, such as processing items in a collection, performing calculations multiple times, or iterating through data structures. Understanding how to use loops effectively is crucial for writing efficient and clean code.

Prerequisites

  • Basic knowledge of C programming syntax
  • Understanding of variables and data types
  • Familiarity with conditional statements
  • Access to a C compiler or development environment

for Loop in C

The for loop is one of the most commonly used loops in C. It is particularly useful when the number of iterations is known beforehand. The syntax of a for loop consists of three main components: initialization, condition, and increment/decrement.

Syntax

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

Let’s see a complete example of a for loop:

#include 

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

In this code:

  • #include <stdio.h>: This line includes the standard input-output library needed for using the printf function.
  • int main() {...}: This is the main function where the program execution starts.
  • for (int i = 0; i < 5; i++) {...}: This initializes the loop variable i to 0, sets the condition to continue looping while i is less than 5, and increments i by 1 after each iteration.
  • printf("Iteration: %d\n", i);: This line prints the current iteration number to the console.
  • return 0;: This indicates that the program has executed successfully.

while Loop in C

The while loop is another type of loop that executes a block of code as long as a specified condition remains true. It is particularly useful when the number of iterations is not known in advance.

Syntax

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

Let’s look at an example of a while loop:

#include 

int main() {
    int i = 0;
    while (i < 5) {
        printf("Iteration: %d\n", i);
        i++;
    }
    return 0;
}

In this code:

  • int i = 0;: This initializes the loop variable i to 0.
  • while (i < 5) {...}: This loop continues to execute as long as i is less than 5.
  • printf("Iteration: %d\n", i);: This prints the current iteration number to the console.
  • i++;: This line increments i by 1 after each iteration.
  • return 0;: This indicates successful execution of the program.

do-while Loop in C

The do-while loop is similar to the while loop, but with one key difference: the block of code inside the loop is executed at least once before the condition is tested. This makes it useful when you want to ensure the code runs before checking the condition.

Syntax

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

Here’s an example of a do-while loop:

#include 

int main() {
    int i = 0;
    do {
        printf("Iteration: %d\n", i);
        i++;
    } while (i < 5);
    return 0;
}

In this code:

  • int i = 0;: Initializes the loop variable i to 0.
  • do {...} while (i < 5);: This executes the block of code at least once, then checks if i is less than 5 to determine if it should continue looping.
  • printf("Iteration: %d\n", i);: Prints the current iteration number.
  • i++;: Increments i by 1 after each iteration.
  • return 0;: Indicates successful execution of the program.

Best Practices and Common Mistakes

When working with loops in C, consider the following best practices and common pitfalls:

  • Ensure loop termination: Always verify that your loop has a clear termination condition to avoid infinite loops.
  • Keep the loop body simple: A complex loop body can lead to bugs; keep it simple and clear.
  • Use meaningful variable names: Name your loop variables descriptively to enhance code readability.
  • Test edge cases: Test your loops with edge cases, such as zero or negative values, to ensure they behave as expected.

Conclusion

In this blog post, we explored the three main types of loops in C: the for, while, and do-while loops. Each loop serves a distinct purpose and is essential for controlling program flow. Remember to always ensure loop termination, keep your code simple, and test for edge cases. Mastering these loops will significantly enhance your programming skills in C.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Abstract Classes in C#: A Comprehensive Guide
Mar 16, 2026
Mastering File Handling in C Programming: A Comprehensive Guide
Mar 12, 2026
Understanding Structures in C Programming: A Comprehensive Guide
Mar 12, 2026
Understanding Functions in C Programming: A Comprehensive Guide
Mar 10, 2026
Previous in C
Mastering Control Structures in C: The if-else and switch Stateme…
Next in C
Understanding Operators in C Programming: A Comprehensive Guide
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,938 views
  • 2
    Error-An error occurred while processing your request in .… 11,272 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 235 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,459 views
  • 5
    Mastering JavaScript Error Handling with Try, Catch, and F… 161 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,497 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,232 views

On this page

More in C

  • Mastering Unconditional Statements in C: A Complete Guide wi… 21497 views
  • Understanding C: A Complete Guide with Examples 5147 views
  • Mastering Unconditional Statements in C: A Complete Guide wi… 4217 views
  • Mastering 2-D Arrays in C: A Complete Guide with Examples 3935 views
  • Introduction to C: A Step-by-Step Guide with Examples 3586 views
View all C posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor