Loops in C

Last Updated :

Loops are a fundamental control structure in programming that allow a section of code to be executed repeatedly. In the C programming language, there are three types of loops: the for loop, the while loop, and the do-while loop.

1. for loop: The for loop is used to execute a block of code a specific number of times. It has the following syntax:

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

Here, initialization is the code that is executed before the loop starts, condition is the condition that is checked before each iteration of the loop, and increment/decrement is the code that is executed after each iteration of the loop. The loop continues as long as the condition is true.

For example, the following for loop prints the numbers from 1 to 10:

for (int i = 1; i <= 10; i++) {
    printf("%d ", i);
}

2. while loop: The while loop is used to execute a block of code as long as a certain condition is true. It has the following syntax:

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

Here, condition is the condition that is checked before each iteration of the loop. The loop continues as long as the condition is true.

For example, the following while loop prints the numbers from 1 to 10:

int i = 1;
while (i <= 10) {
    printf("%d ", i);
    i++;
}

3. do-while loop: The do-while loop is similar to the while loop, but the condition is checked at the end of the loop instead of the beginning. This means that the loop is guaranteed to be executed at least once. It has the following syntax:

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

Here, condition is the condition that is checked at the end of each iteration of the loop. The loop continues as long as the condition is true.

For example, the following do-while loop prints the numbers from 1 to 10:

int i = 1;
do {
    printf("%d ", i);
    i++;
} while (i <= 10);

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 21 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read