← Back to C Course | Chapter 4: Control Flow | Lesson 9 of 11

C का for Loop

एक for loop steps को एक set number of times दोहराता है, गिनते हुए दस jumping jacks करने जैसा। start, stop, और counting सब एक line में बैठते हैं।
Syntax
c
for (initialization; condition; update) {
  // body
}

for Loop क्या है?

एक for loop एक variable की initialization, इसकी exit condition, और इसका update step एक single line में combine करता है, अलग statements में बिखरे होने की बजाय loop की पूरी lifecycle को एक नज़र में visible बनाते हुए।

उदाहरण: What is a for Loop?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Counter

एक for loop एक natural choice है जब भी आपको पहले से exactly पता हो कि आप कुछ कितनी बार दोहराना चाहते हैं, क्योंकि iterations की fixed संख्या को सीधे loop की condition में bake किया जा सकता है।

उदाहरण: Standard Counter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 5; i++) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Even Numbers Print करना

एक for loop की initial value और इसके update step को adjust करना (उदाहरण के लिए 1 की बजाय 2 से increment करना) आपको loop body के अंदर एक extra if check की ज़रूरत के बिना एक range में सिर्फ even numbers print करने देता है।

उदाहरण: Printing Even Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i <= 10; i += 2) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Sequences Sum करना

loop के बाहर declared एक accumulator variable, हर pass पर loop counter की value add करके update होता है, एक for loop इस्तेमाल करके numbers की एक sequence sum करने का standard pattern है।

उदाहरण: Summing Sequences

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare sum and set it to 0
	int sum = 0;
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 5; i++) {
		sum += i;
	}
	// Print formatted text to the screen
	printf("%d", sum);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Decremental For Loop

एक for loop से नीचे गिनने के लिए, counter को एक high value पर शुरू करें, एक greater-than-or-equal condition इस्तेमाल करें, और हर pass पर increment की बजाय decrement करें -- same three-part structure, बस reverse में चलते हुए।

उदाहरण: Decremental For Loop

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 5; i >= 1; i--) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक array length के साथ <= इस्तेमाल करना, जैसे i <= n, अंत से एक element आगे पढ़ते हुए।
  2. for (...); के बाद एक semicolon लगाना, जो body को empty बना देता है।
  3. parentheses के अंदर semicolons की बजाय commas इस्तेमाल करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.