0% found this document useful (0 votes)
4 views4 pages

Loops

The document provides comprehensive notes on loops in C programming, detailing three types: for loop, while loop, and do...while loop, along with their syntax and examples. It also covers loop control statements such as break and continue, as well as nested loops. Best practices for using loops are highlighted, emphasizing the importance of valid termination conditions and code readability.

Uploaded by

wambuif376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Loops

The document provides comprehensive notes on loops in C programming, detailing three types: for loop, while loop, and do...while loop, along with their syntax and examples. It also covers loop control statements such as break and continue, as well as nested loops. Best practices for using loops are highlighted, emphasizing the importance of valid termination conditions and code readability.

Uploaded by

wambuif376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sure!

Here are comprehensive notes on Loops in C programming, covering types, syntax, and
examples:

🌀 Loops in C Programming

📘 Introduction

🔁 Types of Loops in C

1. for loop
2. while loop
3. do...while loop

1. ✅ for Loop

Used when the number of iterations is known.

Syntax:

for(initialization; condition; increment/decrement) {


// Code to be executed
}

Example:

#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
2. 🔄 while Loop

Used when the number of iterations is unknown and depends on a condition.

Syntax:

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

Example:

#include <stdio.h>
int main() {
int i = 1;
while(i <= 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}

3. 🔁 do...while Loop

Executes the code at least once, then checks the condition.

Syntax:

do {
// Code to be executed
} while(condition);
Example:

#include <stdio.h>
int main() {
int i = 1;
do {
printf("i = %d\n", i);
i++;
} while(i <= 5);
return 0;
}

🧱 Loop Control Statements

🛑 break

 Exits the loop immediately.

for(int i = 1; i <= 10; i++) {


if(i == 5) break;
printf("%d\n", i);
}

🔁 continue

 Skips the current iteration and moves to the next one.

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


if(i == 3) continue;
printf("%d\n", i);
}

💡 Nested Loops
 A loop inside another loop.

Example:

for(int i = 1; i <= 3; i++) {


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

🧠 Best Practices

 Always ensure loops have a valid termination condition.


 Avoid infinite loops unless intentionally required.
 Use meaningful variable names and proper indentation.

You might also like