You are on page 1of 20

Iteration

• Iteration and recursion are the two mechanisms that allow a


computer to perform similar operation repeatedly.
• Programmers in imperative languages tend to use iteration more
than they use recursion
• recursion is more common in functional languages
• In most languages, iteration takes the form of loops
• There are two types of loops
i)Enumeration controlled loop
• -executed once for every value in a given finite set
• -the no of iterations is known in advance
ii)logically controlled loop
• -executed until some Boolean condition changes value
i)Enumeration controlled loop
•orginated with the do loop in fortran I
•In fotran 90
do i=1,10,2
end do
variable i- index of the loop 1-Initial value
10-bound 2 –step size
•The above loop will execute five times with i set to
1,3,5,7, and 9
•In Module 2
For i= first to last by stepdo
………
End
Combination loops
•C for loop is logically controlled and also its makes
enumeration easy
•Module -2 Example can be written in c as
Fo(i=first:i<=last;i+=step)
{
}
C defines this to be precisely equivalent to
{
i = first;
while(i<=last)
{
…………
i + = step;
}
}
Iterators:
•Iterators are the containers in which the elements of any
well-defined set are iterated
A)True iterators:
•Languages like clu, python,ruby,c# provides the iterators
that enumerate items.
•The iterator resembles a subroutine or function which
contains the yield statement each of which provides an
index value.
• The module-2 fragment
For i=first to last by step do
…………
Return yield
b) Iterator as objects
•Euclid, C++, Java have no yield statements.
•These languages iterator is an object that provides methods for
initialization, generation of the next value and testing for
completion.
Eg: Java
BinTree<Integer> myTree = ……
……….
For (integer i:my tree)
{
System.out.println(i);
}
Logically Controlled Loops:
•The most common approach is to test the condition before each iteration
•The familiar while loop syntax was introduced in algol-w
While condition do statement
a) Post test loops
•Test the condition at the bottom of the loop
Eg: pascal ,modula
Readln(line);
Until line[1]=’$’;
C provides the post test loop whose condition works
Do
{
Line = read_line(stdin);
}
While(line[0]!=’$’);
Mid test loops:
•Some languages uses the test condition in the middle of the
loop
•Mid test loop can be used as exit in ada,break in c and last in
perl
•Eg:
For(;;)
{
Line = read_line(stdin);
If (all_blanks(line)) break;
Consume_line(line);
}
Recursion
• It means
• To permit function to call themselves
• Or to call other function then call them back in return
Iteration and recursion
• Fortran 77 and certain other languages does not permit
recursion
• A few functional languages does not permit iteration
• Most modern language provides both mechanisms
• Iteration is based on the repeated modification of variables and
recursion is based variables which are not modified.
Recursion
• Eg:1)To compute the Sum ,It sems natural to use iteration
int summation(int_func f ,int low,int high)
{
int i,total=0;
for(i=low;i<=high;i++)
{
total+ = f(i)
}
return total;
}
 
Recursion

• 2)To computer gcd, recursion may seem more


natural
 gcd(a,b) = { a if a=b
{ gcd(a-b,b) if a>b
{ gcd(a,b-a) if b>a
eg: int gcd (int a,int b)
{
if( a = = b) return a;
else if (a >b) return gcd(a-b,b);
else return gcd(a,b-a);
}
 
b)Tail recursion:
•It is sometimes argued that iteration is more efficient than recursion
•It is more accurate to say that the implementation of iteration is usually more
efficient than implementation of recursion
•An optimizing compiler however designed for a functional language will be
able to generate an excellent code for recursive functions
•A tail recursive function is one which additional computations never follows
a recursive call
•Gcd function generated by the compiler
int gcd(int a,int b )
{
start:
If (a == b) return a;
else if (a>b)
a = a-b ;go to start;
else b = b-a ; goto start;
}
• Thinking recursively
• The Fibonacci numbers are defined by the mathematical
recurrence
  Fn ={ 1 if n=0 or n =1
{ f(n-1)+f(n-2) otherwise
Eg :
int fib(int n)
{
int f1=1,f2=1,i;
for(i=2;i<=n;i++)
{
int temp = f1+f2;
f1 =f2;
f2 = temp;
}
return f2
}
Records (Structures) and
Variants (Unions)

• Records types allow data of heterogeneous


types to be stored and manipulated together.
• Some languages C,C++ use the term structure
instead of the record
• Structures in C++ defined as special form of
class.
• Java has no distinguished notion of struct
• C## structs do not support inheirtance
Records (Structures) and
Variants (Unions)
• Syntax:
Struct element
{
Char name[2];
Int atomic_number;
Double atomic_weight;
};
Most lanaguges allow record definitions to be nested
Struct one {
char name[30];
struct{
char name[2];
Int atomic_number;
Double atomic_weight;
}
};
Records (Structures) and
Variants (Unions)

• Memory layout and its impact (structures)

Figure 8.1 Likely layout in memory for objects of type element on a 32-bit machine. Alignment restrictions lead
to the shaded “holes.”
Records (Structures) and
Variants (Unions)

• Memory layout and its impact (structures)

Figure 8.3 Likely memory layout for packed element records. The atomic_number and atomic_weight
fields are nonaligned, and can only be read or written (on most machines) via multi-instruction
sequences.
Records (Structures) and
Variants (Unions)

• Memory layout and its impact (structures)

Figure 8.4 Rearranging record fields to minimize holes. By sor ting fields according to the size of their
alignment constraint, a compiler can minimize the space devoted to holes, while keeping the fields aligned.
Variants (Unions)

• Many allowed the programmer to specify that


certain variables should be allocate on the top of
one another
• C’s syntax heavily influenced by algol 60
union {
int i;
double d;
Bool b;
};
Unions

• The overall size of the union would be that of its


largest number
• Unions have been used for two main purposes
i)The first arises in system programs
ii)To represent alternative sets of fields within a
record
Question

• What is recursion? Explain the working principle of a


recursion with the help of a suitable program
• Define iterator and explain the different types of iterators
• Explain in detail about structures and unions

You might also like