Recursion

Learn the Basic Java Concepts Loops, arrays and methods
6 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. So last time we went over methods to do, your job was to create four methods print for Print to print one, and print three. And then your job was to build it functionality to those methods. So that if they were called for a main, it would just print, it would print the number of the method and then print all the other methods numbers as well. So essentially, this is what should have done. So here, I just have four methods, each of them have a string call that they take in, and then if that call equals zero, then it's going to execute all the other methods.

If that call on the other hand is equal to one, then we just print one or mean is not equal to zero. Then we print the actual method number, so 123 or four. And so now if you run this project, you'll see We get 1234 There we go. So that's actually should have done if they get that's okay. This really wasn't a very personally, I'd say a very good exercise to do. I just couldn't really think of anything to do with methods and methods or it's kind of hard to think of exercise without actually knowing what we're going to learn today.

So today, we're going to be learning about recursion, which is essentially kind of like loops, except it's by calling a method. So let's see exactly what it is in a second here. So yeah, I guess. Let's get into today's lesson. Alright, so as always, let's go ahead and create a class here. So we're going to create new, whoops, whoops, nope, nope, nope, nope, nope, no.

New class. And we're going to create a What are we gonna create? We're going to create a recursion. Hope I spelled that right. There we go. I am confident that I misspelled that.

But you know. Alright, let's create a main method. public static void main main string args. There we go. Okay, and now we can actually go ahead and implement recursion. So first of all, we're actually going to create a method.

So we're going to create a method called, let's say print one. All right, just going to be public. Let's do static void, just static. static void print one. Okay, now we're going to go ahead and do is inside print one, we can do print one. There we go.

So then after that, we'll we'll do this. So we're going to have int a, that we're going to pass in and then we're going to have If a is less than 100. Else, System dot out dot print ln. Okay. All right, we're gonna print a post one. There we go.

And I just realized we don't need these else here. So we can just keep it like this. There we go. All right. And so now if you run this There you go. As you can see now prints all numbers from 100 down to one.

So let's go ahead we can actually change this put system out print ln before the if statement. And like that, we get all numbers from one to 100. So let's go and actually shorten the range from 100 to 10. And seeing what's going on here. So when you first call this method, so we print one, we pass in one, we go ahead and print it. So we print one, so there it is.

There's one Let's go to run it just that there's 10. Yeah, save it, there we go. So we print one, then, if a is less than 10, which it is, we're going to print one, and then pass in a plus one. So this is going to be two, and that's going to print two, then if two is less than 10, it's going to pass in two plus one, which is three, then it prints three, and then 45678. And then it gets to 10. And then once it passes in 10, it checks is a less than 10.

It is, no I'm sorry, it's not. So this shows false and result, it doesn't actually execute the method anymore, and it stops working. So that is essentially how recursion works. And recursion is actually a pretty useful thing. There's a lot of different things that we can do using recursion, like for example, finding the factorial of a number. So that's what I'm gonna do right now together.

So let's go and delete this. Delete changes to factorial and this method is going to return an int all right there going. So what are we going to do? So if a is equal to one, then we're gonna return one. Okay? And then we're going to do is return a times factorial, a minus one.

All right, there we go. Okay, and now let's go ahead and do this. So we're going to System dot out dot print ln. And then we're let's let's just do five factorial equals plus factorial. Five. There we go.

All right, so now let's go ahead and run this. five factorial equals 120. There we go. And now if we change this to let's just say 100 to 100, order under a factorial will be equal to, actually it'll be equal to zero since we're working with integers, but if we change this to let's just say 10 just for a reason, a more reasonable number, then we'll get 33,628,800. And so there we go. Alright, so that is how we use recursion in Java.

Now recursion itself may not seem like something very useful. So essentially, it's a little bit like loops just a little bit. But never, it's just a different way of thinking about loops. But there are certain things, notably factorials that recursion can do pretty well. So anyway, your homework is to create a recursive method, which will just print all the numbers from one to 100 excluding odd numbers, so only the even numbers from one to 100. And so yeah, you wouldn't include one there since it's an even number.

So anyway, I wish you luck with homework and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.