Proposing a New Hybrid Controlled Loop
Click here to load reader
-
Upload
truonglien -
Category
Documents
-
view
213 -
download
0
Transcript of Proposing a New Hybrid Controlled Loop
![Page 1: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/1.jpg)
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
ISSN: 1738-9984 IJSEIA Copyright ⓒ 2014 SERSC
Proposing a New Hybrid Controlled Loop
George Tsaramirsis1,*
, Salam Al-jammoor2 and Seyed M. Buhari
1
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++;
}
![Page 2: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/2.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
204 Copyright ⓒ 2014 SERSC
for (int i = 1; i <= 10; i++)
// do something
Additionally, the do while loop, executes the code in the body of the loop one time
prior of checking the condition.
This paper is proposing a new loop that is a hybrid between for, while and do while
loops offering all their capabilities in one loop. The proposed loop has one or two
arguments and an inbuilt counter. The first argument is how many times the loop should
iterate its body prior of checking the condition, while the second argument is the
condition if any. The three proposed syntaxes are:
The first will iterate X times and then stop. The second will iterate X times and then
check the condition. The last will check the condition before execute the code in its body.
While the ideas presented in this paper are simple and easy to implement, they can
simplify the iteration statements and make them easier for new programmers to learn.
The next section of this paper presents a brief summary on how most programming
languages are dealing with iterations. Section three, presents the proposed loop in detail
and it includes an implementation in C++ to serve as proof of concept. Section four,
discuss the advantages of the proposed syntaxes and finally section five presents the
conclusions of this paper.
2. Iteration Statements in Programming Languages
Different programming languages support repetition of instructions using various
techniques. One of the oldest mechanisms used for instruction repetition is the go -
to command. The go-to is presented in a number of languages such as ADA, C,
Fortran, Basic and Algol 60 that were unconditionally transferring control to another
part of the code. In languages such as Basic and Fortran, the go-to requires the
number of lines in the source code that will be executed next while languages such
as ADA and C transfer control to the code that follow pre-defined labels. The
programmer can place labels anywhere in the source code. Other languages such as
Algol 60, allow the go-to to transfer control to both lines of the source code and
labels. GOTOs can be useful features, “eliminate duplicate code” [2] and resu lt in
“faster and smaller code”. [3] However, “In practice, use of gotos tends to violate
structured programming principles” [2]. Böhm proved that it is possible to solve any
computational problem without the use of go-to [4]. “Methodical decomposition,
refinement, and selection of control structures automatically leads to goto -free
programs in most cases” [2] and in fact languages such as Java use control
statements instead “go-to” statements.
Another way to achieve repetition is the use of iterative control statements. Such
statements are known as loops. Loops repeat fragments of code and depending on
the mechanism that they use to stop, are categorized as counter controlled, condition
controlled and collection controlled loops.
Counter controlled loops use a variable with a numeric value as a counter in order
to keep track of how many times has been executed. This counter is usually part of
the syntax of the loop. Once the counter research a user defined value the loop
stops. In some languages such as ADA, FORTRAN 77 and 90 the value of the
counter can’t be changed from the body of the loop because they are evaluated only
once at the beginning of the loop. On the other hand languages like C, C++, Java
![Page 3: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/3.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
Copyright ⓒ 2014 SERSC 205
and C# allow the counter to be changed from inside the body as it is evaluated after
each iteration. [5] The syntax of counter controlled loops for incrementing from one
to ten is described below in a number of languages:
Ada:
for i in 1 .. 10 loop
-- do something
end loop;
Bash:
for i in 1 2 3 4 5 6 7 8 9 10
do
# do something
done
C, C++, C#, Java, JavaScript, Perl, PHP:
for (int i = 1; i <= 10; i++)
// do something
Even if the syntax between the above languages looks the same there are some
differences. The main point is that some languages such as Java, has Boolean condition
while C/C++ allows expressions as condition.
FORTRAN:
do i = 1, 10, 1
// do something
end do
In the above code, the “i” is initialized to 1 and the maximum that will reach is 10 and
the step is 1. So it will start from 1 and increment by 1 until it reaches 10.
Lua:
for i = 1, 10 do
-- do something
end
Pascal:
for I := 1 to 10 do
(*do something*);
Python:
for i in range(1, 10):
# do something
The above will give the values from 1 to 9 but not 10.
Smalltalk:
1 to: 10 do: [ :i | "do something" ]
Another famous category is condition controlled loops. Condition controlled loops also
known as logical controlled loops, iterate while a user defined condition is true. This
condition is defined as part of the syntax of the loop. The question that is raised is
whether the loop is executed first and then the condition is checked (pre-test) or if the
![Page 4: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/4.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
206 Copyright ⓒ 2014 SERSC
condition is checked first and if it is satisfied the code in the body is executed (post-test).
Most of the programming languages that support iterative control statement support pre-
test but not all support post-test. For example Ada does not support post-test [5]. The
syntax of pre-test and post-test loops (if available), for incrementing from one to ten,
while satisfying a condition, is described below in a number of languages:
Ada:
begin
while i < 10 loop
-- do something
i := i + 1;
end loop;
Ada first tests the condition and then executes. There are ways to achieve post-test loop
in Ada but there is no default ways for doing it.
Bash:
i=0
while [ $i -lt 10 ]; do
# do something
i=$((i + 1))
done
Like Ada, Bash also has no post-test controlled loop.
C, C++, C#, Java, JavaScript, Perl, PHP:
int i = 0;
while (i < 10)
{
// do something
i++;
}
The above code will first test the condition and then execute. An alternative known as
“do while” is the post-test version.
int i = 0;
do {
// do something
} while (i < 10);
FORTRAN:
integer :: i = 0
do while (i < 10)
i = i + 1
end do
Fortran, and Lua have no post-test controlled loops.
Lua:
i = 0
while i < 10 do
i = i + 1
end
Pascal:
Var i: integer;
begin
![Page 5: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/5.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
Copyright ⓒ 2014 SERSC 207
i := 0;
while i < 10 do
begin
(*do something*);
i := i + 1
end;
The above code will first test the condition and then execute. A post-test version of the
loop is shown below.
repeat
(*do something*);
i := i + 1;
until i = 10;
Python:
i = 0
while i < 10:
# do something.
i += 1
Python and SmalTalk have no post-test loop by default.
SmallTalk:
i := 0.
[ i < 10 ] whileTrue:
# do something.
i := i + 1 ].
In this section we have shown an example of iterating a loop 10 times, using a counter
or a condition, in a number of programming languages. The purpose of this was to have a
benchmark for our loop. The proposed loop will be discussed in some detail in the
following section.
3. The Proposed Loop
The proposed loop attempts to combine the “for”, “while” and “do while” loops
as they appear in C++ with a new simpler syntax. The key idea behind the loop is
that most of times we use simple iterations [7]. The proposed loop has three
syntaxes.
loop(number){ // do something }
This will execute the body a number of times without checking any condition.
loop(condition){ // do something }
The second syntax include pre-test of a condition like C++’s while loop.
loop(number,condition){ // do something }
Finally, the loop is able to run a pre-defined number of times prior of testing the
condition. The parameters of the loop can be modified from the body of the loop.
The loop includes two reserved keywords. “loopCount” and “loopStep”. The first
holds the current value of an inbuilt counter. The second holds the step that will
make the loop to increment or decrement. The loop can restart at any time by setting
the loop Counter to 0 or any other starting value in the body of the loop. Early exit
![Page 6: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/6.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
208 Copyright ⓒ 2014 SERSC
is supported using the break; statement like C++. A possible implementation of the
proposed loop is presented in the following section.
4. Implementation
The aim of this paper is to propose a new simpler syntax. The proposed loop is
currently implemented in C++ based on the language’s pre-processor and tested
online at [6]. This is to serve as a proof of concept and we would be honored to see
our syntax included in other languages. The code below defines three syntaxes of
the loop function. The first _loop(X), will iterate from X to 1. The appropriate loop
function is chosen based on the number of arguments passed. The loop(X) with one
argument syntax, can accept an integer or a Boolean as parameter. If the argument is
an integer, it will iterate from 0 until X-1. If the argument is a Boolean then the
counter will increment from zero to the max value that an integer can take and it
will be depended on the Boolean condition to be false in order to terminate the loop.
The last syntax takes two arguments, an integer X and a Boolean expression con,
and it will first iterate from zero to X without checking any condition and then start
checking the condition. If both the counter evaluation and condition are false then
the loop will terminate.
The code above can be included in any C++ code. The loop can be used in the main
function like any other loop.
int main()
{ loop(10)
// loopCount is an inbuilt variable cout << loopCount;
return 0;
}
The aim of this section is to illustrate a possible implementation of the proposed
loop in order to offer a better understanding to the readers.
5. Discussion
The new loop is a hybrid approach that combines the counter controlled loops
with the condition controlled loops. In the proposed loop the syntax for executing 10
times, starting from 1 and with step 1, is:
loop(10){ // do something }
![Page 7: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/7.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
Copyright ⓒ 2014 SERSC 209
As it can be seen the syntax is simpler than all the loops discussed in Section 2, and
there is no need to differentiate between logical or counter controlled loop as this is
hidden within the loop. It is worth to be noted that most of the time we use loops for
simple things. Let’s assume that we want to use a condition instead of a number. The code
will change to:
loop(loopCount<10){ // do something }
The above is simpler than a while loop because there is no need to define any counters.
Let’s consider another example where there is a need to decrement two steps at a time,
from 10 to 2 and print the value of the counter.
_loop(10) {
loopStep=-2;
cout << loopCount;
}
Modifying the loopStep and loopCounter parameters allow the programmers to
express complex expressions.
The code of the proposed post-test loop is:
Loop(10,loopCounter<10){
// do something
}
The above code is similar to the “do while” loop of C++ with the difference that the
code will first iterate 10 times and then it will start checking the condition, while in “do
while” loop, it runs only one time prior of testing the condition. This is the main
contribution of this work
The computational complexity of loop function with one or two parameters is
discussed here. Presence of a single parameter, which is counter based, causes the loop to
be executed a specified number of times, denoted as N. Thus, the complexity is O(N). For
a loop function with single parameter, which is condition based, the evaluation is based
on the condition. For the dual parameter loop function, the computation complexity is:
O(Counter) + O(Condition) = O(N) + ???
Considering that O(Condition) has an upper bound of O(N), the total computational
complexity of the dual parameter loop function is still O(N). This proves that the upper
bound and scalability of the loop function is mainly restricted by the Counter-based
parameter.
6. Conclusions and Future Work
The aim of this paper was to propose a new syntax of a loop that combines the
counter controlled loops with the condition controlled loops and makes it easier for
programming simple iterations. The motivation comes by combining C++ “for”,
“while” and “do while” loops. The loop has three possible syntaxes. a) loop(int), b)
loop(condition), c) loop(int, condition). The first will iterate a number of times
without testing any conditions. The second will conduct a pre-test and if it is
satisfied it will iterate once before checking the condition again. The last will iterate
a user defined number of times without testing any conditions and then start testing
the condition. The loop can be modified to express more complex statements, by
modifying two inbuilt variables. The loopCount holds the current value of the
counter and the loopStep holds the step value. However, a current limitation is that
these are absent in the case of loop(condition). Early exit is supported using the C++
break command. The paper also provides a C++ implementation of the loop, to be
used as proof of concept.
In the future we will improve the syntax by including the loop(int A, int B)
syntax that will iterate from A to B and the loop(-Number) syntax that will replace
![Page 8: Proposing a New Hybrid Controlled Loop](https://reader037.fdocuments.net/reader037/viewer/2022100805/588341101a28ab17198be52d/html5/thumbnails/8.jpg)
International Journal of Software Engineering and Its Applications Vol.8, No.3 (2014)
210 Copyright ⓒ 2014 SERSC
the _loop(Number). Additionally, we hope to offer implementations of the proposed
loop in more programming languages and receive more feedback about the usability
of the proposed syntax. This will allow us to improve further.
References
[1] Programming Language Popularity, www.LangPop.com. Last accessed, (2013) February 2.
[2] R. Wa, “Code Complete: A practical Handbook of Software Construction”, Microsoft Press,
http://www.stevemcconnell.com/ccgoto.htm, (1993).
[3] D. E. Knuth, “Structured Programming with go to Statements”, ACM Computing Surveys, vol. 6, no. 4,
(1974) December, pp. 261-301.
[4] J. Böhm, “Flow diagrams, turing machines and languages with only two formation rules”, Comm. ACM,
vol. 9, no. 5, (1966) May, pp. 366-371.
[5] S. Mohamed Buhari, “Principles of programming languages”, A paradigm approach, Tata McGraw-Hill
Education, New Delhi, (2010).
[6] Codepad http://codepad.org/.
[7] E. Larson, “Program Analysis Too Loopy? Set the Loops Aside”, 11th IEEE International Working
Conference on Source Code Analysis and Manipulation (SCAM), (2011) September 25-26, pp. 15-24.
Authors
Seyed Buhari, is an Assistant Professor in the Information
Technology Department, King Abdulaziz University, Jeddah,
Saudi Arabia. He received his B.E. degree in Computer
Engineering from Madurai Kamaraj University, India, in 1996,
and M.E. degree in Computer Science and Engineering from
Bharathiar University, India, in 1998. He has obtained his Ph.D
in Information Technology from Multimedia University,
Malaysia. From 2006 to 2012, he was with University Brunei
Darussalam, Brunei Darussalam. From 2000 to 2006, he was
with Information and Computer Science Department, King Fahd
University of Petroleum and Minerals, Saudi Arabia. His current
research interests are in the areas of cognitive radio networks,
grid computing, IPv6 performance testing and high performance
computing.
George Tsaramirsis, is an Assistant Professor in the Information
Technology Department, King Abdulaziz University, Jeddah, Saudi
Arabia. George is currently supervising one PhD candidate at City
University London. George’s recent work experience included
working for Accenture UK, as well as smaller IT companies in
London, UK. Dr. George received his PhD from King's College
London, University of London, UK. George's current research
interests lie within Software engineering and its applications.
Salam Al-jammoor, is an assistant lecturer in the university of
sulaymaniyah, Iraq. Salam has more than 7 years experience teaching
undergraduate students in the university. He is specializing in Java,
operating systems and networking. His current research interests are
optical networks and programming.