C for , while , do while loop | C looping statement hindi

C Looping statement 

हमारे दैनिक जीवन में ऐसे कई कार्य जो बार-बार repeat करने पड़ते है जैसे पैदल चलना बार-बार यदि हम कदम नही बढ़ाएंगे तो चल नही पाएंगे ,दूसरा उदाहरण जैसे पंखे का चलना पंखा बार-बार घुमने पर ही हवा देता है ठीक उसी तरह से हमे progamming language में भी कुछ task बार-बार करने होते है जिसके लिए loop की आवश्यकता होता है, तो आइये हम looping statementको विस्तार से पढ़ते है-

  1. C language में जब हमे किसी एक task/work (कार्य)  बार-बार repeat करना होता है तो looping statement का इस्तेमाल किया जाता है, 
  2. Looping statement में condition लिखा जाता है जिसके base यह निर्धारण होता है की कोई भी task/statement को कितना बार repeat करना है या कितनी बार loop चलेगे
  3. Loop में लिखा गया condition जब तक true होता है तब तक loop चलता है और condition false होने पर loop terminate हो जाता है
  4. Programming langauge में loop नही होता तो हमें task/work को  repeat करने के लिए एक ही statement को बार-बार लिखना पड़ता जिससे code duplication होता तथा readability भी ठीक नही होता है

Type of loop in c langauge:

Three type of loop in c langunage 

  1. for loop
  2. while loop
  3. do while loop

Type of Loop base on control

  • Entry controlled loop
    • for loop
    • while loop
  • Exit controlled loop
    • do while loop
"What is the C loop?,What is a loop in C?, what is a looping statement in c, What is type loop in C?, C loop in c with example, Loop c language in hindi example, How many types of loop in c, Types of loop in c with examples, for loop in c, for loop syntax in c,for loop in c, while loop syntax in c, semant sir, do  while loop syntax in c, Loop कितने प्रकार के होते हैं? ,सी में 3 प्रकार के लूप क्या हैं?, लूप्स का मतलब क्या होता है?

1. Entry controlled loop : ऐसा loop जिसके entry में condition लिखा जाता है जिससे loop को control किया जा सके वो entry controlled loop के अंतर्गत आते है

2. Exit controlled loop : ऐसा loop जिसके exit में condition लिखा जाता है जिससे loop को control किया जा सके वो exit controlled loop के अंतर्गत आते है

Advantages of Loop

  1. loop के इस्तेमाल से code की संख्या कम हो जाता है तथा code की reusability, readibility होती है 
  2. loop के करण same code बार-बार नही लिखने की जरूरत नही पड़ता जिससे code का duplication ख़त्म होता है
  3. array, arraylist, linkedlist जैसे data structure का data आसानी से traverse/show/access/show किया जाता है 

Disadvantages of loop

  1. Beginners के लिए loop को समझना थोडा कठिन, loop को समझने के लिए बार-बार prectice करना होता है तथा दिमाग खर्च करना पड़ता है

For Loop : 

for loop एक Iterating/looping statement है जिसके block/body में लिखे गये code repeat किये जाते है 

  • for loop का इस्तेमाल तब किया जाता है जब हमें पता हो की loop कितनी बार चलाना है अथवा  कहाँ से कहाँ तक loop चलाना है इस बात की जानकारी हो तो for loop का इस्तेमाल किया जाता है
  • C language के अंदर For loop एक looping statement है तथा for loop को entry controlled loop भी कहा जाता है
  • for loop में लिखा गया condition जब तक true होता है तब तक loop चलता है यदि condition false हो जाए तो loop terminate हो जाता है
  • for loop के  Curly Braces {} में entry से पहले ही condition check किया जाता है अथवा दूसरी शब्दों में कहे तो statement के execute/run होने से पहले ही condition की जांच की जाती है जिसके करण for loop entry controlled loop के category में आता है
  • for loop का use किसी भी statement को बार-बार execute/run करने केलिए किया जाता है
  • loop के अंदर यदि single line का statement लिखा है तो Curly Braces {} लगाने का जरूरत नही पड़ता
  • for loop को for loop के body के बगैर भी run करना possible है

Syntax : 
for (initialize expression 1; condition expression 2; increment/decriment expression 3)
{
   // write n number of statement here
}

loop के अंदर यदि single line का statement लिखा है तो Curly Braces {} लगाने का जरूरत नही पड़ता 

for (initialize expression 1; condition expression 2; increment/decriment expression 3)
   // write one number of statement here

for loop में  तीन तरह के expression होते है जो इस प्रकार है :- 

1. Initialization expression 1 : for loop का first step है जिसके द्वारा variable को value assign किया जाता है, Initialization का काम सबसे पहले और सिर्फ एक बार होता है loop चाहे जितने बार चले Initialization expression 1 सिर्फ एक बार चलता है और सबसे पहले चलता है

2. Condition expression 2 : इसमें condition की जांच की जाती है यदि condition true होती है तो  loop के अंदर के statement execute/run होता हैं, अगर condition false होता है तो for loop terminate हो जाता है, जितना बार loop चलता है उससे एक बार ज्यादा condition check किया जाता है क्यों की last का condition जब check किया जाता है तो condition false होता तो statement execute किये बगैर loop terminate हो जाता है

3. Increment/Decrement expression 3 : thrid expression loop के statement execute होने के पश्चात variable की value को update किया जाता जिसमे value increment(बढ़ाना) होगा या decrement(घटाना) का कार्य किया जाता है

10 तक गिनता print करने का for loop program

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

Output:
1 2 3 4 5 6 7 8 9 10

  1. Step : loop के पहले step में expression 1 execute है जिससे variable में value assign किया जाता है (और ये पुरे loop में सिर्फ एक बार चलता है)
  2. Step : loop के 2nd step में expression 2 execute है जिसे condition check किया जाता है
  3. Step : loop के 3rd step में loop के Curly Braces {} के अंदर लिखा हुआ statement execute होता है
  4. Step : loop के 4th step में expression 3 execute होता है जिसमे variable की value increment/decriment होता है
  5. Step : 5th step में फिर से 2nd step में control जाता है और condition check होता है और true होने पर statement execute होता है और false होने पर terminate हो जाता है

important point
1. Initialization expression 1 और Increment/Decrement expression 3 optional होता है हम इसे खली भी छोड़ सकते है परन्तु  Semicolon(;) लिखना जरुरी होता है
2. Condition expression 2 को भी लिखना चाहिए, यदि नही लिखते तो loop infinite time तक चलेगा परन्तु loop के अंदर किसी if condition पर break keyword के help से loop को रोका जा सकता है 


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

Output:
1 2 3 4 5 6 7 8 9 10

