C Programming में Decision Making और Loops/Looping: आसान तरीके से समझें

क्या आप अपने C Programs और दूसरे Programs जैसे: C++, Java, Python को ज्यादा स्मार्ट और बेहतर बनाना चाहते हैं?

Decision Making आपके Program को यह तय करने में मदद करता है कि उसे क्या करना है, जबकि Loops एक ही काम को बार-बार आसानी से करने में मदद करते हैं।

इस लेख या Article में आप if, switch, for, while और do-while को आसान उदाहरणों के साथ समझेंगे।

इस Article को पढ़ने के बाद, C Program के Flow को Control करना आपके लिए काफी आसान हो जाएगा।

कुछ Extra Tips भी दिए गए हैं, जो आपको बेहतर और Efficient C Programs लिखने में मदद करेंगे।

इसके अलावा, Practice के लिए कुछ आसान Programs भी दिए गए हैं। अगर आप इन सभी Programs को अच्छी तरह समझकर और लगातार Practice करेंगे, तो आप बड़े से बड़े Programs को भी आसानी से समझ और बना सकेंगे।

🧑‍💻 Chapter 1: C Programming की मूल बातें
📜 Chapter 2: C Language का इतिहास और विकास
⚙️ Chapter 3: C Compiler Install करना
💻 Chapter 4: अपना पहला C Program लिखना
🏗️ Chapter 5: C Program की Structure
📦 Chapter 6: Variables और Data Types
🔢 Chapter 7: C में Operators
⌨️ Chapter 8: Input और Output Functions
🔀 Chapter 9: Decision Making और Branching – Conditional Statements
🔄 Chapter 10: C Programming में Decision Making और Loops – वर्तमान Chapter
📊 Chapter 11: Arrays
🔤 Chapter 12: Strings
🛠️ Chapter 13: Functions
👉 Chapter 14: Pointers
🏛️ Chapter 15: Structures और Unions
📁 Chapter 16: File Handling

Loop, C Programming का एक ऐसा control structure है, जिसकी मदद से हम किसी काम को बार-बार करवा सकते हैं, जब तक दी गई condition सही रहती है।

मान लीजिए आपको 1 से 100 तक के numbers print करने हैं।

अगर loop का इस्तेमाल न करें, तो आपको बहुत सारी printf() statements लिखनी पड़ेंगी:

printf("1");
printf("2");
printf("3");

और इसी तरह 100 तक लिखना पड़ेगा।

इससे program बहुत लंबा और मुश्किल हो जाएगा।

लेकिन loop की मदद से यही काम कुछ ही lines में किया जा सकता है:

for (int i = 1; i <= 100; i++) {
    printf("%d ", i);
}

इसलिए Loop का मुख्य उद्देश्य बार-बार होने वाले काम को आसान बनाना और program के code को छोटा, साफ और समझने में आसान बनाना है।

C Programming में मुख्य रूप से तीन प्रकार के loops होते हैं:

  • while
  • for
  • do-while

Loops Programming के बहुत महत्वपूर्ण concepts में से एक हैं।

जब किसी काम को बार-बार करना हो, तब loops का इस्तेमाल किया जाता है।

उदाहरण के लिए loops की मदद से हम:

  • Numbers print कर सकते हैं।
  • Factorial निकाल सकते हैं।
  • Fibonacci series बना सकते हैं।
  • Prime numbers खोज सकते हैं।
  • Arrays को process कर सकते हैं।
  • Multiplication table बना सकते हैं।
  • Number को reverse कर सकते हैं।
  • Palindrome number check कर सकते हैं।
  • Armstrong number check कर सकते हैं।
  • अलग-अलग patterns print कर सकते हैं।
  • User से बार-बार input ले सकते हैं।
  • किसी calculation को कई बार कर सकते हैं।
आसान उदाहरण
for (int i = 1; i <= 5; i++) {
    printf("Hello\n");
}
Output
Hello
Hello
Hello
Hello
Hello

अगर loop का इस्तेमाल नहीं करते, तो printf() को पाँच बार लिखना पड़ता।

