Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 26: Recursion Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 class Previously main()

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 class Previously main() class class class

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 One last thing Recursion is a programming technique in which a method can call itself.

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Case A public static void main(String[]args) { sayHello(); } public static void sayHello() { System.out.println("Hello"); sayHello(); // recursive }

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Case B public static void main(String[]args) { sayHello(5); } // a recursive method will always have a condition public static void sayHello(int num) { if (num == 0) System.out.println("Hello and Bye!"); else { System.out.println("Hello"); sayHello(num-1); // recursive } }

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Case C public static void main(String[]args) { printNumbers(10); } // a recursive method will always have a condition public static void printNumbers(int num) { if (num == 0) System.out.println(num); else { System.out.println(num); printNumbers(num-1); // recursive System.out.println(num); } }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Recursion • Each time a recursive method is called, a new copy of the method runs, with new instances of parameters and local variables being created • As each copy finishes executing, it returns to the copy of the method that called it • When the initial copy finishes executing, it returns to the part of the program that made the initial call to the method

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Types of Recursion • Direct recursion: a method calls itself • Indirect recursion: method A calls method B, and method B calls methodA. Or, method A calls method B, which calls ..., which calls method A

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Recursive Factorial Method The natural definition of some problems leads to a recursive solution • The factorial of a nonnegative integer n is the product of all positive integers less or equal to n • Factorial of n is n * (n-1) * ... * 2 x 1 • The factorial of 0 is 1

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Recursive Factorial Method // Factorial of n can be expressed in terms of // the factorial of n-1 // 0 ! =1 // n ! = n x (n-1) ! public static int factorial(int n) { if (n == 0) return 1; else return n*factorial(n-1); }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Recursive Fibonacci Numbers The natural definition of some problems leads to a recursive solution • The Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... • After the starting 0, 1, each term is the sum of the two preceding terms • Recursive solution: fib(n) = fib(n – 1) + fib(n – 2); • Base cases: fib(0) =0, fib(1) =1

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Recursive Fibonacci Numbers public static int fib (int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n – 1) + fib(n – 2); }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Reference Chapter 5, pages 228 - 232

Slide 15

Slide 15 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.