syntax for infinite time loop
for ( ; ; ){
}
2. Example
#include<stdio.h>
int main(){
    int a=1;
    for(; ;){
        if(a>10)
            break;
        printf("%d ",a);
        a++;
    }
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

for loop को for loop के body के बगैर भी run करना possible है जैसे:-
for loop example for counting string length
#include<stdio.h>
int main(){
    int a;
    char string[20];
    printf("Enter Any String ");
    scanf("%s",&string);
    for(a=0; string[a]!='\0';a++);
    printf("%s length is = %d",string,a);
    return 0;
}

Output:
Enter Any String
semantsir.in
semantsir.in length is = 12

C While Loop

while loop एक looping statement है तथा looping statement का use किसी भी statement को बार-बार execute/run करने केलिए किया जाता है

  • while loop का इस्तेमाल तब किया जाता है जब हमें पता ही ना हो की loop कितनी बार चलाना है अथवा दुसरे शब्दों में कहे तो infinite time तक loop चलाने के लिए maximim while loop का प्रयोग किया जाता है परन्तु इसका मतलब ये बिलकुल नही है की while loop में condition check नही होता और while loop को control नही किया जा सकता
  • C language के अंदर while loop एक looping statement है तथा while loop को entry controlled loop भी कहा जाता है क्यों की loop की body में entry से पहले condition check किया जाता है
  • While loop पहले condition check करता है condition true होने के बाद loop के के body में enter होता है जिसके करण while loop को pre tested loop भी कहा जाता है
  • while loop में लिखा गया condition जब तक true होता है तब तक loop चलता है यदि condition false हो जाए तो loop terminate हो जाता है
  • loop के अंदर यदि single line का statement लिखा है तो Curly Braces {} का इस्तेमाल optional हो जाता है
  • while loop को while loop के body के बगैर भी run करना possible है
  • while loop का Parentheses() empty नही हो सकता, condition expression हमें लिखना ही होगा

while loop Syntax : 
while(condition expression)
{
   // write n number of statement here
}

while loop के अंदर यदि single line का statement लिखा है तो Curly Braces {} लगाने का जरूरत नही पड़ता 

while loop Syntax for single line statement:
while (condition expression)
   // write one number of statement here

While loop Condition expression :

Condition expression : इसमें condition की जांच की जाती है यदि condition true होती है तो  loop के अंदर के statement execute/run होता हैं, अगर condition false होता है तो while loop terminate हो जाता है, जितना बार loop चलता है उससे एक बार जादा condition check किया जाता क्यों की last का condition जब check किया जाता है तो condition false होता तो statement execute किये बगैर loop terminate हो जाता है

10 तक गिनता print करने का while loop program

Example: 
#include<stdio.h>
int main(){
    //Declare variable and assign value
    int a=1;
    //check condition
    while(a<=10){
        //print value
        printf("%d ",a);
        //incriment value
        a++;
    }
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

  1. Step : सबसे पहले a variable को Declare किया तथा उसमें value assign किया गया है
  2. Step : loop के 2nd condition check किया गया है जिसमे condition true होता है तो loop के body में entry होता है अन्यथा loop terminate होता है
  3. Step : loop के 3rd step में condition true होने पर loop के body में Curly Braces {} के अंदर लिखा हुआ statement execute होता है
  4. Step : loop के 4th step loop का body execute करने के पश्चात फिर से step 2 यानि की condition check किया जाता है और यही प्रक्रिया बार-बार repeat होता है

important point
1. Condition expression को लिखना optional नही है ,Condition expression हर हाल में लिखा जाना चाहिए हम इसे empty नही छोड़ सकते

while loop program for table
1. Example:
#include<stdio.h>
int main(){
    int a=1,num;
    printf("Enter Number ");
    scanf("%d",&num);
    while(a<=10){
        printf("%d * %d = %d ",a,num,a*num);
        a++;
    }
    return 0;
}

Output:
Enter Number 5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

syntax for infinite time loop
while ( 1 ){
}
2. Example
#include<stdio.h>
int main(){
    int a=1;
    while(1){
        if(a>10)
            break;
        printf("%d ",a);
        a++;
    }
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

While loop को while loop के body के बगैर भी run करना possible है जैसे :- 
for loop example for counting string length
#include<stdio.h>
int main(){
    int a=0;
    char string[20];
    printf("Enter Any String ");
    scanf("%s",&string);
    while(string[a]!='\0' && ++a);
    printf("%s length is = %d",string,a);
    return 0;
}

Output:
Enter Any String
semantsir.in
semantsir.in length is = 12

C do while Loop:

C langauge के अंदर do while एक looping statement है looping statement के block में लिखे गए code को repeat किया जाता है जब तक की condition false नही हो जाता है परन्तु do while loop के case में यदि condition false भी हो जाता है तो भी एक बार code execute हो ही जाता है और फिर loop terminat होता है do while को Post Tested Loop भी कहते

  • do while loop का इस्तेमाल तब किया जाता है जब condition false होने  पर भी एक बार statement execute करना हो 
  • C language के अंदर do while एक looping statement है तथा do while loop को exit controlled loop भी कहा जाता है क्यों की do while में पहले statement/code execute किया जाता फिर condition check किया जाता और condition true होने पर statement/code दुबारा execute किया जाता है 
  • do While loop पहले body के अंदर का code execute करता है उसके बाद condition check करता है इसलिए Post Tested Loop कहलाता है
  • do while loop में लिखा गया condition जब true होता है तब loop दुबारा चलाया जाता है और condition false हो जाए तो loop terminate हो जाता है
  • do while के अंदर यदि single line का statement लिखा है तो Curly Braces {} का इस्तेमाल optional हो जाता है
  • do while loop को body के बगैर run करना possible नही है
  • do while loop का Parentheses () empty अथवा empty character constant ('') नही हो सकता

do while loop Syntax : 
do{
   // write n number of statement here
}while(condition expression);

do while loop के अंदर यदि single line का statement लिखा है तो Curly Braces {} लगाने का जरूरत नही पड़ता 

do while loop Syntax for single line statement:
do
   // write one number of statement here
while(condition expression);

Do while loop Condition expression :

Condition expression : इसमें condition की जांच की जाती है यदि condition true होती है तो  loop दुबारा चलता है, अगर condition false होता है तो do while loop terminate हो जाता है

10 तक गिनता print करने का do while loop program
Example: 
#include<stdio.h>
int main(){
    //Declare variable and assign value
    int a=1;
    do{
        //print value
        printf("%d ",a);
        //incriment value
        a++;
    //check condition    
    }while(a<=10);
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

1. step : सबसे पहले a variable को Declare किया तथा उसमें value assign किया गया है
2. step : loop के 2nd step में do while को body execute किया जायगा
3. step : loop के 3rd step में condition check किया जायगा condition true होने पर loop के body में Curly Braces {} के अंदर लिखा हुआ statement execute होता है
3.1 step : यदि while में लिखा condition false होता है तो loop exit हो जाता है

important point:
Condition expression को लिखना optional नही है ,Condition expression हर हाल में लिखा जाना चाहिए हम इसे empty  नही छोड़ सकते या empty character constant नही लिख सकते

do while loop program for table
1. Example:
#include<stdio.h>
int main(){
    int a=1,num;
    printf("Enter Number ");
    scanf("%d",&num);
    do {
        printf("%d * %d = %d ",a,num,a*num);
        a++;
    }while(a<=10);
    return 0;
}

Output:
Enter Number 6
1 * 6 = 6
2 * 6 = 12
3 * 6 = 18
4 * 6 = 24
5 * 6 = 30
6 * 6 = 36
7 * 6 = 42
8 * 6 = 48
9 * 6 = 54
10 * 6 = 60

Syntax for infinite time loop
do {
// statement
}while ( 1 );
Example:
#include<stdio.h>
int main(){
    int a=1;
    do{
        if(a>10)
            break;
        printf("%d ",a);
        a++;
    }while(1);
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

do while के अंदर यदि single line का statement लिखा है तो Curly Braces { } का इस्तेमाल optional हो जाता है जैसे : 
Example:
#include<stdio.h>
int main(){
    int a=0;
    do
        printf("%d ",++a);
    while(a<10);
    return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10