# Difference Between Recursion and Iteration

Here, We will learn about recursion, iteration, differences between recursion and iteration and their code in java.

### Recursion

Recursion is a technique in which function calls itself until a base condition is satisfied. A recursive method solves a problem by calling a copy of itself to work on a smaller problem

##### Example Recursion Code in Java:
``````public static long fib (long n)
{
if (n <= 1)
return n;
else
return fib (n-1) + fib (n-2);```Code language: Java (java)```

### Iteration

Iteration is a technique in which function call repeatedly for a finite number of times. It use constructs like loops and sometime other data structures like stacks and queues to solve the problems.

##### Example Iteration Code in Java:
``````public static long fib (long n)
{
if ((n == 1) || (n == 2)){
return 1;
}
else{
long prev = 1, current = 1, next = 0;
for (long i=3; i<=n; i++){
next = prev + current;
prev = current;
current = next;
}
return next;
}
}```Code language: PHP (php)```

### Difference between Recursion and Iteration

#### Pow(x n) LeetCode Solution

Implement pow(x, n), which calculates x raised to the power n.

#### Wildcard Matching LeetCode Solution

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support…

#### Recursive Algorithms

Here, We will learn about recursive algorithm, recursion, recursive function, implementation, properties and examples of…

### Want to Contribute:-

If you like “To The Innovation” and want to contribute, you can mail your articles to 📧 contribute@totheinnovation.com. See your articles on the main page and help other coders.😎

Scroll to Top