Loop की मदद से हम केवल एक बार printf() लिखकर उसे पाँच बार चला सकते हैं।

Condition कब check होती है, इसके आधार पर loops को दो categories में बाँटा जाता है।

1. Entry-Controlled Loop

इसमें condition को loop शुरू होने से पहले check किया जाता है।

C में दो Entry-Controlled Loops हैं:

  • while
  • for

2. Exit-Controlled Loop

इसमें loop का body पहले execute होता है और condition बाद में check होती है।

C में एक Exit-Controlled Loop है:

  • do-while

Entry-Controlled Loop में condition को loop body execute होने से पहले check किया जाता है।

अगर condition true है, तभी loop body execute होगी।

अगर condition शुरुआत में ही false है, तो loop body एक बार भी execute नहीं होगी

Entry-Controlled Loops के उदाहरण
  • while
  • for
उदाहरण
int i = 10;

while (i < 5) {
    printf("%d", i);
}

यहाँ condition है:

10 < 5

यह condition false है।

इसलिए loop का body एक बार भी execute नहीं होगा

while एक Entry-Controlled Loop है।

इसमें सबसे पहले condition check होती है। अगर condition true है, तभी loop body execute होती है।

जब तक condition true रहती है, loop बार-बार चलता रहता है।

जैसे ही condition false होती है, loop रुक जाता है।

Syntax
Initialization;

while (test condition)
{
    // Loop Body

    // Updation
}

Statement X;
ध्यान रखें

while loop में condition को loop body से पहले check किया जाता है।

इसलिए अगर condition शुरुआत में ही false है, तो loop body एक बार भी execute नहीं होगी।

Flow Chart:

इस program को देखें:

int i = 1;

while (i <= 5) {
    printf("%d ", i);
    i++;
}

इसका execution इस प्रकार होगा।

Step 1
i = 1

Condition:

1 <= 5 → true

इसलिए 1 print होगा।

Step 2

अब:

i = 2

Condition:

2 <= 5 → true

इसलिए 2 print होगा।

इसी तरह process चलता रहेगा।

जब:

i = 6

तब:

6 <= 5 → false

इसलिए loop रुक जाएगा।

#include <stdio.h>

int main(void) {
    int n, i = 1;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    while (i <= n) {
        printf("%d ", i);
        i++;
    }

    return 0;
}
Example

अगर input है:

5

तो output होगा:

1 2 3 4 5
Program की आसान Explanation
  • i = 1 → counting की शुरुआत करता है।
  • i <= n → condition check करता है।
  • printf() → number print करता है।
  • i++i की value को 1 बढ़ाता है।
  • जब i, n से बड़ी हो जाती है, तो loop रुक जाता है।

while loop की मदद से किसी भी number का multiplication table print किया जा सकता है।

#include <stdio.h>

int main(void) {
    int n, i = 1;

    printf("Enter the number: ");
    scanf("%d", &n);

    while (i <= 10) {
        printf("%d x %d = %d\n", n, i, n * i);
        i++;
    }

    return 0;
}

अगर input है:

8

तो output होगा:

8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

जब एक loop के अंदर दूसरा loop लिखा जाता है, तो उसे Nested Loop कहते हैं।

उदाहरण:

while (condition1) {

    while (condition2) {
        // Inner Loop
    }

}

यहाँ:

  • बाहर वाला loop → Outer Loop
  • अंदर वाला loop → Inner Loop

Nested loops का उपयोग अक्सर इन कामों में किया जाता है:

  • Multiplication tables
  • Patterns
  • Matrices
  • Rows और columns
  • Two-dimensional data
#include <stdio.h>

int main(void) {
    int i = 1;

    while (i <= 10) {
        int j = 1;

        while (j <= 10) {
            printf("%5d", i * j);
            j++;
        }

        printf("\n");
        i++;
    }

    return 0;
}
यह कैसे काम करता है?

Outer loop:

1 से 10

तक चलता है।

Inner loop भी:

1 से 10

तक चलता है।

इसलिए program अलग-अलग multiplication करता है:

