C++ में Arrays पर Loop चलाना
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Range-Based for से Loop करना
एक range-based for loop एक explicit index variable की ज़रूरत के बिना सीधे एक array के elements पर iterate करता है, loop को छोटा और कम error-prone बनाते हुए।
उदाहरण: Looping with Range-Based for
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Array Elements Sum करना
loop से पहले initialized और इसके अंदर updated एक running total variable, एक array में हर element sum करने का standard pattern है, जिसे फिर count से divide करके एक average compute किया जा सकता है।
उदाहरण: Summing Array Elements
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Array में एक Value ढूंढना
एक specific value के लिए एक array search करना इसके elements में loop करता है हर एक को target से check करते हुए, और एक match मिलते ही unnecessarily जारी रखने की बजाय तुरंत loop रोकने के लिए break आमतौर पर इस्तेमाल होता है।
उदाहरण: Finding a Value in an Array
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
i <= sizeसे loop करना, जो अंत से आगे पढ़ता है।- length ढूंढने के लिए एक pointer या function parameter पर
sizeof(arr)इस्तेमाल करना। &इस्तेमाल किए बिना एक range-based for loop में elements modify करना, सिर्फ एक copy बदलते हुए।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings