Today Objectives

Iteration

<aside> 💡 Iteration is a repetition of a mathematical or computational procedure applied to the result of a previous application.

</aside>

In programming, iteration is often referred to as looping, because when a program iterates it loops to an earlier step. Iterative approach uses for (…) or white (…) do loops to solve problems

Simple pseudo-code to calculate the factorial of an integer

factorial(n)
1: fac = 1
2: for = 1 -> n do
3:			fac = fac *i
4: end for
5: return fac

Iteration gives a lot of advantages:

<aside> 💡 Iteration is not the only way to deal with problems in which same problems are repeated.

</aside>

Consider the following example: compute the factorial of a given integer n

int factorial(int n){
    int fac = 1;
    int i;
    for(i = 1; i <= n; i++)
        fac = fac*i;
    return fac;
}
int fac(int n){
    if (n == 1)
        return 1;
    else
        return n*fac(n-1);
}