1 × 1
1 × 2
...
10 × 10

Fibonacci Series इस प्रकार होती है:

0 1 1 2 3 5 8 13 21 34 ...

इस series में अगला number पिछले दो numbers को जोड़कर बनाया जाता है।

उदाहरण:

0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
#include <stdio.h>

int main(void) {
    int n;
    int i = 3;
    long long a = 0, b = 1, c;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Please enter a positive number.");
    } else if (n == 1) {
        printf("0");
    } else {
        printf("0 1");

        while (i <= n) {
            c = a + b;
            printf(" %lld", c);

            a = b;
            b = c;
            i++;
        }
    }

    return 0;
}

अगर input है:

7

तो output होगा:

0 1 1 2 3 5 8

for loop भी एक Entry-Controlled Loop है।

इसमें सामान्यतः तीन चीजें एक साथ लिखी जाती हैं:

  1. Initialization
  2. Condition
  3. Updation

सबसे पहले initialization होता है।

इसके बाद condition check होती है।

अगर condition true है, तो loop body execute होती है।

फिर update होता है और condition दोबारा check होती है।

Syntax
for (initialization; test condition; updation)
{
    // Loop Body
}

Flow Chart:

इस program को देखें:

for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}

1. Initialization

int i = 1

यह केवल शुरुआत में एक बार execute होता है।

2. Condition

i <= 5

यह हर iteration से पहले check होती है।

3. Update

i++

यह हर iteration के बाद execute होता है।

#include <stdio.h>

int main(void) {

    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }

    return 0;
}
Output
1 2 3 4 5

किसी positive number से लेकर 1 तक सभी numbers को multiply करने पर factorial मिलता है।

उदाहरण:

5! = 5 × 4 × 3 × 2 × 1
   = 120
#include <stdio.h>

int main(void) {
    int n;
    unsigned long long fact = 1;

    printf("Enter a non-negative integer: ");
    scanf("%d", &n);

    if (n < 0) {
        printf("Factorial is not defined for negative numbers.");
        return 0;
    }

    for (int i = 1; i <= n; i++) {
        fact *= i;
    }

    printf("Factorial = %llu", fact);

    return 0;
}

अगर input:

5

तो output:

Factorial = 120
महत्वपूर्ण बात (Important for You)

Mathematics के अनुसार:

0! = 1

इसलिए program 0 के लिए भी 1 result देता है।

Prime Number वह number है जो 1 से बड़ा हो और जिसके केवल दो positive divisors हों:

  • 1
  • वही number

उदाहरण:

2, 3, 5, 7, 11, 13, 17...
#include <stdio.h>

int main(void) {
    int n;
    int isPrime = 1;

    printf("Enter the number: ");
    scanf("%d", &n);

    if (n <= 1) {
        isPrime = 0;
    }

    for (int i = 2; i * i <= n && isPrime; i++) {
        if (n % i == 0) {
            isPrime = 0;
        }
    }

    if (isPrime) {
        printf("%d is a prime number.", n);
    } else {
        printf("%d is not a prime number.", n);
    }

    return 0;
}

अगर input:

17

तो output:

17 is a prime number.
#include <stdio.h>

