Header Ads

Loops and its Types in C Language

Loops and its Types in C Language

 In C programming, loops are used to execute a block of code repeatedly until a certain condition is met. There are three primary types of loops in C:

1. For Loop

The `for` loop is typically used when the number of iterations is known beforehand. It consists of three parts:

- Initialization

- Condition

- Increment/Decrement

Syntax:

for (initialization; condition; increment/decrement) {

    // Code to be executed

}


Example:

#include <stdio.h>


int main() {

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

        printf("%d\n", i);

    }

    return 0;

}

Output:

1

2

3

4

5


2. While Loop

The `while` loop is used when the number of iterations is not known and the loop should continue as long as a certain condition is true.

Syntax:

while (condition) {

    // Code to be executed

}


Example:

#include <stdio.h>


int main() {

    int i = 1;

    while (i <= 5) {

        printf("%d\n", i);

        i++;

    }

    return 0;

}

Output:

1

2

3

4

5


3. Do-While Loop

The `do-while` loop is similar to the `while` loop, but the condition is checked after the loop has executed. This means the code block is executed at least once.

Syntax:

do {

    // Code to be executed

} while (condition);


Example:

#include <stdio.h>


int main() {

    int i = 1;

    do {

        printf("%d\n", i);

        i++;

    } while (i <= 5);

    return 0;

}

Output:

1

2

3

4

5

Each type of loop is useful in different situations, depending on when you want the loop to stop and how many times you want it to run.

________________________________________________________________________________

C भाषा में लूप्स और उनके प्रकार

C प्रोग्रामिंग में, लूप्स का उपयोग तब किया जाता है जब हमें किसी कोड ब्लॉक को बार-बार तब तक चलाना होता है जब तक कि कोई विशेष शर्त पूरी नहीं हो जाती। C में मुख्य रूप से तीन प्रकार के लूप होते हैं:


1. For Loop

`for` लूप का उपयोग तब किया जाता है जब हमें पहले से ही पता हो कि कोड को कितनी बार चलाना है। इसमें तीन भाग होते हैं:

- Initialization (आरंभिकरण)

- Condition (शर्त)

- Increment/Decrement (वृद्धि/कमी)


सिंटैक्स:

for (initialization; condition; increment/decrement) {

    // चलाने के लिए कोड

}


उदाहरण:

#include <stdio.h>


int main( ) {

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

        printf("%d\n", i);

    }

    return 0;

}

आउटपुट:

1

2

3

4

5


2. While Loop

`while` लूप का उपयोग तब किया जाता है जब यह पता न हो कि कोड को कितनी बार चलाना है और लूप को तब तक चलाना हो जब तक कि कोई शर्त सही हो।


सिंटैक्स:

while (condition) {

    // चलाने के लिए कोड

}


उदाहरण:

#include <stdio.h>


int main() {

    int i = 1;

    while (i <= 5) {

        printf("%d\n", i);

        i++;

    }

    return 0;

}

आउटपुट:

1

2

3

4

5


3. Do-While Loop

`do-while` लूप `while` लूप के समान है, लेकिन इसमें शर्त को लूप के चलने के बाद चेक किया जाता है। इसका मतलब है कि कोड ब्लॉक कम से कम एक बार जरूर चलेगा।


सिंटैक्स:

do {

    // चलाने के लिए कोड

} while (condition);


उदाहरण:

#include <stdio.h>


int main() {

    int i = 1;

    do {

        printf("%d\n", i);

        i++;

    } while (i <= 5);

    return 0;

}

आउटपुट:

1

2

3

4

5

प्रत्येक प्रकार का लूप अलग-अलग परिस्थितियों में उपयोगी होता है, यह इस पर निर्भर करता है कि आप लूप को कब रुकवाना चाहते हैं और उसे कितनी बार चलाना चाहते हैं।

No comments

Powered by Blogger.