Loop in C Program
Loop in C Program
C PROGRAMMING
Discussion :
1. Introduction
2. Types of Loop
3. Nested Loop
4. Choosing the Right Loop
5. Common mistakes
6. Conclusion
Introduction to Loops:
• Loops are essential control structures that allow repetitive execution of code.
‘do-while’ Loop
The ‘for’ Loop
• In this example, the outer loop iterates over rows, and the inner loop
iterates over columns.
• Each iteration of the inner loop prints the current row and column indices.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i);
}
return 0;
Output: }
Lorna Alvarado
The ‘while’ Loop
Syntex
while (condition) {
// code to be executed repeatedly
}
Example of a ‘while’ Loop
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("Iteration: %d\n", i);
i++; Output:
}
return 0;
}
The ‘do-while’ Loop
• Similar to while loop but guarantees at least one execution
of the loop body.
• The condition is checked after each iteration.
Syntex
do {
// code to be executed repeatedly
} while (condition);
Example of a ‘do-while’ Loop
#include <stdio.h>
int main() {
int i = 0;
do {
Output:
printf("Iteration: %d\n", i);
i++;
} while (i < 5);
return 0;
}
Nested Loop:
• Use do-while when you want the loop body to execute at least once.
Common mistakes
• Forgetting to update the loop variable.