int main(void)
{
    int N, i, flag;

    for (N = 11; N <= 100; N++)
    {
        flag = 0;

        for (i = 2; i <= N / 2; i++)
        {
            if (N % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
        {
            printf("%5d", N);
        }
    }

    return 0;
}
Output
11   13   17   19   23   29   31   37   41   43
47   53   59   61   67   71   73   79   83   89
97
आसान Explanation
  • Outer for loop → 11 से 100 तक numbers check करता है।
  • हर number के लिए flag = 0 किया जाता है।
  • Inner for loop → number के factors check करता है।
  • अगर कोई factor मिल जाता है, तो flag = 1 हो जाता है।
  • break inner loop को रोक देता है।
  • अगर flag अभी भी 0 है, तो number prime है।

इस तरह की series का sum भी loop की मदद से निकाला जाता है।

#include <stdio.h>
#include <math.h>

int main(void)
{
    int N, x, i;
    int s = 0;

    printf("Enter the value of N and x: ");
    scanf("%d %d", &N, &x);

    for (i = 1; i <= N; i++)
    {
        s = s + (int)pow(x, i);
    }

    printf("Sum = %d", s);

    return 0;
}

अगर:

N = 3
x = 5

तो:

S = 5¹ + 5² + 5³
  = 5 + 25 + 125
  = 155
Output
Enter the value of N and x: 3 5
Sum = 155
#include <stdio.h>
#include <math.h>

int main(void)
{
    int N, x, i, j, fact;
    float s = 0;

    printf("Enter the value of N and x: ");
    scanf("%d %d", &N, &x);

    for (i = 1; i <= N; i++)
    {
        fact = 1;

        for (j = 1; j <= 2 * i - 1; j++)
        {
            fact = fact * j;
        }

        s = s + pow(-1, i) * pow(x, 2 * i) / fact;
    }

    printf("Sum = %f", s);

    return 0;
}
उदाहरण

मान लीजिए:

N = 3
x = 2

तब:S=221!+243!265!S=-\frac{2^2}{1!}+\frac{2^4}{3!}-\frac{2^6}{5!}

अब मान निकालते हैं:S=41+16664120S=-\frac{4}{1}+\frac{16}{6}-\frac{64}{120}S=4+2.6666670.533333S=-4+2.666667-0.533333S=1.866667\boxed{S=-1.866667}

अतः Series का योग = -1.866667

#include <stdio.h>

int main(void)
{
    int N, i, j, s = 0, R;

    printf("Enter the value of N: ");
    scanf("%d", &N);

    for (i = 1; i <= N; i++)
    {
        R = 0;

        for (j = 1; j <= i; j++)
        {
            R = R + j;
        }

        s = s + R;
    }

    printf("Result = %d", s);

    return 0;
}
उदाहरण

यदि N = 3 है:S=(1)+(1+2)+(1+2+3)S=(1)+(1+2)+(1+2+3)

पहले प्रत्येक bracket का योग निकालते हैं:S=1+3+6S=1+3+6S=10S=10

अतः परिणाम = 10

आउटपुट
Enter the value of N: 3
Result = 10

मान लीजिए कोई संख्या Composite Number (भाज्य संख्या) है।

अगर उस संख्या का कोई factor उसके वर्गमूल (Square Root) से बड़ा है, तो उसका एक दूसरा factor वर्गमूल से छोटा या बराबर जरूर होगा।

इसलिए किसी संख्या के Prime Number होने की जाँच करने के लिए उसके वर्गमूल तक के factors को जाँचना ही पर्याप्त होता है।

इससे प्रोग्राम अधिक तेज़ और efficient हो जाता है, क्योंकि हमें n तक के सभी numbers को check नहीं करना पड़ता।

जब एक for loop के अंदर दूसरा for loop होता है, उसे Nested for Loop कहते हैं।

Syntax
for (initialization; condition; update) {

    for (initialization; condition; update) {
        // Inner Loop
    }

}

Nested for loops का उपयोग मुख्य रूप से:

  • Patterns
  • Multiplication tables
  • Matrices
  • Rows और columns
  • Multidimensional data

में किया जाता है।

Flow Chart:

Nested for Loop से Pattern Print करना

हमें यह pattern print करना है:

1 2 3
1 2 3
1 2 3
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 3; i++)
    {
        for (j = 1; j <= 3; j++)
        {
            printf("%d ", j);
        }

        printf("\n");
    }

    return 0;
}
Output
1 2 3
1 2 3
1 2 3
यह कैसे काम करता है?
  • Outer for loop → rows को control करता है।
  • Inner for loop → हर row में numbers print करता है।
  • Outer loop की हर एक iteration में inner loop 3 बार चलता है।

Exit-Controlled Loop में सबसे पहले loop body execute होती है।

इसके बाद condition check की जाती है।

इसलिए इसका body कम से कम एक बार जरूर execute होता है

C में do-while एक Exit-Controlled Loop है।

do-while loop एक ऐसा loop है जिसमें पहले statements को execute किया जाता है और बाद में condition check की जाती है। इसलिए यह loop कम से कम एक बार जरूर चलता है।

Syntax
Initialization;

do
{
    // Loop Body

    // Updation

}
while (test condition);

Statement X;
ध्यान रखें

do-while में condition body execute होने के बाद check होती है।

इसलिए body कम से कम एक बार execute होती है।

Flow Chart:

#include <stdio.h>

int main(void) {
    int i = 1;

    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);

    return 0;
}
Output
1 2 3 4 5

इस program को देखें:

int i = 10;

do {
    printf("%d", i);
} while (i < 5);

Condition है:

10 < 5

जो false है।

फिर भी output मिलेगा:

10

क्योंकि do-while में पहले body execute होती है और बाद में condition check होती है।

यही while और do-while के बीच सबसे बड़ा difference है।

किसी number को reverse करने के लिए हम उसका last digit निकालते हैं।

Last digit निकालने के लिए:

digit = number % 10;

और last digit हटाने के लिए:

number = number / 10;
#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int reverse = 0;

    printf("Enter the number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    } while (n > 0);

    printf("Original number = %d\n", num);
    printf("Reversed number = %d", reverse);

    return 0;
}
Explanation:
  • num में user से number लिया जाता है।
  • n = num करके number की copy बनाई जाती है।
  • n % 10 से last digit निकाला जाता है।
  • reverse = reverse * 10 + digit से digits को उल्टा करके जोड़ा जाता है।
  • n /= 10 से last digit हटाई जाती है।
  • do-while loop तब तक चलता है जब तक n > 0 रहता है।
  • अंत में original और reversed number को print किया जाता है।

उदाहरण: 12344321

अगर किसी number को reverse करने के बाद भी वही number मिले, तो उसे Palindrome Number कहते हैं।

उदाहरण:

121
131
111
#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int reverse = 0;

    printf("Enter the number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    } while (n > 0);

    if (num == reverse) {
        printf("%d is a palindrome number.", num);
    } else {
        printf("%d is not a palindrome number.", num);
    }

    return 0;
}

अगर input:

121

तो output:

121 is a palindrome number.

तीन-digit के Armstrong Number में उसके सभी digits के cubes का sum उसी original number के बराबर होता है।

उदाहरण:

153 = 1³ + 5³ + 3³
    = 1 + 125 + 27
    = 153

इसलिए 153 एक Armstrong Number है।

कुछ Armstrong numbers जैसे:

1, 153, 370, 371, 407
#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int sum = 0;

    printf("Enter a three-digit number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        sum += digit * digit * digit;
        n /= 10;
    } while (n > 0);

    if (num == sum) {
        printf("%d is an Armstrong number.", num);
    } else {
        printf("%d is not an Armstrong number.", num);
    }

    return 0;
}

अगर input:

153

तो output:

153 is an Armstrong number.
#include <stdio.h>

int main(void) {

    for (int num = 1; num <= 1000; num++) {

        int n = num;
        int sum = 0;

        do {
            int digit = n % 10;
            sum += digit * digit * digit;
            n /= 10;
        } while (n > 0);

        if (num == sum) {
            printf("%d ", num);
        }
    }

    return 0;
}
Output
1 153 370 371 407

Nested loops का उपयोग अलग-अलग patterns बनाने के लिए बहुत किया जाता है।

Increasing Star Pattern

Output
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>

int main(void) {

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

        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}
आसान Logic
  • Outer loop → rows की संख्या बताता है।
  • Inner loop → हर row में stars की संख्या बताता है।

जैसे:

Row 1 → 1 star
Row 2 → 2 stars
Row 3 → 3 stars
Output
#include <stdio.h>

