R Nested Loops
🔁🔁 R Nested Loops
They are commonly used when working with tables, matrices, patterns, or multi-dimensional data.
🔹 What is a Nested Loop?
Outer loop runs first
Inner loop runs completely for each iteration of the outer loop
👉 Inner loop executes multiple times.
🔹 1. Nested for Loop (Basic Example)
Output:
🔹 2. Nested Loop for Multiplication Table
✔ Useful for tables and patterns
🔹 3. Nested Loop with while
🔹 4. Nested Loop with break
✔ break exits inner loop only
🔹 5. Nested Loop with next
✔ Skips only the current inner iteration
🔹 6. Common Use Cases of Nested Loops
✔ Working with matrices
✔ Creating patterns
✔ Comparing each element with others
✔ Multi-level data processing
🔹 Performance Tip ⚠️
Nested loops can be slow for large data.
👉 Prefer vectorized operations or apply() functions when possible.
📌 Summary
Nested loop = loop inside another loop
Inner loop runs fully for each outer loop iteration
Can use
fororwhilebreakandnextaffect inner loopUseful but avoid for very large data
