Introduction
Programming में loops का उपयोग तब किया जाता है जब किसी statement या statement group को बार-बार execute करना हो।
अगर loop न हो, तो same code को कई बार लिखना पड़ेगा, जिससे program लंबा और difficult हो जाएगा।
C language में loops mainly repetitive tasks के लिए use होते हैं, जैसे:
- counting
- table printing
- sum निकालना
- input validation
- menu driven program
C में तीन मुख्य loops होते हैं:
- while loop
- do-while loop
- for loop
while Loop
Definition
while loop एक entry-controlled loop है, जिसमें सबसे पहले condition check होती है। यदि condition true होती है, तभी loop body execute होती है।
Syntax
while (condition)
{
// statements
}
Example 1: Numbers Print
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
return 0;
}
Output:
1 2 3 4 5
Example 2: Even Numbers Print
#include <stdio.h>
int main()
{
int i = 2;
while (i <= 10)
{
printf("%d ", i);
i = i + 2;
}
return 0;
}
Output:
2 4 6 8 10
Example 3: Password Validation
#include <stdio.h>
int main()
{
int password = 0;
while (password != 1234)
{
printf("Enter password: ");
scanf("%d", &password);
}
printf("Access Granted");
return 0;
}
Real World Example
while loop का use वहाँ होता है जहाँ condition पहले check करनी हो, जैसे:
- login system
- password verification
- sensor reading जब तक valid value न मिले
do-while Loop
Definition
do-while loop एक exit-controlled loop है, जिसमें loop body पहले execute होती है और condition बाद में check होती है।
इसलिए यह loop कम से कम एक बार जरूर चलता है।
Syntax
do
{
// statements
}
while (condition);
Example 1: Numbers Print
#include <stdio.h>
int main()
{
int i = 1; do
{
printf("%d ", i);
i++;
}
while (i <= 5);
return 0;
}
Output:
1 2 3 4 5
Example 2: Menu Display
#include <stdio.h>
int main()
{
int choice; do
{
printf("\n1. Add");
printf("\n2. Delete");
printf("\n3. Exit");
printf("\nEnter choice: ");
scanf("%d", &choice); }
while (choice != 3);
return 0;
}
Example 3: At Least One Input
#include <stdio.h>
int main()
{
int num;
do
{
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0);
return 0;
}
Real World Example
do-while loop उन situations में useful है जहाँ program को कम से कम एक बार run होना ही चाहिए, जैसे:
- menu driven program
- form input prompt
- retry option
for Loop
Definition
for loop एक entry-controlled loop है, जिसमें initialization, condition और update एक ही line में लिखा जाता है।
यह fixed number of iterations के लिए सबसे अधिक useful होता है।
Syntax
for (initialization; condition; update)
{
// statements
}
Example 1: Numbers Print Karna
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
printf("%d ", i);
} return 0;
}
Output:
1 2 3 4 5
Example 2: Multiplication Table
#include <stdio.h>
int main()
{
int i, n = 2;
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", n, i, n * i);
} return 0;
}
Example 3: Sum of First 5 Numbers
#include <stdio.h>
int main()
{
int i, sum = 0;
for (i = 1; i <= 5; i++)
{
sum = sum + i;
} printf("Sum = %d", sum);
return 0;
}
Output:
Sum = 15
Example 4: Factorial Program
#include <stdio.h>
int main()
{
int i, fact = 1, n = 5;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
printf("Factorial = %d", fact);
return 0;
}
Technical Real World Example
for loop उन problems में use होता है जहाँ iterations पहले से known हों, जैसे:
- marks का total निकालना
- array traversal
- table generation
- factorial calculation
Difference Between while, do-while, and for
| while | do-while | for |
|---|---|---|
| Condition पहले check होती है | Condition बाद में check होती है | Initialization, condition, update एक ही line में |
| 0 बार भी run हो सकता है | कम से कम 1 बार run होगा | Fixed iterations के लिए suitable |
| Input validation में useful | Menu driven programs में useful | Counting और calculations में useful |
Exam Points
- while और for loops entry-controlled होते हैं
- do-while loop exit-controlled होता है
- do-while कम से कम एक बार जरूर execute होता है
- for loop fixed number of repetitions के लिए best होता है
- while loop unknown iterations के लिए useful होता है
- loops से output-based questions अक्सर exam में पूछे जाते हैं