int main(void) {

    for (int i = 5; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}
Output
#include <stdio.h>

int main(void) {
    int n;

    printf("Enter the number of rows: ");
    scanf("%d", &n);

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

        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}
Logic

हर row में दो चीजें होती हैं:

  1. Stars से पहले spaces।
  2. उसके बाद stars।

Jump Statements का उपयोग program के एक हिस्से से दूसरे हिस्से में control transfer करने के लिए किया जाता है।

C में चार मुख्य Jump Statements हैं:

  1. break
  2. continue
  3. goto
  4. return

break statement का उपयोग loop को तुरंत रोकने के लिए किया जाता है।

यह nearest loop या switch को terminate कर देता है।

Syntax
break;
Example
#include <stdio.h>

int main(void) {

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

        if (i == 3) {
            break;
        }

        printf("%d ", i);
    }

    return 0;
}
Output
1 2

जब i की value 3 हुई, break ने loop को रोक दिया।

याद रखने का आसान तरीका

break = Loop को रोक दो

continue statement current iteration के बाकी statements को छोड़ देता है और अगली iteration पर चला जाता है।

Syntax
continue;
Example
#include <stdio.h>

int main(void) {

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

        if (i == 3) {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}
Output
1 2 4 5

जब i = 3 हुआ, continue ने उस iteration को skip कर दिया।

याद रखने का आसान तरीका

continue = Current iteration को छोड़ दो

breakcontinue
पूरे loop को रोक देता हैकेवल current iteration को skip करता है
Control loop के बाहर चला जाता हैControl अगली iteration पर जाता है
आगे की iterations नहीं चलतींआगे की iterations चलती रहती हैं
Loop और switch में उपयोग हो सकता हैLoops में उपयोग होता है

यह program maximum 20 numbers लेता है।

  • 0 आने पर program loop को रोक देता है।
  • Positive number की counting करता है।
  • Positive number का square root दिखाता है।
  • Negative number की counting करता है।
  • Negative number के लिए continue का उपयोग करता है।

यह एक practical program है जिसमें अधिकतम 20 numbers input लिए जाते हैं। यह program positive और negative numbers की गिनती करता है। यदि user 0 enter करता है, तो program रुक जाता है। साथ ही, positive numbers का square root भी दिखाया जाता है।

#include <stdio.h>
#include <math.h>

int main(void) {
    int n;
    int positive = 0;
    int negative = 0;

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

        printf("Enter a number (0 to exit): ");
        scanf("%d", &n);

        if (n == 0) {
            break;
        }

        if (n > 0) {
            positive++;

            printf("Square root of %d = %.2f\n",
                   n, sqrt((double)n));
        } else {
            negative++;
            continue;
        }
    }

    printf("\nTotal positive numbers = %d\n", positive);
    printf("Total negative numbers = %d\n", negative);

    return 0;
}
यहाँ क्या होगा?
  • 0break loop को रोक देगा।
  • Positive number → positive count बढ़ेगा और square root दिखाई देगा।
  • Negative number → negative count बढ़ेगा और continue अगली iteration पर चला जाएगा।

goto statement का उपयोग program के control को एक labeled statement पर भेजने के लिए किया जाता है।

Syntax
goto label;

label:
    // statements

Label के बाद : लगाया जाता है।

OR

goto Statement का Syntax

goto statement का उपयोग program में control को किसी label वाली जगह पर भेजने के लिए किया जाता है।

1. आगे जाना (Forward Jump)
label:
    --------------
    --------------
    goto label;
2. पीछे जाना (Backward Jump)
goto label;

    ---------------
    ---------------

label:
    ------------
    ------------

आसान भाषा में:

  • Forward Jump → Program आगे की ओर jump करता है।
  • Backward Jump → Program पीछे की ओर jump करता है।

#include <stdio.h>

int main(void) {
    int n;
    int i = 1;
    int sum = 0;

    printf("Enter the value of N: ");
    scanf("%d", &n);

sum_loop:

    sum += i * i;
    i++;

    if (i <= n) {
        goto sum_loop;
    }

    printf("Result = %d", sum);

    return 0;
}

अगर:

N = 3

तो:

1² + 2² + 3²
= 1 + 4 + 9
= 14
क्या goto का ज्यादा इस्तेमाल करना चाहिए?

