Range-Based for Loop in C++ (for-each Loop) – Explained with Examples
The Range-Based for Loop, also known as the for-each loop, was introduced in C++11. It provides a simple way to access and process each element of an array or collection without manually managing the loop counter or array index.
Range-Based for Loop, जिसे for-each loop भी कहा जाता है, C++11 में introduce किया गया था। इसका उपयोग array या collection के प्रत्येक element को आसानी से access और process करने के लिए किया जाता है। इसमें loop counter या array index को manually manage करने की आवश्यकता नहीं होती।
1. Syntax of Range-Based for Loop (सिंटैक्स)
for (declaration : range){// Statements}
Explanation (व्याख्या):
declaration: Declares a variable that receives each element from the range.
range: The array, container, or other supported range whose elements are processed.
Statements: The statements executed for each element.
declaration: एक variable declare करता है जिसमें range का प्रत्येक element प्राप्त होता है।
range: वह array, container या अन्य supported range है जिसके elements process किए जाते हैं।
Statements: वे statements जो प्रत्येक element के लिए execute होते हैं।
2. Example: Display Array Elements
#include <iostream>using namespace std;int main(){int numbers[] = {10, 20, 30, 40, 50};for (int num : numbers){cout << num << " ";}return 0;}
Output:
10 20 30 40 50Explanation (व्याख्या):
In this example, the range-based for loop accesses each element of the numbers array one by one. The variable num receives the current element during each iteration.
इस उदाहरण में range-based for loop, numbers array के प्रत्येक element को एक-एक करके access करता है। प्रत्येक iteration में num variable को current element प्राप्त होता है।
3. Example: Calculate Sum of Array Elements
#include <iostream>using namespace std;int main(){int numbers[] = {10, 20, 30, 40, 50};int sum = 0;for (int num : numbers){sum = sum + num;}cout << "Sum = " << sum;return 0;}
Output:
Sum = 150Explanation (व्याख्या):
The loop accesses each array element and adds it to the sum variable. After all elements have been processed, the program displays the total.
इस loop में array के प्रत्येक element को access करके sum variable में add किया जाता है। सभी elements process होने के बाद program total sum display करता है।
4. Using auto with Range-Based for Loop
C++ allows the use of the auto keyword to automatically determine the data type of each element.
C++ में प्रत्येक element का data type automatically determine करने के लिए auto keyword का उपयोग किया जा सकता है।
#include <iostream>using namespace std;int main(){double prices[] = {10.5, 20.5, 30.5};for (auto price : prices){cout << price << " ";}return 0;}
Output:
10.5 20.5 30.5Here, the compiler automatically determines that price is of type double.
यहाँ compiler automatically determine करता है कि price का data type double है।
5. Modifying Array Elements Using Reference
By default, the loop variable receives a copy of each element. If we want to modify the original array elements, we can use a reference (&).
Default रूप से loop variable को प्रत्येक element की एक copy प्राप्त होती है। यदि हम original array के elements को modify करना चाहते हैं, तो reference (&) का उपयोग कर सकते हैं।
#include <iostream>using namespace std;int main(){int numbers[] = {1, 2, 3, 4, 5};for (int &num : numbers){num = num * 2;}for (int num : numbers){cout << num << " ";}return 0;}
Output:
2 4 6 8 10Explanation (व्याख्या):
The reference variable num refers directly to each original array element. Therefore, multiplying num by 2 modifies the actual array elements.
Reference variable num सीधे original array के प्रत्येक element को refer करता है। इसलिए num को 2 से multiply करने पर original array के elements modify हो जाते हैं।
6. Difference Between Traditional for Loop and Range-Based for Loop
| Traditional for Loop | Range-Based for Loop |
|---|---|
| Uses initialization, condition, and update expressions. | Uses a range of elements directly. |
| Array index or loop counter is commonly used. | No manual array index is required. |
| Provides direct control over the loop counter. | Automatically processes each element in the range. |
| Useful when index-based access or custom iteration is needed. | Useful when processing every element sequentially. |
Traditional for Loop | Range-Based for Loop |
|---|---|
| इसमें initialization, condition और update expressions का उपयोग होता है। | इसमें elements की range directly दी जाती है। |
| सामान्यतः array index या loop counter का उपयोग होता है। | इसमें manually array index की आवश्यकता नहीं होती। |
| Loop counter पर direct control मिलता है। | Range के प्रत्येक element को automatically process करता है। |
| जब index-based access या custom iteration की आवश्यकता हो, तब उपयोगी है। | जब सभी elements को क्रम से process करना हो, तब उपयोगी है। |
The Range-Based for Loop makes C++ programming simpler and more readable when working with arrays and containers. It is especially useful when we need to process every element without managing indexes manually. Use references when you need to modify the original elements.
Range-Based for Loop arrays और containers के साथ काम करते समय C++ programming को सरल और readable बनाता है। जब हमें बिना manually indexes manage किए प्रत्येक element को process करना हो, तब यह विशेष रूप से उपयोगी है। Original elements को modify करने के लिए reference का उपयोग करें।
Comments
Post a Comment