Vs Recursion
In Programming, any task that requires something to be done repeatedly can be solved either with iteration or recursion.
Iteration is generally done with a for loop, while recursion calls the same function.
Here's how we might print an array iteratively and recursively.
#include <stdio.h>
int main() {
int arr[5] = {1,2,3,4,5};
int length = 5;
for (int i = 0; i < length; i++) {
("%i\n", arr[i]);
printf}
}
#include <stdio.h>
void print_all_elements(int* arr, int i, int length) {
if (i >= length) return;
else {
("%i\n", arr[i]);
printfreturn print_all_elements(arr, i + 1);
}
}
int main() {
int arr[5] = {1,2,3,4,5};
int length = 5;
(arr, 0, length);
print_all_elements}