आमतौर पर नहीं

goto C में valid statement है, लेकिन सामान्य loops और structured programming code को ज्यादा आसान और readable बनाते हैं।

return statement current function को समाप्त करता है।

यह calling function को value भी वापस भेज सकता है।

Syntax
return expression;
#include <stdio.h>

int square(int n) {
    return n * n;
}

int main(void) {
    int result = square(5);

    printf("Square = %d", result);

    return 0;
}
Output
Square = 25

यहाँ square() function 25 value वापस करता है।

Featureforwhiledo-while
TypeEntry-controlledEntry-controlledExit-controlled
Conditionपहले check होती हैपहले check होती हैबाद में check होती है
Minimum execution001
उपयोगजब iterations का अंदाजा होजब condition के आधार पर loop चलाना होजब body कम से कम एक बार चलानी हो
Initializationअक्सर loop मेंआमतौर पर पहलेआमतौर पर पहले
Updateआमतौर पर loop मेंbody मेंbody में

सबसे महत्वपूर्ण बात

for और while में condition पहले check होती है।

do-while में condition बाद में check होती है।

दोनों Entry-Controlled Loops हैं।

for Loop

जब हमें पता हो कि loop लगभग कितनी बार चलना है, तब for loop उपयोगी होता है।

for (int i = 1; i <= 10; i++) {
    printf("%d ", i);
}

while Loop

जब loop किसी condition के आधार पर चलता हो, तब while ज्यादा convenient हो सकता है।

while (number != 0) {
    // statements
}

while

Condition पहले check होती है:

while (condition) {
    // body
}

इसलिए body 0 बार भी execute हो सकती है

do-while

Body पहले execute होती है:

do {
    // body
} while (condition);

इसलिए body कम से कम 1 बार execute होती है

Beginners अक्सर loop variable को update करना भूल जाते हैं।

गलत Program (Incorrect)

int i = 1;

while (i <= 5) {
    printf("%d ", i);
}

यहाँ i की value कभी change नहीं हो रही।

इसलिए:

i <= 5

हमेशा true रहेगा और loop normal तरीके से बंद नहीं होगा।

सही Program (Correct)

int i = 1;

while (i <= 5) {
    printf("%d ", i);
    i++;
}

इसमें i++ की वजह से i की value बढ़ती रहती है और आखिर में condition false हो जाती है।

याद रखें (Remember)

हर loop में यह देखना जरूरी है कि loop किस तरह खत्म होगा

इस program को देखें:

for (int i = 1; i >= 5; i++) {
    printf("%d", i);
}

शुरुआत में condition है:

1 >= 5

यह false है।

इसलिए loop एक बार भी execute नहीं होगा।

सही Condition

for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}

do-while के अंत में condition के बाद semicolon ; लगाना जरूरी है

सही (Correct)

do {
    printf("Hello");
} while (condition);

गलत (Not Correct:)

do {
    printf("Hello");
} while (condition)

जब एक loop के अंदर दूसरा loop होता है, तो inner loop सामान्यतः outer loop की हर iteration में पूरा चलता है

उदाहरण:

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

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

}

Outer loop 3 बार चलता है।

हर बार inner loop भी 3 बार चलता है।

इसलिए inner loop कुल:

3 × 3 = 9

बार execute होगा।

यह concept आगे Arrays और Matrices में बहुत उपयोगी होगा।

Loops को control करने के लिए मुख्य रूप से:

break
continue

का उपयोग किया जाता है।

break

Loop को पूरी तरह रोक देता है।

continue

Current iteration को skip करता है।

goto

Control को किसी label पर भेजता है।

return

Current function को समाप्त करता है।

1. सही Loop चुनें

  • for → जब iterations की संख्या पता हो।
  • while → जब condition के आधार पर repetition हो।
  • do-while → जब body को कम से कम एक बार चलाना हो।

2. Meaningful Variable Names रखें

इसकी जगह:

int x;

अगर variable counting के लिए है, तो:

int count;

ज्यादा अच्छा है।

3. Loop Variable को Update करें

