From 2ef5af7ab1945a5abef7fe58690a1123fa035860 Mon Sep 17 00:00:00 2001
From: vaeng <34183939+vaeng@users.noreply.github.com>
Date: Tue, 25 Feb 2025 14:15:29 +0100
Subject: [PATCH 1/4] docs: add ranged based for loop concept files

---
 concepts/ranged-loops/.meta/config.json |  6 ++
 concepts/ranged-loops/introduction.md   | 88 +++++++++++++++++++++++++
 concepts/ranged-loops/links.json        | 10 +++
 3 files changed, 104 insertions(+)
 create mode 100644 concepts/ranged-loops/.meta/config.json
 create mode 100644 concepts/ranged-loops/introduction.md
 create mode 100644 concepts/ranged-loops/links.json

diff --git a/concepts/ranged-loops/.meta/config.json b/concepts/ranged-loops/.meta/config.json
new file mode 100644
index 00000000..c4f05eeb
--- /dev/null
+++ b/concepts/ranged-loops/.meta/config.json
@@ -0,0 +1,6 @@
+{
+  "blurb": "Range-based loops in C++ provide a concise and readable way to iterate over elements in a container, eliminating index management and reducing errors.",
+  "authors": [
+    "vaeng"
+  ]
+}
diff --git a/concepts/ranged-loops/introduction.md b/concepts/ranged-loops/introduction.md
new file mode 100644
index 00000000..c4ce0cf2
--- /dev/null
+++ b/concepts/ranged-loops/introduction.md
@@ -0,0 +1,88 @@
+# Introduction
+
+When working with arrays or vectors in C++, iterating over their elements is a common task.  
+A range-based `for` loop provides a concise and readable way to access the elements without dealing with indices.
+
+## Basic Syntax
+
+A range-based `for` loop consists of the following parts:
+
+```cpp
+for (element_type temp_element : container) {
+    // Use temp_element inside the loop
+}
+```
+
+- `element_type`: The type of each element in the container.
+- `temp_element`: A temporary variable that holds the value of each element.
+- `container`: The array or vector to loop over.
+
+## Example Usage
+
+```cpp
+#include <vector>
+#include <iostream>
+
+std::vector<std::string> obscured_password {"Tooms", "roswell", "uFO", "scully", "tunguska", "Nicholas Lea", "oil", "1Breath"};
+for (std::string part : obscured_password) {
+    part = part.substr(0,1);
+    std::cout << part;
+}
+// Output: TrustNo1
+```
+
+The ranged loop syntax eliminates the need for manual indexing, making the code cleaner and less error-prone.
+Other languages reference to a similar access style as `for each` loop.
+
+The example above shows access by value.
+Although the `part` variable is changed inside the loop, the original vector remains unchanged.
+This means each element will be a copy and might produce overhead and may thus impact performance.
+
+## Using `const` for Read-Only Access
+
+If you do not need to modify the elements, using `const` ensures they remain unchanged:
+
+```cpp
+std::vector<std::string> file_name {"Jraphics", "Interchange", "Format"};
+for (const std::string part : file_name) {
+    std::cout << part.front();
+}
+// Output: JIF
+```
+
+This prevents accidental modification of `number` but will still make a copy with every side-effect from above.
+
+## Using References to Modify Elements
+
+With a reference (`&`) you can modify the original container and avoid copies:
+
+Range-based `for` loops also work with arrays:
+
+```cpp
+#include <array>
+
+std::array<std::string, 3> trilobites {"Johnny", "Joey", "Dee Dee", "CJ"};
+
+for (std::string& member : trilobites) {
+    member += " Ramone";
+}
+// trilobites now contains "Johnny Ramone", "Joey Ramone", "Dee Dee Ramone", "CJ Ramone"};
+```
+
+If it is not necessary to change the container content it is advisable to use a `const` reference, which can be further optimized by the compiler.
+
+## When to Use Range-Based Loops
+
+- When you need to read or modify each element in a container.
+- When you do not need to track indices explicitly.
+- When you want clearer, more maintainable code.
+
+## Limitations
+
+- You cannot modify the structure of a container (e.g., adding or removing elements) while iterating.
+- You cannot access the index of an element directly.
+
+~~~~exercism/note
+As the elements are always traversed in order and never skipped, you can build your own counter to get an index.
+If you really need the index of an element, a traditional `for` loop might be a better choice.
+~~~~
diff --git a/concepts/ranged-loops/links.json b/concepts/ranged-loops/links.json
new file mode 100644
index 00000000..31853ad0
--- /dev/null
+++ b/concepts/ranged-loops/links.json
@@ -0,0 +1,10 @@
+[
+    {
+        "url": "https://en.cppreference.com/w/cpp/language/range-for",
+        "description": "C++ reference on ranged for loops"
+    },
+    {
+        "url": "https://www.learncpp.com/cpp-tutorial/range-based-for-loops-for-each/",
+        "description": "Range-based for loops (for-each) chapter on Learn C++"
+    }
+]
\ No newline at end of file

