Зміст курсу
Java Extended
Java Extended
Recursion
How to easily break your code?
Why can this break the code? Because it can lead to infinite recursion, which will endlessly consume memory and reduce the device's performance. So why do we need recursion at all? In some cases, recursion can be useful, but it should be used carefully. For example, recursion can replace a while loop. Let's look at an example of using recursion to calculate the sum of all numbers up to a number passed as a parameter:
Main
package com.example; public class Main { // main method to run the application public static void main(String[] args) { // do not modify the code below System.out.println(calculateSum(5)); } // method to calculate the sum of numbers from 1 to num using recursion static int calculateSum(int num) { // if num is greater than 0, recursively calculate the sum if (num > 0) { num = num + calculateSum(num - 1); } else { return 0; } return num; } }
The method calculateSum()
calls itself with reduced values. We also have an exit point from this recursion when the variable num
becomes zero. This method calculates the sum of all numbers from 1 to the parameter, in our case 5
. Let's look at the flowchart that shows how recursion works using this method as an example:
The example above demonstrates how the method calls itself with reduced values, and when it reaches zero, it goes back up by summing the values. We can also observe how the body of the if
block is executed, where we add num
to the result of the method call with a reduced value. The intermediate values after each method call are indicated near the arrows.
Can we avoid using recursion?
Recursion can also be replaced with a regular loop. Let's look at an example where we first use a recursive method and then use a for
loop to perform the same operation:
Main
package com.example; public class Main { // main method to run the application public static void main(String[] args) { // do not modify the code below int num = 5; // printing the result of the recursive sum calculation System.out.println("Result using recursion: " + calculateSum(num)); int result = 0; // calculating the sum using a for-loop for (int i = 1; i <= num; i++) { result = result + i; } // printing the result of the sum calculation using the for-loop System.out.println("Result using for-loop: " + result); } // method to calculate the sum of numbers from 1 to num using recursion static int calculateSum(int num) { // if num is greater than 0, recursively calculate the sum if (num > 0) { num = num + calculateSum(num - 1); } else { return 0; } return num; } }
Here we can see how we replace our recursive method with a regular for
loop, using the variable i
to increment our result
by i
each time until it reaches the specified number num
, which is the parameter in the method.
Conclusion
In conclusion, recursive methods can be useful but also risky. Recursion can often be replaced by loops, and while it can simplify certain tasks, it can also cause issues if not used carefully. If you choose to use recursion, even with experience, make sure to proceed with caution.
Дякуємо за ваш відгук!