खासकर while loop में यह बहुत जरूरी है।

4. Condition को आसान रखें

बहुत complicated conditions लिखने से बचें।

5. Braces का इस्तेमाल करें

एक statement होने पर भी braces लगाने से code ज्यादा साफ दिखाई देता है।

while (i <= 10) {
    printf("%d", i);
}

6. अनावश्यक goto से बचें

Normal loops code को समझना आसान बनाते हैं।

7. अलग-अलग Values से Test करें

Program को इन values से भी check करें:

0
1
Negative values
Maximum expected values

इस chapter को पूरा करने के बाद इन programs की practice जरूर करें।

Basic Loop Programs

  1. First N natural numbers print करना।
  2. 1 से 100 तक numbers print करना।
  3. Even numbers print करना।
  4. Odd numbers print करना।
  5. Multiplication table print करना।
  6. Natural numbers का sum निकालना।
  7. Factorial निकालना।

Number Programs

  1. Range में prime numbers print करना।
  2. Fibonacci series बनाना।
  3. Prime number check करना।
  4. Number reverse करना।
  5. Palindrome number check करना।
  6. Armstrong number check करना।
  7. Range में Armstrong numbers print करना।

Series Programs

  1. x + x² + x³ + ... + xⁿ का sum निकालना।
  2. Factorial वाली mathematical series का sum निकालना।
  3. (1) + (1+2) + (1+2+3) + ... जैसी series का sum निकालना।

Nested Loop Programs

  1. Multiplication table।
  2. Increasing star pattern।
  3. Decreasing star pattern।
  4. Pyramid pattern।
  5. Matrix-style programs।

Jump Statement Programs

  1. break की practice।
  2. continue की practice।
  3. goto को समझना।
  4. Functions के साथ return का उपयोग करना।

C में Loops

1. while
2. for
3. do-while

Entry-Controlled Loops

while
for

Exit-Controlled Loop

do-while

Jump Statements

break
continue
goto
return

Important Loop Concepts

Initialization
Condition
Loop Body
Updation
Iteration
Nested Loop
Infinite Loop

C में Loop क्या है?

Loop एक ऐसा control structure है जिसकी मदद से किसी statement या statements के group को बार-बार execute किया जाता है, जब तक दी गई condition सही रहती है।

C में कितने प्रकार के Loops होते हैं?

C में तीन मुख्य loops होते हैं:

  • for
  • while
  • do-while

कौन से Loops Entry-Controlled हैं?

for और while Entry-Controlled Loops हैं।

कौन सा Loop Exit-Controlled है?

do-while Exit-Controlled Loop है।

कौन सा Loop कम से कम एक बार जरूर चलता है?

do-while loop का body कम से कम एक बार जरूर execute होता है, क्योंकि condition बाद में check होती है।

for और while में क्या अंतर है?

दोनों Entry-Controlled Loops हैं।

for loop तब सुविधाजनक होता है जब initialization, condition और update को एक साथ लिखना हो।

while loop तब उपयोगी होता है जब loop मुख्य रूप से किसी condition के आधार पर चलता हो।

Nested Loop क्या है?

जब एक loop के अंदर दूसरा loop लिखा जाता है, तो उसे Nested Loop कहते हैं।

Infinite Loop क्या है?

ऐसा loop जो समाप्त नहीं होता, उसे Infinite Loop कहते हैं।

उदाहरण:

while (1) {
    printf("Running...");
}

इस तरह के loop का उपयोग केवल जानबूझकर करना चाहिए।

C में break क्या करता है?

break nearest loop या switch को तुरंत समाप्त कर देता है।

C में continue क्या करता है?

continue current iteration के बाकी हिस्से को छोड़कर अगली iteration पर चला जाता है।

goto का क्या उपयोग है?

goto program का control किसी labeled statement पर भेजता है। इसका इस्तेमाल सामान्यतः कम करना बेहतर होता है।

return का क्या उपयोग है?

return current function को समाप्त करता है और जरूरत पड़ने पर calling function को कोई value वापस कर सकता है।



Leave a Comment

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *