← Back to C Course | Chapter 6: Arrays & Strings | Lesson 3 of 8

C में Arrays पर Loop चलाना

0 से array की length माइनस एक तक एक index वाला एक for loop एक array के हर element visit करने का standard तरीका है, चाहे print करना हो, sum करना हो, search करना हो, या values modify करनी हों।
Syntax
c
for (int i = 0; i < length; i++) {
  array_name[i];
}

for से Array के जरिए Loop करना

C में एक array के जरिए loop करने का standard तरीका एक for loop है जिसका index variable 0 से array की length तक चलता है, उसे शामिल किए बिना, हर iteration पर square-bracket indexing से एक element access करते हुए।

उदाहरण: Looping Through an Array with for

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

सारे Elements Print करना

इसका content print करने के लिए एक array के जरिए loop करना हर element को order में visit करता है, loop index को array element access करने और, अक्सर, output को label या number देने दोनों के लिए इस्तेमाल करते हुए।

उदाहरण: Printing All Elements

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

Array Elements Sum करना

loop से पहले initialized और इसके अंदर updated एक running total variable, एक array में हर element sum करने का standard pattern है, जिसे फिर count से divide करके एक average compute किया जा सकता है।

उदाहरण: Summing Array Elements

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

Array में एक Value ढूंढना

एक specific value के लिए एक array search करना इसके elements में loop करता है हर एक को target से check करते हुए, और एक match मिलते ही unnecessarily जारी रखने की बजाय तुरंत loop रोकने के लिए break आमतौर पर इस्तेमाल होता है।

उदाहरण: Finding a Value in an Array

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[4] = {5, 10, 15, 20};
	// Declare target and set it to 15
	int target = 15;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Check whether arr[i] == target
		if (arr[i] == target) {
			// Print formatted text to the screen
			printf("Found at index %d", i);
			// Leave the switch/loop
			break;
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Loop में Elements Modify करना

एक loop इसकी body के अंदर same index को पढ़ और लिख दोनों कर array के elements को in place modify कर सकता है, यही तरीका है जिससे हर value को double करना या negative values को clamp करना जैसे operations पूरे array पर apply होते हैं।

उदाहरण: Modifying Elements in a Loop

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		arr[i] *= 2;
	}
	// Print formatted text to the screen
	printf("%d %d %d", arr[0], arr[1], arr[2]);
	// 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. i <= size से loop करना, जो अंत से एक element आगे पढ़ता है।
  2. elements की संख्या पाने के लिए एक function के अंदर sizeof(arr) इस्तेमाल करना।
  3. loop variables मिलाकर गलत array या index के elements modify करना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.