Iterative Statement In Programming Language
Iterative statement is the programming language design feature that is going to be discussed. And the programming languages for which the feature is discussed are C, Perl, Prolog and Common Lisp.
In C the three pre-defined iterative statements are for, while and do while.
In Perl the pre-defined iterative statements include for, foreach, while, do while and until.
In Common Lisp the pre-defined iterative statements include do, dotimes, dolist and loop (includes for and while)
In Prolog there are no iterative statements. Since Prolog is a fact and rule based language and the programmers are allowed to provide with the info what needs to be achieved rather than how it needs to be achieved there are no iterative statements in Prolog. …show more content…
• In do while loop statements are executed without any condition check for first iteration and are executed only if the condition is true for the successive iterations.
• until is used to iterate statements until a particular expression or condition is evaluated to true.
• foreach and dolist are used to iterate statements over an array or a list. Both the loops can have a variable whose value represents an element of the list for each iteration.
• dotimes is similar to dolist but instead of iterating over the values of a list, it iterates the value of a variable up to a specified count. The value of the variable ranges from 0 to specified count – 1.
• do is a flexible iterator construct and can be used to replace dotimes and dolist. Unlike dotimes and dolist which provides only one loop variable do supports an arbitrary number of loop variables.
• loop is an iterator when coupled with constructs like when, for, while, finally… called as extended loop and loop without them is called a simple loop. A simple loop is an infinite loop whereas extended loop can be made as a finite loop.
Syntax for iterative statements in …show more content…
clause2(Y) :- clause2(X).
Semantics of Iterative statements in C: for: The loop commences by initializing the loop variable. The condition is first checked before executing the statements. After each iteration the increment/ decrement of the loop variable done after which the condition is checked. The loop terminates once the condition is evaluated to false. while: The loop starts by checking the condition/expression and continues only if the condition/expression is evaluated to true. Then the statements in the while block is executed. The iteration continues until the expression/condition is evaluated to false. dowhile: The loop starts by executing the statements first and then checking the expression/condition the loop continues to execute the statements for each iteration until the expression is evaluated to true.
Semantics of Iterative statements in Perl: The semantics of for, while and dowhile in Perl is same as that of C. Perl has two other iterative statements such as foreach and until. foreach: foreach in Perl iterates through the values of the list provided. It has an optional loop variable whose value is equal to the value in the list for which the iteration occurs. The statements of the loop are executed for each iteration. The loop terminates once all the values in the list are