C++ का for Loop
In this page:
for (initialization; condition; update) {
// body
}
For Loops का Introduction
एक for loop initialization, एक condition, और एक update step को एक line में pack करता है — for (int i = 0; i < 10; i++) — इसे natural choice बनाते हुए जब भी आपको पहले से exactly पता हो कि आपको कुछ कितनी बार दोहराना है।
एक while loop के मुकाबले, तीनों parts को साथ रखना loop के start, stop, और progress conditions को एक नज़र में देखना आसान बनाता है।
उदाहरण: Introduction to For Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 10; i++) {
// Print to the console with cout
std::cout << 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
Loop Steps
Update step सिर्फ simple +1 increments तक सीमित नहीं है — आप हर pass counter में कोई भी amount add कर सकते हैं, multiply कर सकते हैं, या divide कर सकते हैं, जो loop को linearly की बजाय geometrically step करने में बदल देता है।
यह exactly वह तरीका है जिससे binary search जैसे algorithms, जो बार-बार एक value को आधा या दोगुना करते हैं, अपना loop structure करते हैं।
उदाहरण: Loop Steps
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 20; i += 5) {
// Print to the console with cout
std::cout << 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
Nested For Loops
एक for loop को दूसरे के अंदर रखना inner loop को outer loop की हर single iteration के लिए पूरी तरह चलने देता है, जो exactly एक 2D grid process करने के पीछे का pattern है — outer loop rows में चलता है जबकि inner loop हर row के अंदर columns में चलता है।
Iterations की total संख्या दोनों loops के counts का product है।
उदाहरण: Nested For Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 2; i++) {
// Loop: repeat while the condition holds
for (int j = 1; j <= 2; j++) {
// Print to the console with cout
std::cout << i << "," << j << " ";
}
}
// 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
Arrays पर Loop करना
एक for loop के counter को एक array index की तरह इस्तेमाल करना, जैसे for (int i = 0; i < size; i++) cout << arr[i];, C++ में sequence में हर element visit करने के सबसे common patterns में से एक है।
यह ज़रूरत पड़ने पर आपको index खुद देता है, कुछ alternatives के उलट जो सिर्फ value देते हैं।
उदाहरण: Looping Over Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
// Declare size and set it to 3
int size = 3;
// Loop: repeat while the condition holds
for (int i = 0; i < size; 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, for (int x : arr) की तरह लिखा गया, आपके किसी index manage किए बिना सीधे हर element की value पर iterate करता है, जो off-by-one bugs की पूरी category हटा देता है।
जब भी आपको actually index की ज़रूरत न हो, सिर्फ values की, यह modern C++ में preferred style है।
उदाहरण: Range-based For Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
for (int x : arr) {
// Print to the console with cout
std::cout << x << " ";
}
// 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 length के साथ
<=इस्तेमाल करना, जैसेi <= n, जो अंत से एक element आगे पढ़ता है। for (...);के बाद एक semicolon add करना, जो loop body को empty बना देता है।for (int i = 0, i < 5, i++)में semicolons की बजाय commas या colons इस्तेमाल करना।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: