C में Arrays पर Loop चलाना
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
सारे Elements Print करना
इसका content print करने के लिए एक array के जरिए loop करना हर element को order में visit करता है, loop index को array element access करने और, अक्सर, output को label या number देने दोनों के लिए इस्तेमाल करते हुए।
उदाहरण: Printing All Elements
// 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
i <= sizeसे loop करना, जो अंत से एक element आगे पढ़ता है।- elements की संख्या पाने के लिए एक function के अंदर
sizeof(arr)इस्तेमाल करना। - loop variables मिलाकर गलत array या index के elements modify करना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: