Module: (Java) Subroutines. Recursion.


Problem

2/10

Recursion. Cycle Simulation

Theory Click to read/hide

We have seen that recursion is the repeated execution of contained instructions in a subroutine. And this, in turn, is similar to the work of the cycle. There are programming languages ​​in which the loop construct is absent at all, for example, Prolog. 
Let's try to simulate the operation of the for loop. 
The for loop contains a step counter variable. In a recursive subroutine, such a variable can be passed as a parameter. //LoopImitation() procedure with two parameters //First parameter – step counter, second parameter – total number of steps void LoopImitation(int i, int n) { cout << "Hello N" << i << endl; // Operator to be repeated for any value of i if (i < n) //Until the loop counter equals the value n, { //call a new instance of the procedure, with the i+1 parameter (go to the next i value) LoopImitation(i+1, n); } }

Problem

Study the program below and issue a procedure call with parameters i=1, n=10 in the main program #include <iostream> using namespace std; //LoopImitation() procedure with two parameters //First parameter – step counter, second parameter – total number of steps void LoopImitation(int i, int n) { cout << "Hello N" << i << endl; // Operator to be repeated for any value of i if (i < n) //Until the loop counter equals the value n, { //call a new instance of the procedure, with the i+1 parameter (go to the next i value) LoopImitation(i+1, n); } } main(){ // here it is necessary to issue a procedure call with parameters i=1, n=10 }