From f5ab895d1e64694e98881a4fc2b2c3263feca16c Mon Sep 17 00:00:00 2001
From: vaeng <34183939+vaeng@users.noreply.github.com>
Date: Tue, 25 Feb 2025 14:37:20 +0100
Subject: [PATCH 2/4] fixup! docs: add ranged based for loop concept files

---
 config.json | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/config.json b/config.json
index aff67381..4d30d3fa 100644
--- a/config.json
+++ b/config.json
@@ -1350,6 +1350,11 @@
       "uuid": "5de475cc-5321-476f-bd19-a82b60284f5a",
       "slug": "literals",
       "name": "Literals"
+    },
+    {
+      "uuid": "6ae189b1-2195-4514-abb5-bd270ba5b7c4",
+      "slug": "ranged-loops",
+      "name": "Range-based For Loops"
     }
   ],
   "key_features": [

From 372f5e1528fb0891b97049f140e282c2a7b307b3 Mon Sep 17 00:00:00 2001
From: vaeng <34183939+vaeng@users.noreply.github.com>
Date: Wed, 26 Feb 2025 14:32:26 +0100
Subject: [PATCH 3/4] fixup! fixup! docs: add ranged based for loop concept
 files

---
 concepts/ranged-loops/introduction.md | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/concepts/ranged-loops/introduction.md b/concepts/ranged-loops/introduction.md
index c4ce0cf2..09b05288 100644
--- a/concepts/ranged-loops/introduction.md
+++ b/concepts/ranged-loops/introduction.md
@@ -50,7 +50,7 @@ for (const std::string part : file_name) {
 // Output: JIF
 ```
 
-This prevents accidental modification of `number` but will still make a copy with every side-effect from above.
+This prevents accidental modification of `part` but will still make a copy with every side-effect from above.
 
 ## Using References to Modify Elements
 
@@ -69,7 +69,13 @@ for (std::string& member : trilobites) {
 // trilobites now contains "Johnny Ramone", "Joey Ramone", "Dee Dee Ramone", "CJ Ramone"};
 ```
 
-If it is not necessary to change the container content it is advisable to use a `const` reference, which can be further optimized by the compiler.
+~~~~exercism/note
+If you don't change your loop variable, it is advisable to use `const` references.
+Copy operations can significantly slow your algorithms for larger data structures.
+The only exception for this rule are [fundamental types][fundamentals] like `int`, `char`, and `long`, as the reference process has more overhead than a simple copy.
+
+[fudnamentals]: https://en.cppreference.com/w/cpp/language/types
+~~~~
 
 ## When to Use Range-Based Loops
 

From c2fcc7264ec5f790e0e0befd536f0b7350be4ebf Mon Sep 17 00:00:00 2001
From: vaeng <34183939+vaeng@users.noreply.github.com>
Date: Wed, 26 Feb 2025 14:51:18 +0100
Subject: [PATCH 4/4] fixup! fixup! fixup! docs: add ranged based for loop
 concept files

---
 concepts/ranged-loops/introduction.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/concepts/ranged-loops/introduction.md b/concepts/ranged-loops/introduction.md
index 09b05288..0242a215 100644
--- a/concepts/ranged-loops/introduction.md
+++ b/concepts/ranged-loops/introduction.md
@@ -36,7 +36,7 @@ Other languages reference to a similar access style as `for each` loop.
 
 The example above shows access by value.
 Although the `part` variable is changed inside the loop, the original vector remains unchanged.
-This means each element will be a copy and might produce overhead and may thus impact performance.
+This means each element of the container will be a copied and assigned to the part variable - that may in turn impact performance.
 
 ## Using `const` for Read-Only Access