Last updated on September 4th, 2023 at 09:48 pm
Here, We will learn about the iterative algorithm, iteration, implementation and properties of iteration.
Iterative Algorithms:
An Iterative Algorithm that calls repeatedly but for a finite number of times.
It use constructs like loops and sometime other data structures like stacks and queues to solve the problems.
Iteration is a technique in which function call repeatedly for a finite number of times.
Implementation
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;
}
}
Properties of Iteration
- Iteration uses repetition structure.
- It terminates when the loop condition fails.
- It does not use the stack so it’s faster than recursion.
- It consumes less memory.
- Infinite looping uses CPU cycles repeatedly.
- It makes code longer.
Related:
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.😎