Skip to main content

Command Palette

Search for a command to run...

Loops in C

Updated
3 min read
J

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.

At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.

In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.

Looping in C is a fundamental concept that allows you to execute a block of code repeatedly. There are three primary types of loops in C:

  1. for loop

  2. while loop

  3. do-while loop

Each type of loop can be used depending on the specific requirement of the situation.

1. for loop

The for loop is used when the number of iterations is known before entering the loop. The syntax is:

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

Sample Code:

#include <stdio.h>

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

This will output: 0 1 2 3 4

2. while loop

The while loop is used when the number of iterations is not known and depends on a condition. The loop will continue as long as the condition is true.

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

Sample Code:

#include <stdio.h>

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

This will output: 0 1 2 3 4

3. do-while loop

The do-while loop is similar to the while loop, but the code block will execute at least once because the condition is evaluated after the block is executed.

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

Sample Code:

#include <stdio.h>

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

This will output: 0 1 2 3 4

Nested Loops

Loops can be nested within other loops to perform multi-dimensional iteration.

Sample Code:

#include <stdio.h>

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

This will output a 3x3 grid of coordinates:

(0, 0) (0, 1) (0, 2) 
(1, 0) (1, 1) (1, 2) 
(2, 0) (2, 1) (2, 2)

Infinite Loops

An infinite loop occurs when the terminating condition will never be false. These are often used deliberately to keep a program running until an external event occurs.

Sample Code:

#include <stdio.h>

int main() {
    while (1) {
        // This code will run forever
    }
    return 0;
}

Loop Control Statements

  • break; - Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

  • continue; - Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Sample Code with break and continue:

#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 10; i++) {
        if (i == 5) {
            break; // Exit the loop when i is 5
        }
        if (i % 2 == 0) {
            continue; // Skip the rest of the loop body for even numbers
        }
        printf("%d ", i);
    }
    return 0;
}

This will output: 1 3

Loops are a powerful feature in C that allow for efficient repetition of code blocks. They are used in almost every non-trivial program to perform tasks repeatedly.

C Programming

Part 1 of 50

More from this blog

Jyotiprakash's Blog

251 posts

I'm Jyotiprakash, a software dev and professor at KIIT, with expertise in system programming.