Iterative Statements in Programming Language
Paradigms
Aadil Ahmed Adam
Computer Science Department
University of Alabama in Huntsville
Huntsville, Alabama
aa0079@uah.edu
Abstract—This paper discusses about a common feature
“iterative statements” in the various programming
language paradigm. To describe how iteration occurs in
the four programming language paradigms, i.e.,
imperative programming, object oriented programming,
functional programing and logic programming, the
following languages such as C, Java, Lisp and Prolog
respectively have been considered. The paper compares
and contrasts these language examples based on the
factors such as Syntax, semantics, readability, write
ability, reliability, implementation and evolution of a
feature called as “iterative statements”. It also describes
the advantages and disadvantages of this feature in the
programming language paradigm. We start with an
introduction to iteration and then describe the four
programming languages based on each of the
characteristics. Syntax and semantics section tells us how
iterative constructs are written in a particular language.
The goal of this paper is make one understand how
iteration supports programmers in solving a problem, its
implementation in these languages and evolution of the
constructs over time.
Index Terms—Imperative paradigm, object oriented
paradigm, functional paradigm, logic paradigm, iteration,
syntax, semantics.
I. DEFINITIONS
These are some of the important terms frequently used in
the paper and we ought to know them in order to understand
the paper.
Programming paradigm: “A programming paradigm is a
fundamental style of computer programming, serving as a way
of building the structure and elements of computer programs”
[1].
Imperative paradigm: Based on the von Neumann-Eckert
architecture, “it is a model where both the program and its
variables are stored together, and the program contains a series
of commands that perform calculations, assign values to
variables, and retrieve input, produce output, or redirect control
elsewhere in the series” [2].
Object oriented paradigm: “Object-oriented programming
(OOP) is a programming paradigm based on the concept of
"objects", which are data structures that contain data, in the
form of fields, often known as attributes; and code, in the form
of procedures, often known as methods”[1].
Functional paradigm: As per Tucker & Noonan, functional
programming “models a computational problem as a collection
of mathematical functions, each with an input (domain) and a
result (range) spaces”. [2]
Logic paradigm: According to Tucker & Noonan, “Logic
(declarative) programming allows a program to model a
problem by declaring what outcome the program should
accomplish, rather than how it should be accomplished.” It is
also called as rule-based programming since the program’s
declarations are a set of rules or constraints on a problem rather
than a sequence of commands. [2]
II. INTRODUCTION
A programming language paradigm contains various
control statements which cause the flow of execution to
advance based on the changes to the state of the program. The
basic control statements can be put into three categories:
selection statements, iteration statements, and jump constructs.
In this paper we will be concentrating on iteration in the
various languages.
Iteration is “a repetition of a block of statements within a
computer program.”[3] Various iterative constructs are used in
programming languages such as for () loop, while () loop and
do-while () loop to name a few.
Let’s have an example of iteration relying on destructive
assignment, as in imperative pseudocode-
Sum =0
for i from 1 to 4 // loop four times
{
Sum=sum + i //add the current value of i to sum
}
Print sum // display the value of sum
In this program fragment, the value of the variable i
changes over time, this changing value is a characteristic of
iteration. Now let’s look at the syntax and semantics of
iterative statements in the following languages.
III. SYNTAX AND SEMANTICS
Every programming language possesses syntax and
semantics.
Syntax refers to the way symbols may be combined to
create well format sentences (or programs) in a language. It
solely deals with the form and structure of a symbol in a
language [1].
Semantics refers to the meaning of these sentences (or
program), describing the behavior of a computer while
executing a program in a language [1].
Let’s consider each of the language paradigm and describe
the syntax and semantics of the iterative constructs.
A. Imperative Programming Paradigm-C:
C Programming consists of three types of iterative
constructs: for loop, while loop, do-while loop. [4]
The iterative construct: for loop is an entry controlled loop.
It is one of the most useful types of loops in programming. The
syntax for the for-loop is:
for (initialization ; test condition ; increment / decrement) {
// Body of the loop;
} [4]
The initialization allows us to declare a variable and assign
a value to it or assign a value to an existing variable. The test
condition tells that while the conditional expression is true, the
loop should continue to repeat. The increment (or decrement)
section in a for-loop takes care of changing the variables. For
e.g. z++, i=i+15 etc. A semicolon separates each of these
sections.
The iterative construct: while- loop is an entry controlled
loop, the loop body will execute until the test condition fails.
The syntax for the while-loop is given below: [2] [4]
while (test _condition) {
// Body of the loop;
}
The test condition can consist of any combination of
Boolean statements that can be legal. The body of the loop will
execute until the condition is true. Notice that unlike the for-
loop, the while-loop does not have initialization or the
increment/decrement section.
The next iterative construct is the do-while loop. It is an
exit controlled loop, since the body of the loop will execute
at least once even if the test condition is false. The syntax
of the while loop is given below:
do{
//body of loop
} while ( test condition); [2] [4]
B. Object Oriented Programming Paradigm- Java
Java‘s iterative statements are for, while, and do-while.
These statements create what we commonly call loops. Java
has a loop to fit any programming need.
Beginning with JDK5, there are two forms of for-loop. The
first is the traditional form that has been in use since the
original version of Java [5]. The second is the new “for-each”
form. The syntax for the traditional for() loop is:
for (initialization; test condition; iteration) {
// body of the for-loop
} [5]
The semantics is similar to that of for() loop in C
programming.
Contemporary language theory has embraced the for-each
loop concept, and is becoming a standard feature that
programmers have to except. Unlike other programming
languages like C#. We do not have to use the “for-each”
keyword, Java adds the for-each loop capability by enhancing
the for() loop. [5] The general form of the for-each version is
shown here:
for(type iteration-variable : collection) {
statement block
} [5]
Type specifies the type and iteration-variable specifies the
name of an iteration variable that will receive the elements
from a collection, one at a time. The for-each style for
automates the preceding loop. It eliminates the need of a loop
counter and manually index the array unlike the for() loop
which reads the entire array in a strict sequential order. We will
see how the for-each loop is implemented in the later section.
The while loop is Java’s most fundamental loop statement.
Here is its general form:
while(condition) {
// body of loop
} [5]
The condition can be any Boolean expression. When
condition becomes false, the control comes out of the loop and
the next line is executed.
Sometimes we would like to test the termination expression
at the end of loop instead of the beginning. Java supplies the
do-while loop. The general form is:
do{
//body of loop
}while(test condition); [5]
The do-while loop always executes the body of the loop
once since the test condition is at the bottom of the loop. As
with the java’s loops, test condition must be a Boolean
expression.
C. functional programming paradigm- Lisp
The constructs loop, loop for, do, dolist and dotimes are the
available looping constructs in Lisp. These statements execute
a body of code once for each value taken by a single variable.
The loop construct allows the programmer to execute some
statements repeatedly until a return statement is found. The
general syntax is here:
(loop (s-expressions))
The general syntax of the loop-for construct is here:
(loop for loop-variable in <a list>
do (action)
)
The most powerful iterative form in Common Lisp is do
construct. Because “do” is so powerful, its template is complex
[6].
(DO ((VARIABLE-1 INIT-VALUE1 OPTIONAL-
UPDATE-VALUE1)
(VARIABLE-2 INIT-VALUE2 OPTIONAL-UPDATE-
VALUE2)
(VARIABLE-N INIT-VALUEN OPTIONAL-UPDATE-
VALUEN))
(LOOP-TERMINATION-TEST CONSEQUENT-1
CONSEQUENT-2 . . . CONSEQUENT-N)
BODY) [6]
Variables may be initialized in “do” and their values
optionally updated at each iteration. A LOOP-
TERMINATION-TEST terminates processing. One or more
consequences may be evaluated when the loop terminates. If
the condition to terminate processing is false, the body of
the DO is evaluated [6].
The Common Lisp primitive “dolist” is like “dotimes”,
except that it operates on lists. Like dotimes, dolist has a loop-
control-variable, an optional result, and a body. The loop-
control-variable serves as an index to the list. The loop-control-
variable increments until it is equal to the length of the list [6].
The template for “dolist” is:
(DOLIST (<LOOP-CONTROL-VARIABLE> <LIST>
<OPTIONAL-RESULT>) <BODY>) [6]
Dotimes provides straightforward iteration over a sequence
of integers. Dotimes, as its name suggests, performs a
specified task(s) a prescribed number of times. Dotimes
increments a loop-control-variable up to (but not
including) an upper-bound. You may optionally specify a
return value when the loop is terminated. The body of the
loop specifies the task(s) that should be completed during
each iteration. The syntax of the dotimes is here: [6]
(DOTIMES (<LOOP-CONTROL-VARIABLE> <UPPER-
BOUND> <OPTIONAL-RESULT>) <BODY>) [6]
4. Logical programming paradigm- Prolog
As it is common in many programming tasks, we often
wish to repeatedly perform some operation, either over a whole
data-structure, or until a certain point is reached. The way we
typically do this in Prolog is by recursion. It simply means that
a program calls itself repeatedly until some final point is
reached. Being frequently used in Prolog, it means that we
have a first fact that acts as some stopping condition followed
up by some rule(s) that performs some operation before
revoking itself.
IV. READABILITY,WRITABILTY & RELIABLITY
The iteration constructs in C, for-loop, while-loop and the
do-while loop are useful when the programmers want to iterate
over a block of statements. For loop is useful when the
programmer knows how many times to repeat.so, it is called a
counter-controlled loop. If it is important that the loop
complete at least once before checking for the stopping
condition, or if it is not meaningful to check the stopping
condition before the loop has executed at least once, then use a
do-while loop. A do-while loop is executed at least once. This
can be useful to write more readable code, although admittedly
it gets used far less often than for and while loops and the
potential of mixing it up with a while loop when skimming
through code can actually make code less readable.
The for-each loop in Java is certainly the popular feature
from Java 5. The for-each loop increases the abstraction level,
i.e. the developer does not have to express the low level details
of how to loop around a list or an array, they just have to
simply state it. But the benefit is lost soon when they have to
access the index or remove an item from the list (or array). The
process of converting the loop back to be the index or iterator
is painful because the old style of looping is more verbose and
less clear. The enhanced for loop offers a concise higher level
syntax to loop over a list which improves readability and
clarity thus making it more reliable. The while loop and the do-
while is similar to the iterative constructs in C programming.
Any iterative construct need not be reliable because it
has to loop number of times mentioned in the condition.
There is no iterative construct in prolog and is replaced
by recursion in prolog. The main advantage of iteration over
recursion is efficiency, mainly space efficiency. We know that
pure prolog clause is iterative, if it has one recursive call in the
body. Recursive solutions consume more space and processor
time than iterative solutions. Compilers, optimizers, and smart
programming can help overcome this, but there are still many
cases where we must coerce a naturally recursive solution to
be iterative. Prolog does not have storage variables to store the
results of the intermediate computation of a recursive
procedures. This increases the complexity of the program and
makes it unreliable.
V. IMPLEMENTATION
A. Iterative statements in C
Let’s look at the execution of the looping constructs in C
programming in the section.
Execution of for loop:
• Initialize or declare any loop control variable.
• Evaluation of test condition takes place. If the
condition is true, then the body executes.
• After the execution of the statements in the body of
the loop, the value of loop control variable is either
incremented or decremented, again the validation of
the test condition occurs.
When the test condition fails, the body of the loop does
not execute and the loop terminates and control passes to
the next statements after the loop.
Execution of while loop:
• If the test condition is always true, indefinite loop
will arise.
• If the condition is false, the loop control comes out of
the condition and terminates and executes the next
line after the while loop.
Execution of do-while loop:
• When the control of the program reaches the do, the
body of the loop will always execute once, then it
will check the test condition after the first execution
of loop body.
• This ensures that the body of the loop is executed at
least once.
If the condition fails then the loop terminates and executes
other statements after the loop.
B. Iterative statements in Java
The for() loop is a powerful and versatile construct in
Java. The for() loop operates as follows: [4]
• when the loop first starts, the initialization portion of
the loop is executed. This is generally an expression
that sets the value of the loop control variable, which
acts as a counter that controls the loop.
• Next the test condition is executed. If this expression
is true, then the body of the loop is executed. If the
expression is false then the loop terminates.
• Next, the iteration portion of the loop is executed.
This is an expression that usually increments or
decrements the loop counter variable. This process
repeats until the controlling expression is false.
Consider the below example,
Class Add{
Public static void main(string args[]) {
Sum=0;
// here , n is declared inside the for loop
for( int n=5;n>0;n--)
sum= sum+n;
system.out.println(“total “ + sum);
}
} [4]
In this example, the for loop contains the loop counter
variable n initialized to 5, the test condition is true since the
value of n is greater than 0, so the body of the loop is executed
,and the computation is done. Then the value of n is
decremented one at a time and when the value of n becomes 0
then the loop terminates.
If the three expressions of the for loop are empty; an
infinite loop can be created as follows:
// infinite loop
for ( ; ; ) {
// code goes here
}
Let’s see how a while loop in java is implemented with an
example,
Class Example {
Public static main(int args[]){
int i=100,j=200;
while(++i < --j); // no body for this loop
system.out.println(midpoint is “ +i);
}
} [4]
Here, the value of i is incremented and value of j
decremented. The values are compared. If the new value of i is
still less than new value of j, then the loop repeats. If i is equal
to or greater than j, then the loop terminates. Upon exit, i will
hold the value that is the value midway and display it.
C. Iterative statements in lisp
The loop construct allows us to execute statement(s)
repeatedly until a return statement is found.
The loop-for construct allows us to implement a for-loop.
The dotimes construct allows a fixed number of iteration.
Lets consider an example,
? (dotimes (counter 5)
(print counter))
0
1
2
3
4
NIL [6]
In above example, [6] we use dotimes to increment the
loop-control-variable “counter”. As we enter the loop, the
variable counter starts counting at 0. We enter the body of the
loop and print the value of counter. Because the value
of counter is less than the upper-bound, counter increments by
1 and the body of the loop is executed again. The iterative
process stops when counter equals the upper-bound.
The do construct provides a structured form of iteration.in
this example below, we implement the same algorithm as the
dotimes using do.
? (do ((counter 0 (incf counter)))
((= counter 5) 'done)
(print counter))
0
1
2
3
4
DONE [6]
We enter the do in the first statement. We initialize the
variable counter to 0. The loop-termination-test (= counter
5) is evaluated. If the evaluation is false, then do
construct executes its body and prints the value of counter. On
the next iteration, do updates the variable counter by
incrementing it by one. The loop-termination-test is evaluated
and because it evaluates to false, the body is executed again.
This process repeats until the value of counter is equal to 5.
The consequent clause on loop termination evaluates the
quoted symbol DONE. [6]
The dolist construct allows iteration through each item
in list. Consider the example below, [6]
? (dolist (counter '(60 61 62 ) 'done)
(print counter))
60
61
62
DONE
In this fragment of code, the dolist iterates over the list of
numbers given. The counter checks for the end of the list and
print the output as the numbers of the list are read and the
done variable is reached which marks the end of the file.
D. Recursion in Prolog
Recursion is the only iterative method available in Prolog.
However, tail recursion is often implemented as iteration. ‘tail
recursion‘ corresponds to a while-loop in an imperative
programming language. For example,
fact(0,1).
fact(N,F) :- N > 0, fact(N,1,F). [7]
Here the second argument functions as an accumulator.
The accumulator is used to store the partial product in the
implementation of recursion, a data structure has to be
maintained for every recursive call that has not been
terminated [7]. Every recursive computation would require n
recursive procedure calls, so the space linear is n, a tail
recursion optimizes the prolog recursive program. Here is an
example,
parent(mike,jake). /* jake is mike's parent */
parent(jake,tom). /* tom is jake's parent */
parent(tom,lucy). /* lucy is tom's parent */
ancestor(X,Y):- parent(X,Y). /* someone is your ancestor if
there are your parent */
ancestor(X,Y):- parent(X,Z), /* or somebody is your ancestor
if they are the parent */ancestor(Z,Y). /* of someone who is
your ancestor */ [2]
The above program finds ancestors, by trying to link up
people according to the database of parents at the top to the
card. So let's try it out by asking
?- ancestor(mike,tom).
The first clause of ancestor looks to see if there exists a
clause that could match the goal parent(mike,tom). This fails
to match, as a result we try the second clause of ancestor. We
now pose the query parent(mike,Z). This results in us
choosing clause one of parent and binding Z=jake. As a result,
we now pose the recursive query ancestor(jake,tom). Applying
the ancestor rule again, we first try the first clause. This means
we check for parent(jake,tom)., which successfully matches
the second clause of parent. As a result the goal
ancestor(jake,tom) succeeds. This in turn leads to the goal
ancestor(mike,tom) succeeding and Prolog responding yes to
the query. [2]
VI. EVOLUTION
There are not many changes in the iterative constructs in c
programming. The major notable changes we can say are the
goto statement is not used in the present versions of c
now.K&R C programming has the goto statement. It has the
following syntax:
goto_statement ::= goto label
label ::= identifier
The other notable change is the do construct was equivalent
to the while statement in K&R C programming.
For() loop has come a long way in Java8 with the new for-
each method. For() loop is there from the beginning of Java 1.0
and almost all programmers have used the traditional for()
loop. Since the introduction of the new for-each loop in java
1.5, everything changed from looping over a collection and
arrays. A more elegant solution took over which was shorter,
cleaner and less error prone. In java 8, looping has taken a
major step into a new avatar, for-each method. It takes the help
of lazy evaluation and in built parallelism of stream interface.
Pre JDK1.5, this is a way of iterating over the array or list,
List<string>
countries=arrays.aList(“usa’,”india”,”uae”,”china”,”korea”
);
for(int j=0;i<countries.size():j++){
system.out.println(countries.get(j));
}
So when the advanced for each loop was introduced, the
following style of looping over a collection changed as,
for(string country: countries){
system.out.println(country);
}
Common lisp is a dialect of the original lisp and all the
iterative constructs are the same with not many changes.
Recursion is prolog is the result of logic programming.
Since the syntax and semantics of prolog programming
consists of only facts and rules, we cannot code a for () loop or
a while in the same syntax as we state in other programming
languages. Using the recursive nature of the rules, we can solve
all the problems in prolog and produce all the solutions.
VI. CONCLUSION
Iteration is always been an important feature in
programming languages. It is used to solve major problems
where the programmer needs to repeat a set of statements for a
particular computation in a problem. Different languages
implement this feature differently. We cannot say that once
feature is good and the other bad. It is always suitably
implemented based on the compiler and the time it takes to
process those statements efficiently. The iterative statements in
C and Java are syntactically similar and efficient. The iterative
constructs in Lisp are different in terms of syntax and
semantics and not user friendly. Recursion in Prolog is the
alternative to iteration. Since programing in Prolog is based on
rules and facts, we cannot have for() loop, while() loop as in
other programming language paradigms. But internally the
recursive implementation happens the same an iterative
processing happens on the stack [7]. On the whole, any
implementation should lead to better utilization of resources
and execute any problem efficiently with less overhead and
error prone execution.
REFERENCES
[1] http://www.ijarcsse.com/docs/papers/Volume_5/8_Augus
t2015/V5I8-0344.pdf
[2] Allen B. Tucker and Robert E. Noonan, Programming
Languages- Principles and Paradigm, Second edition,
International Version, 2007.
[3] https://en.wikipedia.org/wiki/Iteration
[4] http://www.circuitsgallery.com/2012/07/loops-or-
iterative-statements-in-c.html
[5] Herbert Schildt, The Complete Reference JAVA, Indian
version, seventh edition, 2014.
[6] http://quob.lib.umich.edu/s/spobooks/bbv9810.0001.001/
1:17/--algorithmic-composition-a-gentle-introduction-to-
music?rgn=div1:view=fulltext
[7] https://books.google.com/books?id=wXjuvpOrjMC&pg=
PA154&lpg=PA154&dq=iteration+in+prolog&source=bl
&ots=4YB2VGFYKt&sid=9AW8DG88glxZ7VACQVT
ZCvYPcSg&hl=en7sa=
X&ved=0CDMQ6AEwA2oVChMI5ovlidGEyQIVw2
MmCh343AS1#v=onepage&q=iteration%20in%20prolog
&f=false

final pl paper

  • 1.
    Iterative Statements inProgramming Language Paradigms Aadil Ahmed Adam Computer Science Department University of Alabama in Huntsville Huntsville, Alabama aa0079@uah.edu Abstract—This paper discusses about a common feature “iterative statements” in the various programming language paradigm. To describe how iteration occurs in the four programming language paradigms, i.e., imperative programming, object oriented programming, functional programing and logic programming, the following languages such as C, Java, Lisp and Prolog respectively have been considered. The paper compares and contrasts these language examples based on the factors such as Syntax, semantics, readability, write ability, reliability, implementation and evolution of a feature called as “iterative statements”. It also describes the advantages and disadvantages of this feature in the programming language paradigm. We start with an introduction to iteration and then describe the four programming languages based on each of the characteristics. Syntax and semantics section tells us how iterative constructs are written in a particular language. The goal of this paper is make one understand how iteration supports programmers in solving a problem, its implementation in these languages and evolution of the constructs over time. Index Terms—Imperative paradigm, object oriented paradigm, functional paradigm, logic paradigm, iteration, syntax, semantics. I. DEFINITIONS These are some of the important terms frequently used in the paper and we ought to know them in order to understand the paper. Programming paradigm: “A programming paradigm is a fundamental style of computer programming, serving as a way of building the structure and elements of computer programs” [1]. Imperative paradigm: Based on the von Neumann-Eckert architecture, “it is a model where both the program and its variables are stored together, and the program contains a series of commands that perform calculations, assign values to variables, and retrieve input, produce output, or redirect control elsewhere in the series” [2]. Object oriented paradigm: “Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods”[1]. Functional paradigm: As per Tucker & Noonan, functional programming “models a computational problem as a collection of mathematical functions, each with an input (domain) and a result (range) spaces”. [2] Logic paradigm: According to Tucker & Noonan, “Logic (declarative) programming allows a program to model a problem by declaring what outcome the program should accomplish, rather than how it should be accomplished.” It is also called as rule-based programming since the program’s declarations are a set of rules or constraints on a problem rather than a sequence of commands. [2] II. INTRODUCTION A programming language paradigm contains various control statements which cause the flow of execution to advance based on the changes to the state of the program. The basic control statements can be put into three categories: selection statements, iteration statements, and jump constructs. In this paper we will be concentrating on iteration in the various languages. Iteration is “a repetition of a block of statements within a computer program.”[3] Various iterative constructs are used in programming languages such as for () loop, while () loop and do-while () loop to name a few. Let’s have an example of iteration relying on destructive assignment, as in imperative pseudocode- Sum =0 for i from 1 to 4 // loop four times { Sum=sum + i //add the current value of i to sum } Print sum // display the value of sum In this program fragment, the value of the variable i changes over time, this changing value is a characteristic of iteration. Now let’s look at the syntax and semantics of iterative statements in the following languages.
  • 2.
    III. SYNTAX ANDSEMANTICS Every programming language possesses syntax and semantics. Syntax refers to the way symbols may be combined to create well format sentences (or programs) in a language. It solely deals with the form and structure of a symbol in a language [1]. Semantics refers to the meaning of these sentences (or program), describing the behavior of a computer while executing a program in a language [1]. Let’s consider each of the language paradigm and describe the syntax and semantics of the iterative constructs. A. Imperative Programming Paradigm-C: C Programming consists of three types of iterative constructs: for loop, while loop, do-while loop. [4] The iterative construct: for loop is an entry controlled loop. It is one of the most useful types of loops in programming. The syntax for the for-loop is: for (initialization ; test condition ; increment / decrement) { // Body of the loop; } [4] The initialization allows us to declare a variable and assign a value to it or assign a value to an existing variable. The test condition tells that while the conditional expression is true, the loop should continue to repeat. The increment (or decrement) section in a for-loop takes care of changing the variables. For e.g. z++, i=i+15 etc. A semicolon separates each of these sections. The iterative construct: while- loop is an entry controlled loop, the loop body will execute until the test condition fails. The syntax for the while-loop is given below: [2] [4] while (test _condition) { // Body of the loop; } The test condition can consist of any combination of Boolean statements that can be legal. The body of the loop will execute until the condition is true. Notice that unlike the for- loop, the while-loop does not have initialization or the increment/decrement section. The next iterative construct is the do-while loop. It is an exit controlled loop, since the body of the loop will execute at least once even if the test condition is false. The syntax of the while loop is given below: do{ //body of loop } while ( test condition); [2] [4] B. Object Oriented Programming Paradigm- Java Java‘s iterative statements are for, while, and do-while. These statements create what we commonly call loops. Java has a loop to fit any programming need. Beginning with JDK5, there are two forms of for-loop. The first is the traditional form that has been in use since the original version of Java [5]. The second is the new “for-each” form. The syntax for the traditional for() loop is: for (initialization; test condition; iteration) { // body of the for-loop } [5] The semantics is similar to that of for() loop in C programming. Contemporary language theory has embraced the for-each loop concept, and is becoming a standard feature that programmers have to except. Unlike other programming languages like C#. We do not have to use the “for-each” keyword, Java adds the for-each loop capability by enhancing the for() loop. [5] The general form of the for-each version is shown here: for(type iteration-variable : collection) { statement block } [5] Type specifies the type and iteration-variable specifies the name of an iteration variable that will receive the elements from a collection, one at a time. The for-each style for automates the preceding loop. It eliminates the need of a loop counter and manually index the array unlike the for() loop which reads the entire array in a strict sequential order. We will see how the for-each loop is implemented in the later section. The while loop is Java’s most fundamental loop statement. Here is its general form: while(condition) { // body of loop } [5] The condition can be any Boolean expression. When condition becomes false, the control comes out of the loop and the next line is executed. Sometimes we would like to test the termination expression at the end of loop instead of the beginning. Java supplies the do-while loop. The general form is: do{ //body of loop }while(test condition); [5] The do-while loop always executes the body of the loop once since the test condition is at the bottom of the loop. As with the java’s loops, test condition must be a Boolean expression. C. functional programming paradigm- Lisp The constructs loop, loop for, do, dolist and dotimes are the available looping constructs in Lisp. These statements execute a body of code once for each value taken by a single variable. The loop construct allows the programmer to execute some statements repeatedly until a return statement is found. The general syntax is here: (loop (s-expressions)) The general syntax of the loop-for construct is here: (loop for loop-variable in <a list>
  • 3.
    do (action) ) The mostpowerful iterative form in Common Lisp is do construct. Because “do” is so powerful, its template is complex [6]. (DO ((VARIABLE-1 INIT-VALUE1 OPTIONAL- UPDATE-VALUE1) (VARIABLE-2 INIT-VALUE2 OPTIONAL-UPDATE- VALUE2) (VARIABLE-N INIT-VALUEN OPTIONAL-UPDATE- VALUEN)) (LOOP-TERMINATION-TEST CONSEQUENT-1 CONSEQUENT-2 . . . CONSEQUENT-N) BODY) [6] Variables may be initialized in “do” and their values optionally updated at each iteration. A LOOP- TERMINATION-TEST terminates processing. One or more consequences may be evaluated when the loop terminates. If the condition to terminate processing is false, the body of the DO is evaluated [6]. The Common Lisp primitive “dolist” is like “dotimes”, except that it operates on lists. Like dotimes, dolist has a loop- control-variable, an optional result, and a body. The loop- control-variable serves as an index to the list. The loop-control- variable increments until it is equal to the length of the list [6]. The template for “dolist” is: (DOLIST (<LOOP-CONTROL-VARIABLE> <LIST> <OPTIONAL-RESULT>) <BODY>) [6] Dotimes provides straightforward iteration over a sequence of integers. Dotimes, as its name suggests, performs a specified task(s) a prescribed number of times. Dotimes increments a loop-control-variable up to (but not including) an upper-bound. You may optionally specify a return value when the loop is terminated. The body of the loop specifies the task(s) that should be completed during each iteration. The syntax of the dotimes is here: [6] (DOTIMES (<LOOP-CONTROL-VARIABLE> <UPPER- BOUND> <OPTIONAL-RESULT>) <BODY>) [6] 4. Logical programming paradigm- Prolog As it is common in many programming tasks, we often wish to repeatedly perform some operation, either over a whole data-structure, or until a certain point is reached. The way we typically do this in Prolog is by recursion. It simply means that a program calls itself repeatedly until some final point is reached. Being frequently used in Prolog, it means that we have a first fact that acts as some stopping condition followed up by some rule(s) that performs some operation before revoking itself. IV. READABILITY,WRITABILTY & RELIABLITY The iteration constructs in C, for-loop, while-loop and the do-while loop are useful when the programmers want to iterate over a block of statements. For loop is useful when the programmer knows how many times to repeat.so, it is called a counter-controlled loop. If it is important that the loop complete at least once before checking for the stopping condition, or if it is not meaningful to check the stopping condition before the loop has executed at least once, then use a do-while loop. A do-while loop is executed at least once. This can be useful to write more readable code, although admittedly it gets used far less often than for and while loops and the potential of mixing it up with a while loop when skimming through code can actually make code less readable. The for-each loop in Java is certainly the popular feature from Java 5. The for-each loop increases the abstraction level, i.e. the developer does not have to express the low level details of how to loop around a list or an array, they just have to simply state it. But the benefit is lost soon when they have to access the index or remove an item from the list (or array). The process of converting the loop back to be the index or iterator is painful because the old style of looping is more verbose and less clear. The enhanced for loop offers a concise higher level syntax to loop over a list which improves readability and clarity thus making it more reliable. The while loop and the do- while is similar to the iterative constructs in C programming. Any iterative construct need not be reliable because it has to loop number of times mentioned in the condition. There is no iterative construct in prolog and is replaced by recursion in prolog. The main advantage of iteration over recursion is efficiency, mainly space efficiency. We know that pure prolog clause is iterative, if it has one recursive call in the body. Recursive solutions consume more space and processor time than iterative solutions. Compilers, optimizers, and smart programming can help overcome this, but there are still many cases where we must coerce a naturally recursive solution to be iterative. Prolog does not have storage variables to store the results of the intermediate computation of a recursive procedures. This increases the complexity of the program and makes it unreliable. V. IMPLEMENTATION A. Iterative statements in C Let’s look at the execution of the looping constructs in C programming in the section. Execution of for loop: • Initialize or declare any loop control variable. • Evaluation of test condition takes place. If the condition is true, then the body executes. • After the execution of the statements in the body of the loop, the value of loop control variable is either incremented or decremented, again the validation of the test condition occurs. When the test condition fails, the body of the loop does not execute and the loop terminates and control passes to the next statements after the loop. Execution of while loop:
  • 4.
    • If thetest condition is always true, indefinite loop will arise. • If the condition is false, the loop control comes out of the condition and terminates and executes the next line after the while loop. Execution of do-while loop: • When the control of the program reaches the do, the body of the loop will always execute once, then it will check the test condition after the first execution of loop body. • This ensures that the body of the loop is executed at least once. If the condition fails then the loop terminates and executes other statements after the loop. B. Iterative statements in Java The for() loop is a powerful and versatile construct in Java. The for() loop operates as follows: [4] • when the loop first starts, the initialization portion of the loop is executed. This is generally an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. • Next the test condition is executed. If this expression is true, then the body of the loop is executed. If the expression is false then the loop terminates. • Next, the iteration portion of the loop is executed. This is an expression that usually increments or decrements the loop counter variable. This process repeats until the controlling expression is false. Consider the below example, Class Add{ Public static void main(string args[]) { Sum=0; // here , n is declared inside the for loop for( int n=5;n>0;n--) sum= sum+n; system.out.println(“total “ + sum); } } [4] In this example, the for loop contains the loop counter variable n initialized to 5, the test condition is true since the value of n is greater than 0, so the body of the loop is executed ,and the computation is done. Then the value of n is decremented one at a time and when the value of n becomes 0 then the loop terminates. If the three expressions of the for loop are empty; an infinite loop can be created as follows: // infinite loop for ( ; ; ) { // code goes here } Let’s see how a while loop in java is implemented with an example, Class Example { Public static main(int args[]){ int i=100,j=200; while(++i < --j); // no body for this loop system.out.println(midpoint is “ +i); } } [4] Here, the value of i is incremented and value of j decremented. The values are compared. If the new value of i is still less than new value of j, then the loop repeats. If i is equal to or greater than j, then the loop terminates. Upon exit, i will hold the value that is the value midway and display it. C. Iterative statements in lisp The loop construct allows us to execute statement(s) repeatedly until a return statement is found. The loop-for construct allows us to implement a for-loop. The dotimes construct allows a fixed number of iteration. Lets consider an example, ? (dotimes (counter 5) (print counter)) 0 1 2 3 4 NIL [6] In above example, [6] we use dotimes to increment the loop-control-variable “counter”. As we enter the loop, the variable counter starts counting at 0. We enter the body of the loop and print the value of counter. Because the value of counter is less than the upper-bound, counter increments by 1 and the body of the loop is executed again. The iterative process stops when counter equals the upper-bound. The do construct provides a structured form of iteration.in this example below, we implement the same algorithm as the dotimes using do. ? (do ((counter 0 (incf counter))) ((= counter 5) 'done) (print counter)) 0 1 2 3 4 DONE [6] We enter the do in the first statement. We initialize the variable counter to 0. The loop-termination-test (= counter 5) is evaluated. If the evaluation is false, then do construct executes its body and prints the value of counter. On the next iteration, do updates the variable counter by incrementing it by one. The loop-termination-test is evaluated and because it evaluates to false, the body is executed again. This process repeats until the value of counter is equal to 5. The consequent clause on loop termination evaluates the quoted symbol DONE. [6]
  • 5.
    The dolist constructallows iteration through each item in list. Consider the example below, [6] ? (dolist (counter '(60 61 62 ) 'done) (print counter)) 60 61 62 DONE In this fragment of code, the dolist iterates over the list of numbers given. The counter checks for the end of the list and print the output as the numbers of the list are read and the done variable is reached which marks the end of the file. D. Recursion in Prolog Recursion is the only iterative method available in Prolog. However, tail recursion is often implemented as iteration. ‘tail recursion‘ corresponds to a while-loop in an imperative programming language. For example, fact(0,1). fact(N,F) :- N > 0, fact(N,1,F). [7] Here the second argument functions as an accumulator. The accumulator is used to store the partial product in the implementation of recursion, a data structure has to be maintained for every recursive call that has not been terminated [7]. Every recursive computation would require n recursive procedure calls, so the space linear is n, a tail recursion optimizes the prolog recursive program. Here is an example, parent(mike,jake). /* jake is mike's parent */ parent(jake,tom). /* tom is jake's parent */ parent(tom,lucy). /* lucy is tom's parent */ ancestor(X,Y):- parent(X,Y). /* someone is your ancestor if there are your parent */ ancestor(X,Y):- parent(X,Z), /* or somebody is your ancestor if they are the parent */ancestor(Z,Y). /* of someone who is your ancestor */ [2] The above program finds ancestors, by trying to link up people according to the database of parents at the top to the card. So let's try it out by asking ?- ancestor(mike,tom). The first clause of ancestor looks to see if there exists a clause that could match the goal parent(mike,tom). This fails to match, as a result we try the second clause of ancestor. We now pose the query parent(mike,Z). This results in us choosing clause one of parent and binding Z=jake. As a result, we now pose the recursive query ancestor(jake,tom). Applying the ancestor rule again, we first try the first clause. This means we check for parent(jake,tom)., which successfully matches the second clause of parent. As a result the goal ancestor(jake,tom) succeeds. This in turn leads to the goal ancestor(mike,tom) succeeding and Prolog responding yes to the query. [2] VI. EVOLUTION There are not many changes in the iterative constructs in c programming. The major notable changes we can say are the goto statement is not used in the present versions of c now.K&R C programming has the goto statement. It has the following syntax: goto_statement ::= goto label label ::= identifier The other notable change is the do construct was equivalent to the while statement in K&R C programming. For() loop has come a long way in Java8 with the new for- each method. For() loop is there from the beginning of Java 1.0 and almost all programmers have used the traditional for() loop. Since the introduction of the new for-each loop in java 1.5, everything changed from looping over a collection and arrays. A more elegant solution took over which was shorter, cleaner and less error prone. In java 8, looping has taken a major step into a new avatar, for-each method. It takes the help of lazy evaluation and in built parallelism of stream interface. Pre JDK1.5, this is a way of iterating over the array or list, List<string> countries=arrays.aList(“usa’,”india”,”uae”,”china”,”korea” ); for(int j=0;i<countries.size():j++){ system.out.println(countries.get(j)); } So when the advanced for each loop was introduced, the following style of looping over a collection changed as, for(string country: countries){ system.out.println(country); } Common lisp is a dialect of the original lisp and all the iterative constructs are the same with not many changes. Recursion is prolog is the result of logic programming. Since the syntax and semantics of prolog programming consists of only facts and rules, we cannot code a for () loop or a while in the same syntax as we state in other programming languages. Using the recursive nature of the rules, we can solve all the problems in prolog and produce all the solutions. VI. CONCLUSION Iteration is always been an important feature in programming languages. It is used to solve major problems where the programmer needs to repeat a set of statements for a particular computation in a problem. Different languages implement this feature differently. We cannot say that once feature is good and the other bad. It is always suitably implemented based on the compiler and the time it takes to process those statements efficiently. The iterative statements in C and Java are syntactically similar and efficient. The iterative constructs in Lisp are different in terms of syntax and semantics and not user friendly. Recursion in Prolog is the alternative to iteration. Since programing in Prolog is based on rules and facts, we cannot have for() loop, while() loop as in other programming language paradigms. But internally the
  • 6.
    recursive implementation happensthe same an iterative processing happens on the stack [7]. On the whole, any implementation should lead to better utilization of resources and execute any problem efficiently with less overhead and error prone execution. REFERENCES [1] http://www.ijarcsse.com/docs/papers/Volume_5/8_Augus t2015/V5I8-0344.pdf [2] Allen B. Tucker and Robert E. Noonan, Programming Languages- Principles and Paradigm, Second edition, International Version, 2007. [3] https://en.wikipedia.org/wiki/Iteration [4] http://www.circuitsgallery.com/2012/07/loops-or- iterative-statements-in-c.html [5] Herbert Schildt, The Complete Reference JAVA, Indian version, seventh edition, 2014. [6] http://quob.lib.umich.edu/s/spobooks/bbv9810.0001.001/ 1:17/--algorithmic-composition-a-gentle-introduction-to- music?rgn=div1:view=fulltext [7] https://books.google.com/books?id=wXjuvpOrjMC&pg= PA154&lpg=PA154&dq=iteration+in+prolog&source=bl &ots=4YB2VGFYKt&sid=9AW8DG88glxZ7VACQVT ZCvYPcSg&hl=en7sa= X&ved=0CDMQ6AEwA2oVChMI5ovlidGEyQIVw2 MmCh343AS1#v=onepage&q=iteration%20in%20prolog &f=false