Why does this recursive function not work?
Code:
Clear[i];
Clear[v];
final= 4;
Recursion = Function[{v},
For[i = 1, i <= 2, i++,
Print["Current level ", v ];
Print["Try: ", i];
If[v == final,
Print["End"];,
Recursion[v + 1]; (*else-case*)
];
Print["Back! i:", i];
];
]
Recursion[1];
Out:
0: Current level 1
1: Try: 1
2: Current level 2
3: Try: 1
4: Current level 3
5: Try: 1
6: Current level 4
7: Try: 1
8: End
9: Back! i:1
10: Current level 4
11: Try: 2
12: End
13: Back! i:2
14: Back! i:3
15: Back! i:4
16: Back! i:5
Heeelp
In the 14th line, "i" should be =2, current level: 3 and try: 2.. then i=1, then i=2.. like a binary tree.
Why is this happening?!