Loops in C
Loops are control structures that allow the execution of a block of code repeatedly as long as a specified condition is met.

Common types of loops in C are: for
, while
, and do-while
.
for Loop:
Repeats a block of code a specific number of times. It is also known as Entry-Controlled loop as the condition is checked before the execution of code inside the loop body .
for (initialization; condition; increment/decrement) { // code block}
while Loop:
Repeats a block of code while a condition is true. It is also Entry-Controlled loop
while (condition) { // code block}
do-while Loop:
Executes code block at least once, then repeats while condition is true. It is also known as Exit-Controlled loop as the condition is checked after loop is executed
do { // code block} while (condition);
Code Example
Section titled “Code Example” #include <stdio.h>
int main(){ printf("\n\n*******Loop in C*********\n\n"); int index = 0;
for(int j = 0; j <= 100; j++) { //FOR LOOP => A for loop is a repetition control structure that allows us to efficiently // write a loop that will execute a specific number of times. printf("%d\n", j); }
while (index <= 10) { // WHILE LOOP => The while loop allows code to be executed multiple times, depending upon a // boolean condition. It is terminated on the basis of the Boolean (true or false) test condition. printf("%d\n", index); index++; }
int j=0; do { printf("do while loop is running"); // DOWHILE LOOP => The main difference between the do-while loop and while loop is that, in the do-while loop, // the condition is tested at the end of the loop body, whereas the other two loops are entry controlled loops. } while (j>5); // Note: In do-while loop, the loop body will execute at least once irrespective of the condition.
return 0; }
*******Loop in C*********
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 0 1 2 3 4 5 6 7 8 9 10 do while loop is running