Skip to main content

Loops In C

Ayesha
EditReport

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.

Video Explanationโ€‹

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!

Track Your Progress

Done with this topic? Mark it as complete to track your progress.