https://www.rakeshmgs.in/search/label/Template
https://www.rakeshmgs.in
RakeshMgs

Programming C me Loops kya hota hai aur Kaise istemal kiya jata hai

Updated:

 How to use Loops in C

यदि मै आपसे कहुँ की आपको C language in Hindi 10 बार print करवाना है तो ऐसा आप किस तरह करेंगे? सामान्यतया: ऐसा करने के लिए आप 10 printf() statements लिखेंगे। ये एक बेकार approach है। ऐसा करने से programmer का time और computer की memory waste होती है।

C language में आपको loops provide किये गए है। Loops की मदद से आप एक ही statement को बार बार execute करवा सकते है। हर तरह का loop एक block provide करता है जिसमे वो statements लिखे जाते है जिन्हें आप एक से ज्यादा बार execute करवाना चाहते है।

Loop Statement



Loop 3 चीज़ों से मिलकर बना होता है।

  • Initial variable – ये वो variable होता है जँहा से आप loop को start करते है। ये एक integer variable होता है। इस variable को तब तक increment किया जाता है जब तक की दी गयी condition false ना हो जाये। इस variable को loop की condition में include किया जाता है।
  • Condition – ये वो condition होती है जो loop को control करती है। जब तक ये condition true रहती है loop execute होता रहता है। जैसे ही ये condition false होती है loop terminate हो जाता है।
  • Increment – आप कितने number से या कैसे initial variable/condition को increment करना चाहते है ये increment part में define किया जाता है।

Types of Loop

C language आपको 3 तरह के loops प्रोवाइड करती है।
1.while loop
2.for loop
3.do while loop

while loop

While एक simple loop होता है ये जब तक condition true रहती है तब तक execute होता है। Condition के false होने पर ये loop terminate हो जाता है। While loop का general syntax नीचे दिया जा रहा है।

Syntax :

	
	
variable initialization; while(condition) { statements; variable increment or decrement; }

Condition यदि पहली बार में ही false हो तो compiler loop में enter ही नहीं होता है। Loop को totally skip कर दिया जाता है। आइये while loop को एक उदाहरण से समझने का प्रयास करते है।

Example: Program to print first 10 natural numbers

    
#include <stdio.h> int main() { int x; x = 1; while(x <= 10) { printf("%d\t,"x); /* below statement means, do x = x+1, increment x by 1*/ x++; } }

Output

1 2 3 4 5 6 7 8 9 10

Loop के अंदर हर iteration में num variable की value print की जा रही है। इसके बाद num variable को increment किया जा रहा है। ये program निचे दिया गया output generate करता है।

यदि initial variable को increment ना किया जाये तो condition कभी false नहीं होगी। ऐसे में loop infinite time तक चलता जायेगा।

for loop

C loops में for loop सबसे ज्यादा use किया जाता है। ये loop बहुत ही easy होता है और एक single statement में define हो जाता है। इस loop का general structure नीचे दिया जा रहा है।

	
	
for(initialization; condition; increment/decrement) { statement-block; }

For loop के बारे में खास बात ये है की एक statement में तीनों elements define किये जाते है। इस loop को नीचे उदाहरण द्वारा समझाया गया है।

Example: Program to print first 10 natural numbers

    
#include <stdio.h> int main() { int x; for(x = 1; x <= 10; x++) { printf("%d\t,"x); } }

Output

1 2 3 4 5 6 7 8 9 10

Nested for loop

loop के लिए nested for loop भी हो सकते हैं, यानी एक loop के लिए दूसरे के अंदर loop, basic syntax है

	
	
for(initialization; condition; increment/decrement) { for(initialization; condition; increment/decrement) { statement ; } }

Example: Program to print half Pyramid of numbers

    
#include <stdio.h> int main() { int x,j; for(x = 1; x <= 10; x++) { printf("\n"); /* second for loop inside the first */ for(j = i; j > 0; j--) { printf ("%d", j); } } }

output

1
21
321
4321
54321

do while loop

situations को do-while loop की मदद से नियंत्रित किया जा सकता है। do statement पहले लूप के body का check करता है और अंत में, while का उपयोग करके चेक किया जाता है। इसका अर्थ है कि loop के body को कम से कम एक बार evaluates किया जाएगा, भले ही अंदर की initialized condition झूठी होने के लिए inialized हो। सामान्य syntax है

	
	
do { ..... ..... } while(condition)

Example: Program to print first 10 multiples of 5.

    
#include <stdio.h> int main() { int x; a = 5; i = 1; do { printf("%d\t,"a*i); i++; } while(i <= 10); }

output

5 10 15 20 25 30 35 40 45 50


आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com