Skip to main content

Loops In C

Hey there! In this guide, we'll explore loops in C. Loops are used to execute a block of code repeatedly based on specific conditions. Let's dive in!

  • C provides several types of loops that allow you to execute a block of code multiple times based on specific conditions.
  • The main types of loops in C are for, while, and do-while.

1. For Loop​

The for loop is used when you know how many times you want to execute a statement or a block of statements.

Syntax:​

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

Example:​

#include <stdio.h>

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

Output:​

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

2. While Loop​

The while loop is used when you want to execute a block of code as long as a specified condition is true.

Syntax:​

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

Example:​

#include <stdio.h>

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

Output:​

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

3. Do-While Loop​

The do-while loop is similar to the while loop, except that it guarantees that the code block will be executed at least once before the condition is tested.

Syntax:​

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

Example:​

#include <stdio.h>

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

Output:​

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

4. Nested Loops​

You can also use loops inside other loops, which are called nested loops.

Example:​

#include <stdio.h>

int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("Outer Loop: %d, Inner Loop: %d\n", i, j);
}
}
return 0;
}

Outer Loop: 1, Inner Loop: 1
Outer Loop: 1, Inner Loop: 2
Outer Loop: 2, Inner Loop: 1
Outer Loop: 2, Inner Loop: 2
Outer Loop: 3, Inner Loop: 1
Outer Loop: 3, Inner Loop: 2


5. Break and Continue Statements​

a. Break Statement​

The break statement is used to exit a loop prematurely.

Example:​

#include <stdio.h>

int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("Iteration %d\n", i);
}
return 0;
}

Output:​

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

b. Break Statement​

The continue statement skips the current iteration and proceeds to the next one.

Example:​

#include <stdio.h>

int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the iteration when i equals 2
}
printf("Iteration %d\n", i);
}
return 0;
}


Output:​

Iteration 0
Iteration 1
Iteration 3
Iteration 4



Loops are essential for controlling the flow of execution in your C++ programs, enabling you to perform repetitive tasks efficiently. Understanding how to use them effectively will greatly enhance your programming skills!