Академический Документы
Профессиональный Документы
Культура Документы
Compare and contrast control flow in Python, ANSI C 99, and Java 7
INTRODUCTION
Every software system in development, based on its requirements and goals requires the
use of different programming language in order to accomplish its specific task more effectively
and efficiently. Some programming languages are better to use for accomplishing certain goals
and tasks, and others for different tasks. If for example, the software being developed requires
more control over the lower aspects of the system, such as memory management, machine-level
concepts (bytes and addresses), than programming it in C will provide the developer with that level
of detail and control. If on the other hand, the software requires a higher-level approach, than it
would be more effective to use high-level programming languages like Java or Python. Essentially,
that is why we have so many different programming languages, each empowering different aspect
and need of the software system being developed. This report will provide a comparison between
Python, C and Java, focusing on how they implement their control flow.
To accomplish branching, Python uses only one statement, the ‘if’ statement accompanied
with the optional ‘elif’ and ‘else’ statements. According to Python Documentation, the simplest
form of the ‘if’ statement checks a condition, and if the condition is true it executes the following
branch or skips it if it is false. The ‘if’ statement can also be accompanied with the optional ‘elif’
short for ‘else if’ statement which executes a branch if the previous ‘if’ statement was false, and
with the optional ‘else’ statement that executes if all previous ‘if .. elif’ branches are false. Python
does not have ‘case’ and ‘switch’ statements like in Java and C, but the ‘if..elif...elif…’ sequence
is a substitution for the ‘switch’ and ‘case’ statements that are present in Java and C. (Python
Docs).
On the other hand, Java and C provide ‘switch’ and ‘case’ statements for checking different cases
when evaluating a condition for a match. According to King (2008), in C, ‘switch’ statements are
faster than cascaded if statements when there are many cases present, so it is preferable to use the
switch statement in such situations. The only thing the programmer should be aware of is not to
forget to include break statements at the end of each case check body, so the execution can break
after a match is found and the body statement(s) are executed. This is a common place for errors,
when a match is found and the following branch is executed, the condition block exits the body
Java inherits most of its characteristics from C, so the selection statements are almost the
same. One aspect in which they differ is that C supports the keyword ‘goto’ which enables the
control flow to jump to another block of code. This enables for control structures the ability to
have multiple entries, which is not possible in Java. However, it is common to have multiple exits
from a control structure via the ‘return’ and ‘’break’ statements (Sebesta, 2015). Using the ‘goto’
keyword is discouraged in many modern languages, because it produces the so called ‘spaghetti
code’ which is very hard to follow and debug. Python and Java do not support this keyword,
although Java only keeps it reserved but it is not used in the language itself.
Another important aspect to point out is the indentation. For Python, indentation is very
important, and its role is to distinguish which statements belong to what structure body. Contrary,
Java and C do not take into consideration the indentation, but they use the semicolon symbol ; and
curly braces {} to indicate where a statement begins, ends and belongs to. That is why we have to
be very careful in Python to keep the indentation level right so our selection statements are
interpreted properly, and also all other statements inside a structure body.
If we look how the iteration is implemented in Java and C, we will see that it’s almost the
same, except that Java introduces additional iteration structure, the ‘for each’ structure. This
structure gives the ability to iterate over an array, or list of items without explicitly specifying the
number of iterations like in a traditional deterministic for loop, but instead it iterates for every
element it finds in the specified collection, and gives access to each element as it goes over it.
The other loop structures, the traditional for, while, and do-while, are identical for C and Java.
Python on the other hand has a little different approach with ‘for’ loops, although the ‘while’ loop
is identical with that in Java and C. With the ‘for’ loop, instead giving the programmer the ability
to define both the iteration step and halting condition inside the loop declaration like in a traditional
‘for’ loop in C and Java, Python’s ‘for’ statement can iterate over any sequence of items. That
includes lists, strings or any iterable object, in the order the items appear in the sequence. If the
goal is to iterate over a sequence of numbers, Python provides very handy function – the range()
Fig. 5. Iterating over a range of numbers Fig. 6. ‘for’ statement in Python can iterate over any sequence of items.
In order to produce an arithmetic sequence, the range function in Python can take three parameters,
range(a, b, c), where a is the starting inclusive number, b is the ending exclusive number, and c is
the iteration step. The starting number a and the iteration step c are optional parameters. If the
function is executed with only one parameter like in Fig. 5, it will produce an arithmetic sequence
of 0, 1, 2, 3, 4. According to the Python Documentation, it is worth pointing out that the range
function returns an object which is iterable, meaning that it is suitable as a target for functions and
constructs that expect something from which they can obtain successive items until the supply is
exhausted. It is also important to mention that the ‘in’ keyword is used in combination with the
Python also supports the break statement which breaks out of the smallest enclosing ‘for’
or ‘while’ loop like in C and Java. The only difference in that aspect is that Java provides additional
labeled break, which can break out from multiple nested loops at once. The form for this labeled
break is:
This labeled break is not present in C, but the same result can be achieved using a ‘goto’ statement.
Another difference between Python on one side and C and Java on other side, is that Python also
supports an ‘else’ clause after a loop statement. The ‘else’ clause is executed after a list has been
exhausted like in a ‘for’ loop, or the condition has become false like in a ‘while’ loop, but not
when the loop is terminated with a break statement. The following example demonstrates such
When dealing with functions, there is an essential difference of how Python interprets a
function, and how C and Java does. For Python everything is seen as an object, even functions.
This means that functions can be passed as parameters to other functions or returned from within
other functions. This approach of viewing functions as objects is not present in C and Java, and
this makes it to be the primary distinction between them. It is not possible to pass a function as a
parameter in C, nor is it possible to return a function from other function. The same is true for
Java. The way Python manages to see functions as objects is by using a symbol table where it
stores all user defined variables, functions and classes, their scopes and a lot more information
about them. Actually, Python’s interpreter sees all of them as symbolic names bound to objects. It
turns out that everything in a sense is viewed as an object in Python. (Bendersky, 2010). Thus,
In the above code segment, function ‘fib’ is assigned to the name ‘f’. This means, that ‘f’ now is
bound to ‘fib’ which is an object. When ‘f’ is called it produces the same result as what ‘fib’ would
have produced. This can serve as a general function renaming mechanism. Looking at the ‘fib’
function from C and Java perspective it can be argued that it is not a function but a procedure since
it does not return a value. According to Python Documentation, it turns out that all functions in
Python return a value, even if no return statement is present. In such situation the function returns
Python also supports passing a list of parameters to functions using the * and ** specifiers.
We use * for non-keyworded variable length list of parameters, and ** for keyworded variable
Python.
The above code segment will produce the following output: >>> green eggs ham 1 2 3. It is
important to note that the first call that uses only one *, receives the parameters that are passed in
the same order as they are originally defined in the list. But for the second call using two **, the
If we compare how functions return values in Python, C, and Java, we can notice that
Python has the ability to return multiple values at once, whereas Java and C do not allow such
In the above code segment, ‘var1’ will receive expression 1, and ‘var2’ will receive expression 2.
Somewhat similar result can be achieved in C using pointers as parameters. When they are
modified inside a function, the changes will be visible outside the function where they are
originally defined.
When comparing control flow it is important to mention how variables and their scope is
seen inside and outside functions and how it differs between Python, C and Java. In Python when
a variable is declared inside a function, it is not related to any variable with the same name that is
defined outside the function, meaning that variable names are local to the function (A Byte of
Python). If a global variable needs to be accessed locally, Python provides a ‘global’ keyword that
needs to be written before the variable. That way, the global variable will be accessed locally inside
a function or another block. In Java it is not possible to redefine a variable with the same name
When evaluating the scope of a variable and looking for a reference, Python uses a LEGB rule,
which means first it check the local scope, then enclosing scope, then global scope and finally
built-in scope.
Functions arguments in Python are used like aliases (symbolic names) and are passed by
reference. This means you can pass to a function any kind of parameters, and as long as the
operations which are performed inside the function are legal, the function call will be successful.
This approach is not allowed in C and Java, where they explicitly require a type for every
parameter. Python also supports default arguments values. If an argument is not explicitly assigned
argument ‘times’ which is 1, whereas for the second call it will print ‘Word’ 5 times using the
explicitly defined value of 5. This approach of default argument values is not supported in Java
and C.
PROCEDURAL ABSTRACTION
The main purpose of procedural abstraction is to provide support in the top down design
phase of the program in development. In this phase when bigger problems are divided into smaller
problems (tasks), we only care what those smaller tasks need to do, but not how are they going to
implement that functionality. This way, we functionally abstract those tasks, and the programmer
is only concerned of what a function does, and not how it does it. As mentioned earlier, Python
has a lot to offer in this regard, and one of the many features it has, is allowing functions to be
In the above code segment, the name ‘function’ receives an object reference to the inner ‘volume’
function which is returned from the ‘make_cylinder_volume_function’ with the parameter value
10. The inner function ‘volume’ which is now bound to the name ‘function’ remembers its state
including the radius argument of its outer function. At this point we can call ‘function’ with any
height value and the function will execute its calculation with its remembered radius value of 10.
Java and C do not allow functions to be defined inside other functions. The way they provide
procedural abstraction is more in a traditional sense, a function can call other function from within
its body, and by that one function can serve as a wrapper function to another.
SUMMARY
We have seen the main differences how Python, Java and C implement their control flow.
We can conclude that Python as scripting language provide more flexibility when it comes to
function abstractions and iteration structures. Having the ability to iterate over any sequence of
items using a simple ‘for’ and ‘in’ statements is a very convenient and a straightforward way. This
makes Python popular when it comes to developing web applications and other automated web
service components that need such flexibility, working easily with lists and other data structures.
The concept of seeing everything as an object in Python and rebinding objects to one another easily
also makes Python popular in that aspect. On the other hand Java and C are more traditional
desktop based programming languages that seems to give more control when dealing with larger
scale software systems that are going to be shared on various platforms, and therefore need more
More Control Flow Tools - if Statements. (n.d.). Python Documentation. Retrieved from
https://docs.python.org/3.5/tutorial/controlflow.html#if-statements
Bendersky, Eli (2010). Python internals: Symbol tables, part 1. Retrieved from
http://eli.thegreenplace.net/2010/09/18/python-internals-symbol-tables-part-1/
https://python.swaroopch.com/functions.html