Loops in C Language
Understanding For, While, and Do-
While Loops
Overview of Loops
• • Loops are used to execute a block of code
repeatedly.
• • Types of loops in C:
• - For Loop
• - While Loop
• - Do-While Loop
For Loop
• • Used when the number of iterations is
known beforehand.
• • Syntax:
• for(initialization; condition;
increment/decrement) {
• // Code to be executed
• }
• • Example:
• for(int i = 0; i < 5; i++) {
While Loop
• • Used when the number of iterations is not
known in advance.
• • Syntax:
• while(condition) {
• // Code to be executed
• }
• • Example:
• int i = 0;
• while(i < 5) {
Do-While Loop
• • Similar to while loop, but condition is
checked after execution.
• • Syntax:
• do {
• // Code to be executed
• } while(condition);
• • Example:
• int i = 0;
• do {
Key Differences Between Loops
• • For Loop:
• - Condition checked before the loop.
• - Number of iterations known beforehand.
• • While Loop:
• - Condition checked before the loop.
• - Suitable for runtime-dependent conditions.
• • Do-While Loop: