for Loop in C Language with Examples

The for loop is an entry-controlled loop in C language. It is used to execute a block of statements repeatedly as long as the given condition is true. In a for loop, initialization, condition, and updation are generally written together in the loop header. The three parts of a for loop are separated by semicolons (;). The initialization part is executed only once at the beginning. The condition is checked before every iteration, and the updation part is executed after the loop body. If the condition is true (non-zero), the statements inside the loop are executed. When the condition becomes false (zero), the loop terminates and program control moves to the next statement after the loop.

C भाषा में for एक Entry-Controlled Loop है। इसका उपयोग किसी Statement या Statements के समूह को तब तक बार-बार निष्पादित करने के लिए किया जाता है, जब तक दी गई Condition True रहती है। for Loop में Initialization, Condition और Updation को सामान्यतः एक ही Loop Header में लिखा जाता है। for Loop के तीनों भागों को Semicolon (;) से अलग किया जाता है। Initialization वाला भाग शुरुआत में केवल एक बार निष्पादित होता है। प्रत्येक Iteration से पहले Condition की जाँच होती है और Loop Body निष्पादित होने के बाद Updation वाला भाग निष्पादित होता है। यदि Condition True (Non-zero) होती है, तो Loop के अंदर लिखे गए Statements निष्पादित होते हैं। जब Condition False (Zero) हो जाती है, तब Loop समाप्त हो जाता है और प्रोग्राम का कंट्रोल Loop के बाद वाले अगले Statement पर पहुँच जाता है।

The for loop is especially useful when the number of iterations is known or can be determined in advance. It is commonly used for counting, processing arrays, generating tables, and repeating a task a fixed number of times. The initialization, condition, and updation expressions in a for loop are optional. If the initialization and updation parts are omitted and the condition is written in the loop header, a for loop can work similarly to a while loop.

for Loop विशेष रूप से तब उपयोगी होता है जब Iterations की संख्या पहले से ज्ञात हो या निर्धारित की जा सकती हो। इसका उपयोग Counting, Arrays को Process करने, Tables बनाने और किसी कार्य को निश्चित बार दोहराने के लिए किया जाता है। for Loop में Initialization, Condition और Updation Expressions को आवश्यकता के अनुसार छोड़ा भी जा सकता है। यदि Initialization और Updation वाले भाग हटा दिए जाएँ और Condition Loop Header में लिखी हो, तो for Loop while Loop के समान कार्य कर सकता है।

Syntax of for Loop

for (initialization; condition; updation)
{
statements;
}

Explanation of Syntax:

  • Initialization: It initializes the loop-control variable. It is executed only once.

  • Condition: It is checked before every iteration. The loop continues while the condition is true.

  • Updation: It changes the value of the loop-control variable after each iteration.

  • Statements: These are the instructions executed repeatedly inside the loop.

सिंटैक्स का विवरण:

  • Initialization: इसमें Loop-Control Variable को प्रारंभिक वैल्यू दी जाती है। यह केवल एक बार निष्पादित होता है।

  • Condition: प्रत्येक Iteration से पहले इसकी जाँच की जाती है। Condition True रहने तक Loop चलता रहता है।

  • Updation: प्रत्येक Iteration के बाद Loop-Control Variable की वैल्यू बदली जाती है।

  • Statements: ये वे Instructions हैं जो Loop के अंदर बार-बार निष्पादित होते हैं।

Working of for Loop

The working of a for loop can be understood through the following steps:

  1. Initialization is performed once.

  2. The condition is checked.

  3. If the condition is true, the loop body is executed.

  4. The updation expression is executed.

  5. Control returns to the condition.

  6. When the condition becomes false, the loop terminates.

for Loop की कार्यप्रणाली को निम्नलिखित चरणों से समझा जा सकता है:

  1. सबसे पहले Initialization किया जाता है।

  2. इसके बाद Condition की जाँच होती है।

  3. यदि Condition True होती है, तो Loop Body निष्पादित होती है।

  4. इसके बाद Updation Expression निष्पादित होता है।

  5. कंट्रोल वापस Condition पर पहुँचता है।

  6. जब Condition False हो जाती है, तो Loop समाप्त हो जाता है।

Example 1: C Program to Print Numbers from 1 to 10 Using for Loop

This program uses a for loop to display numbers from 1 to 10.

यह प्रोग्राम for Loop का उपयोग करके 1 से 10 तक की संख्याएँ प्रदर्शित करता है।

C Program:

#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

Explanation:

Initially, the variable i is initialized to 1. The condition i <= 10 is checked before each iteration. If it is true, the value of i is printed, and then i is incremented by 1. This process continues until i becomes 11.

प्रारंभ में Variable i को 1 से Initialize किया जाता है। प्रत्येक Iteration से पहले i <= 10 Condition की जाँच होती है। यदि Condition True होती है, तो i की वैल्यू प्रिंट होती है और फिर i को 1 से Increment किया जाता है। यह प्रक्रिया तब तक चलती है जब तक i की वैल्यू 11 नहीं हो जाती।

Example 2: C Program to Print Even Numbers from 1 to 20 Using for Loop

This program displays even numbers from 1 to 20 using a for loop.

यह प्रोग्राम for Loop का उपयोग करके 1 से 20 तक की सम संख्याएँ प्रदर्शित करता है।

C Program:

#include <stdio.h>
int main(void)
{
int i;
for (i = 2; i <= 20; i = i + 2)
{
printf("%d ", i);
}
return 0;
}

Output:

2 4 6 8 10 12 14 16 18 20

Explanation:

The variable i starts with 2 and increases by 2 after each iteration. Therefore, only even numbers are printed. The loop terminates when i becomes greater than 20.

इस प्रोग्राम में Variable i की शुरुआत 2 से होती है और प्रत्येक Iteration के बाद इसकी वैल्यू 2 से बढ़ाई जाती है। इसलिए केवल सम संख्याएँ प्रिंट होती हैं। जब i की वैल्यू 20 से अधिक हो जाती है, तब Loop समाप्त हो जाता है।

Example 3: C Program to Find the Sum of Natural Numbers from 1 to N Using for Loop

This program calculates the sum of natural numbers from 1 to the number entered by the user.

यह प्रोग्राम यूजर द्वारा इनपुट की गई संख्या तक की सभी प्राकृतिक संख्याओं का योग ज्ञात करता है।

C Program:

#include <stdio.h>
int main(void)
{
int n, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}

Output:

Enter a number: 5
Sum = 15

Explanation:

The variable sum is initialized to 0. The for loop starts with i = 1 and adds each value of i to sum until i becomes greater than n.

इस प्रोग्राम में Variable sum को 0 से Initialize किया जाता है। for Loop की शुरुआत i = 1 से होती है और i की प्रत्येक वैल्यू को sum में जोड़ा जाता है। यह प्रक्रिया तब तक चलती है जब तक i की वैल्यू n से अधिक नहीं हो जाती।

Example 4: C Program to Find the Factorial of a Number Using for Loop

Factorial of a positive integer is the product of all positive integers from 1 to that number. For example, the factorial of 5 is 120.

किसी धनात्मक पूर्णांक का Factorial उस संख्या से 1 तक की सभी धनात्मक संख्याओं का गुणनफल होता है। उदाहरण के लिए, 5 का Factorial 120 होता है।

C Program:

#include <stdio.h>
int main(void)
{
int n, i;
unsigned long long fact = 1;
printf("Enter a non-negative number: ");
scanf("%d", &n);
if (n < 0)
{
printf("Factorial is not defined for negative numbers.");
return 0;
}
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
printf("Factorial = %llu", fact);
return 0;
}

Output:

Enter a non-negative number: 5
Factorial = 120

Explanation:

The variable fact is initialized to 1. The for loop multiplies fact by each integer from 1 to n. After the loop terminates, the factorial is displayed. The result must fit within the range of unsigned long long.

इस प्रोग्राम में Variable fact को 1 से Initialize किया जाता है। for Loop में 1 से n तक प्रत्येक पूर्णांक को fact से Multiply किया जाता है। Loop समाप्त होने के बाद Factorial प्रदर्शित किया जाता है। परिणाम unsigned long long की निर्धारित सीमा के अंदर होना चाहिए।

Example 5: C Program to Print Multiplication Table Using for Loop

This program displays the multiplication table of a number entered by the user.

यह प्रोग्राम यूजर द्वारा इनपुट की गई संख्या का Multiplication Table प्रदर्शित करता है।

C Program:

#include <stdio.h>
int main(void)
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}

Output:

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation:

The loop starts with i = 1 and continues up to i = 10. In each iteration, the program multiplies the entered number by i and displays the result.

इस Loop की शुरुआत i = 1 से होती है और यह i = 10 तक चलता है। प्रत्येक Iteration में प्रोग्राम इनपुट की गई संख्या को i से Multiply करता है और परिणाम प्रदर्शित करता है।

Example 6: for Loop Without Initialization and Updation

The initialization and updation parts of a for loop are optional. They can be omitted when the loop-control variable is initialized and updated elsewhere in the program.

for Loop में Initialization और Updation वाले भाग वैकल्पिक होते हैं। जब Loop-Control Variable का Initialization और Updation प्रोग्राम में किसी अन्य स्थान पर किया जाता है, तब इन भागों को छोड़ा जा सकता है।

C Program:

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

Output:

1 2 3 4 5

Explanation:

In this program, initialization is performed before the for loop, and updation is performed inside the loop body. Therefore, the initialization and updation expressions are left empty in the for header. The loop works similarly to a while loop.

इस प्रोग्राम में Initialization को for Loop से पहले और Updation को Loop Body के अंदर किया गया है। इसलिए for Header में Initialization और Updation Expressions खाली रखे गए हैं। यह Loop while Loop के समान कार्य करता है।

Important Points About for Loop

  1. The for loop is an entry-controlled loop because its condition is checked before executing the loop body.
    for एक Entry-Controlled Loop है, क्योंकि इसमें Loop Body निष्पादित होने से पहले Condition की जाँच होती है।

  2. Initialization is executed only once at the beginning of the loop.
    Initialization Loop की शुरुआत में केवल एक बार निष्पादित होता है।

  3. The condition is checked before every iteration.
    प्रत्येक Iteration से पहले Condition की जाँच होती है।

  4. Updation is normally executed after the loop body.
    Updation सामान्यतः Loop Body के निष्पादन के बाद होता है।

  5. The three parts of a for loop are separated by semicolons (;).
    for Loop के तीनों भाग Semicolon (;) से अलग किए जाते हैं।

  6. Initialization, condition, and updation expressions are optional.
    Initialization, Condition और Updation Expressions वैकल्पिक होते हैं।

  7. If the condition is false initially, the loop body is not executed even once.
    यदि शुरुआत में ही Condition False होती है, तो Loop Body एक बार भी निष्पादित नहीं होती है।

  8. If the condition is omitted, it is treated as always true, resulting in an infinite loop unless the loop is terminated by a statement such as break.
    यदि Condition को छोड़ दिया जाता है, तो इसे हमेशा True माना जाता है। ऐसी स्थिति में Loop Infinite हो सकता है, जब तक कि break जैसे Statement द्वारा इसे समाप्त न किया जाए।

Difference Between for and while Loop

for Loopwhile Loop
Initialization, condition, and updation are generally written in one loop header.Initialization and updation are generally written separately.
It is useful when the number of iterations is known or determined in advance.It is useful when the number of iterations depends on a condition.
The three parts are separated by semicolons.Only the condition is written in the loop header.
It can also omit initialization and updation.Initialization and updation are generally managed outside and inside the loop body.

for Loop
while Loop
इसमें Initialization, Condition और Updation सामान्यतः एक ही Loop Header में लिखे जाते हैं।इसमें Initialization और Updation सामान्यतः अलग-अलग लिखे जाते हैं।
यह तब उपयोगी होता है जब Iterations की संख्या पहले से ज्ञात या निर्धारित हो।यह तब उपयोगी होता है जब Iterations की संख्या किसी Condition पर निर्भर करती है।
इसके तीनों भाग Semicolon से अलग होते हैं।इसके Loop Header में केवल Condition लिखी जाती है।
इसमें Initialization और Updation को छोड़ा भी जा सकता है।इसमें Initialization और Updation सामान्यतः Loop Body के बाहर और अंदर Manage किए जाते हैं।

The for loop is a simple and useful looping statement in C language. It is especially suitable for programs that require a fixed or predictable number of iterations. It helps keep initialization, condition, and updation together, making loop control easier to understand.

for Loop C भाषा का एक सरल और उपयोगी Looping Statement है। यह विशेष रूप से उन प्रोग्राम्स के लिए उपयुक्त है जिनमें Iterations की संख्या निश्चित या अनुमानित होती है। इसमें Initialization, Condition और Updation एक साथ लिखे जाते हैं, जिससे Loop को नियंत्रित करना और समझना आसान होता है.

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025