← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 2 of 15

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

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

for (type element : array_name) {
  // use element
}

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

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

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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4};
	// Declare length and set it to 4
	int length = 4;
	// Loop: repeat while the condition holds
	for (int i = 0; i < length; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Range-Based for से Loop करना

एक range-based for loop एक explicit index variable की ज़रूरत के बिना सीधे एक array के elements पर iterate करता है, loop को छोटा और कम error-prone बनाते हुए।

उदाहरण: Looping with Range-Based for

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4};
	for (int value : arr) {
		// Print to the console with cout
		std::cout << value << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {10, 20, 30};
	// Declare total and set it to 0
	int total = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		total += arr[i];
	}
	// Print to the console with cout
	std::cout << total << std::endl;
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {5, 8, 12, 3};
	// Declare target and set it to 12
	int target = 12;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Check whether arr[i] == target
		if (arr[i] == target) {
			// Print to the console with cout
			std::cout << "Found at index " << i << std::endl;
			// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		arr[i] *= 2;
	}
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// 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 करना, जो अंत से आगे पढ़ता है।
  2. length ढूंढने के लिए एक pointer या function parameter पर sizeof(arr) इस्तेमाल करना।
  3. & इस्तेमाल किए बिना एक range-based for loop में elements modify करना, सिर्फ एक copy बदलते हुए।

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.