Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you loop in C

+20 votes
asked Jan 6 by Rhinozz (220 points)

6 Answers

+2 votes
answered Jan 7 by Prasooj Narayan (180 points)
for (initialization; condition; update) {
  // code block to be executed
}
 

// Initialization should be done before the loop
while (condition) {
  // code block to be executed
  // update statement (increment/decrement)
}
0 votes
answered Jan 7 by Chaturya Kalidindi (140 points)
We use the looping statements

we use for,while or do-while loops

you can learn about this clearly with the help of AI agents
+2 votes
answered Jan 7 by Ines Moura (180 points)

In C, there are three main types of loops: for, while, and do-while. Here's how each works with clear examples.

1. for loop (most common when you know the number of iterations)

C

#include <stdio.h>

int main() {
    // Print numbers from 0 to 9
    for (int i = 0; i < 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Structure:

C

for (initialization; condition; update) {
    // code to repeat
}
  • initialization: Runs once at the start (e.g., int i = 0)
  • condition: Checked before each iteration (loop runs while true)
  • update: Runs after each iteration (e.g., i++)

You can also have infinite loops:

C

for (;;) {
    // runs forever (or until break)
}

2. while loop (checks condition before entering)

C

#include <stdio.h>

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

Structure:

C

while (condition) {
    // code to repeat
}

The body runs only if the condition is true at the start.

Infinite while:

C

while (1) {
    // runs forever
}

3. do-while loop (checks condition after executing body at least once)

C

#include <stdio.h>

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

Structure:

C

do {
    // code to repeat
} while (condition);

Note the semicolon ; at the end.

This guarantees the loop body runs at least once, even if the condition is initially false.

Loop control statements

  • break;: Immediately exits the loop
  • continue;: Skips the rest of the current iteration and goes to the next

C

for (int i = 0; i < 10; i++) {
    if (i == 5) continue;  // skip 5
    if (i == 8) break;     // stop at 8
    printf("%d\n", i);
}

Nested loops

You can put loops inside loops (e.g., for 2D arrays):

C

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

Choose the loop type based on your needs:

  • Use for when you know the exact number of iterations.
  • Use while when the end condition is more complex.
  • Use do-while when the body must run at least once (e.g., menu prompts).
commented Jan 19 by Grishma Karmacharya (110 points)
now this is what a real student is like..! Good job keep it up !! ‍️️️️️️️️️️️️
0 votes
answered Jan 9 by Sneh Damasiya (140 points)
#include<stdio.h>

int main{

for(int i=0;i<=5;i++){

printf("%d\n",i);

}

}
0 votes
answered Jan 10 by Charan Reddy (140 points)
#include <stdio.h>

int main() {
    int i = 1;
    do {
        printf("C Programming\n");
        i++;
    } while(i <= 3);
    return 0;
}

OUTPUT:

C Programming
0 votes
answered Jan 25 by Mohit kumar (220 points)

Looping In C:

A loop in C is used to repeat a block of code again and again until a condition becomes false.

Example:
Printing numbers from 1 to 5 without loop:

printf("1\n");
printf("2\n");
printf("3\n");
printf("4\n");
printf("5\n");

 using Loop We Solve easily 

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

Types of Loop :

There are mainly tree types of loop in c 

1. For Loop : 

When the number of condition is known we use For loop There are three parts in for lop 

first is called initialization  where we initialize the value, second is called condition where we check the condition  and third is called increment or decrement this is entry control loop.

Synatx:

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

EX: 

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

2. While Loop :

A while loop repeats a task as long as a given condition is true, this is entry control loop.

Syntax:

while(condition) {
    // code
}

EX:

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

3. do-while Loop:

A do-while loop executes the code at least once, even if the condition is false, it is also called exit control loop.

Syntax:

do {
    // code
} while(condition);

Ex:

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

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...