Loops in C

Posted on November 1, 2023 by Vishesh Namdev
Python C C++ Java
C Programming Language

Loop is a control structure that allows you to repeatedly execute a block of code as long as a certain condition is true. Loops are used to perform repetitive or iterative tasks. They provide a way to execute the same code multiple times, making your programs more efficient and concise.
"In C programming, there are three main types of loops:

1. The 'for' loop.
2. The 'while' loop.
3. The 'do-while' loop."

Loops in C++

for Loop

'for' loop in C is like a recipe that helps you cook a meal a certain number of times. It has three important ingredients: the starting point (initialization), the condition that tells you when to stop, and the step you take between each cooking process (iteration). This loop structure ensures you can repeat a set of cooking instructions precisely as many times as you want.
In other words, a 'for' loop in C lets you automate a specific task for a known number of repetitions by providing the recipe's beginning, end, and the steps to take in between.

Syntax of for Loop:
for (initialization; condition; iteration)
    // Code to be executed repeatedly

While Loop

A while loop in C is like a safety check before doing something repeatedly. It allows you to execute a block of code multiple times as long as a specific condition holds true. You can think of it as a "do this while that is true" structure. Unlike for loops where you know in advance how many times you'll repeat a task, while loops are used when you don't have a precise count of iterations in mind. The loop keeps running as long as the condition remains true. When the condition evaluates to false, the loop stops, ensuring the code is only executed while the condition is met.

Syntax of While Loop:
while (condition) {
   // Code to be executed repeatedly

do-while Loop

The do-while loop is like a while loop, with one crucial distinction: in a do-while loop, we test the condition after executing the loop's body. This means that, unlike a regular while loop, the do-while loop guarantees that the body will run at least once, regardless of the initial test condition's outcome.

Syntax of do-while Loop:
do {
// Code to be executed repeatedly
} while (condition);

Infinite Loop

An infinite loop is a loop that continues to execute indefinitely, with no natural termination point. In programming, this is usually an unintended situation and can lead to your program becoming unresponsive or consuming excessive CPU resources. It's crucial to avoid creating infinite loops in your code.

An infinite loop can occur when the loop's condition is always true, or if there is no condition provided at all. Here's the syntax for an infinite loop in C:

// Code that executes indefinitely
for (;;) {
  // Your code here
Or using a while loop:
// Code that executes indefinitely
while (1) {
  // Your code here

In both cases, there is no specific condition that will ever evaluate to false, so the loop will keep running without a natural exit point. You should always ensure that your loops have a condition that can eventually become false, allowing the loop to terminate when the desired condition is met.