International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014), pp.203-210 http://dx.doi.org/10.14257/ijseia.2014.8.3.18
Proposing a New Hybrid Controlled Loop George Tsaramirsis1,*, Salam Al-jammoor2 and Seyed M. Buhari1 1
Faculty of Computing and Information Technology, King Abdulaziz University, Jeddah, Saudi Arabia 2 Computer science department, School of basic education, Suleimania University, Suleimania, Kurdistan, Iraq * Corresponding Author:
[email protected] Abstract Counter controlled and condition controlled are main categories of iterative statements, with “for”, “while” and “do while” loops dominating the field. While there are clear distinctions between them, it is not uncommon for a programmer to practically convert a condition controlled loop to do counter controlled task or vice versa. For instance, there is a “while loop” that will be iterated a number of times or a “for loop” with an additional condition next to the counter checker. This paper proposes a new loop that will be both counter and condition controlled. The loop will include an in-built predefined counter and it will be able to iterate a user defined number of time, prior to checking the user defined condition. A detailed explanation and a C++ implementation of the proposed loop are included in this paper. Keywords: iterative statements, loop, hybrid control loop
1. Introduction The repeated execution of a statement or compound statement is achieved either by iteration or recursion. Most iteration statements fall under the category of counter controlled, condition controlled or collection controlled loops, depending on what mechanism they use to terminate the repetition. Members of the first category depend on a counter that is usually part of the definition of the loop in order to terminate it. Condition controlled loops maintain the iteration while the condition is satisfied. The condition is usually part of the syntax of the loop and the body includes the code that will make it false and cause the end of the loop. The last category includes a collection of data that the loop goes through all of them or through those that satisfy a certain condition. Most languages support both condition based and counter controlled loops with “while” and “for” loops leading the scene as they are supported by the most popular languages (C, Java. C++) [1]. However, it is not uncommon for a programmer to practically convert a condition controlled loop to counter or vice versa. Consider a “while loop” with an in-build counter added by the programmer in the body of the loop, that will be terminated after it is executed a number of times or a “for loop” with an additional condition next to the counter checker. The following code illustrates examples for both cases. int i = 0; while (i < 10) { // do something i++; }
ISSN: 1738-9984 IJSEIA Copyright ⓒ 2014 SERSC
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
for (int i = 1; i