Structured Programming
Structured Programming
STRUCTURED PROGRAMMING
Introduction
8.11 Definition and classification of programming languages
It emerged in the 1960s—particularly from work by Böhm and Jacopini, and a famous
letter, Go To Statement Considered Harmful, from Edsger Dijkstra in 1968—and was
bolstered theoretically by the structured program theorem, and practically by the
emergence of languages such as ALGOL with suitably rich control structures.
A language is a medium for communication. The languages we speak are called natural
languages. A programming language is a subset of the set of natural languages. It contains
all the symbols, characters, and usage rules that permit a human being to communicate
with computers. A variety of programming languages have been invented over the years of
computer history. However, every programming language must accept certain types of
written instructions that enable a computer system to perform a number of familiar
operations. In other words, every programming language must have instructions that fall
under the following categories:
• Input/output Instructions: A program needs input data from the external world (or
sometimes given implicitly) with which it performs operations on the input data, and
generates output (compare with algorithm). Input/output instructions. Provide
details on the type of input or output operations to be performed, and the storage
locations to be used during the operations, hence they are provided with purpose.
• Arithmetic Instructions: A program might be required to perform arithmetic
operations on the data in the program. Arithmetic instructions are provided for the
requirement. These perform the arithmetic operations of addition, subtraction,
multiplication, division, etc.
• Logical/Comparison Instructions: They are used to compare two values to check
whether the values satisfy a given condition or state.
• Storage/Retrieval and Movement Instructions: These are used to store, retrieve, and
move data during processing. Data may be copied from one storage location to
another and retrieved as required.
4 Structured Programming
• Control Instructions: These are selection and loop constructs which aid in out-of-
sequence program flow.
Although all programming languages have an instruction set that permits these familiar
operations to be performed, a marked difference is found between the symbols and syntax
used in machine languages, assembly languages, and high-level languages.
Some applications are developed by coding in assembly language – a language closest to the
machine language. This type of application software is most efficient for processing data.
However, since a particular flavor of the assembly language is designed for a particular
architecture, the type of assembly language understood by a computer depends upon the
underlying architecture of the microprocessor used. For example, if a Zilog® microprocessor
is used, then the machine language understood is Z-80. On the other hand, a flavour of the
Intel® assembly language is used for a Pentium® microprocessor which differs from the Z-80.
Thus, assembly languages are architecture-dependent.
Given the inability of assembly language to adapt across different architectural platforms
and given the varied nature of real-world problems, a variety of computer languages, called
high-level languages, were developed, each suited best to a model in a particular class of
problems. High-level language programs are architecture-independent, easier to write, and
provide easier maintenance and readability than their assembly-language counterparts.
However, they require longer execution time compared to assembly-language programs.
Different high-level programming languages were introduced in the 1950s to reduce the
problems that arose in writing code in assembly and machine language. When the first high-
level languages were developed, the longer translation and execution time were considered
as serious limitations of the technique. Nonetheless, the following factors contributed to the
popularity of high-level languages for application development:
• Savings in Programming Time and Training: High-level languages were easier to learn
and understand. Thus, it took less time and effort to write an error-free program or
to make corrections and code-revisions.
• Increased Speed and Capacity of Hardware: The third and fourth generations of
computer hardware brought about a revolution in access time, memory and disk
capacities. This caused the time overhead incurred by the use of high-level languages
to be tolerable.
• Increasing Complexities of Software: With time, software systems grew more
complex. This necessitated use of complex constructs each of which could represent
several lines of assembly code and was provided by high-level languages.
Additional note
•Low Level
•High Level
The languages which use only primitive operations of the computer are known as low
language. In these languages, programs are written by means of the memory and registers
available on the computer. As we all know that the architecture of computer differs from
one machine to another, so far each type of computer there is a separate low level
programming language. In the other words, Programs written in one low level language of
one, architectural can’t be ported on any other machine dependent languages. Examples
are Machine Language and Assembly Language.
Machine Language
In machine language program, the computation is based on binary numbers. All the
instructions including operations, registers, data and memory locations are given in there
binary equivalent.
The machine directly understands this language by virtue of its circuitry design so these
programs are directly executable on the computer without any translations. This makes the
program execution very fast. Machine languages are also known as first generation
languages.
Advantages
Machine language makes most efficient use of computer system resources like storage,
registers, etc. the instruction of a machine language program are directly executable so
there is no need of translators. Machine language instruction can be used to manipulate the
individual bits in a computer system with high execution speed due to direct manipulation
of memory and registers.
Drawbacks
FOR FULLNOTES
Machine languages are machine dependent and, therefore, programs are not portable from
Assembly Language
Assembly language are also known as second generation languages. These languages
substitutes alphabetic or numeric symbols for the binary codes of machine language. That is,
we can use mnemonics for all opcodes, registers and for the memory locations which
provide us with a facility to write reusable code in the form of macros. Has two parts, one is
macro name and the other is macro body which contains the line of instructions. A macro
can be called at any point of the program by its name to use the instruction. A macro can be
called at any point of the program by its name to use the instructions given in the macro
repetitively.
These language require a translator known as “Assembler” for translating the program code
written in assembly language to machine language. Because computer can interpret only
the machine code instruction, once the translation is completed the program can be
executed.
Advantages
Assembly languages provide optimal use of computer resources like registers and memory
because of direct use of these resources within the programs. Assembly language is easier
to use than machine language because there is no need to remember or calculate the binary
equivalents for opcode and registers. An assembler is useful for detecting programming
errors. Assembly language encourages modular programming which provides the facility of
reusable code, using macro.
Drawbacks
Assembly language programs are not directly executable due to the need of translation.
Also, these languages are machine dependent and, therefore, not portable from one
machine to another. Programming in assembly language requires a high level of
programming skills and knowledge of computer architecture of the particular machine.
All high level language are procedure-oriented language and are intended to be machine
independent. Programs are written in statements akin to English language, a great
advantage over mnemonics of assembly languages require languages use mnemonics of
assembly language. That is, the high level languages use natural language like structures.
These languages require translators (compilers and interpreters) for execution. The
7 Structured Programming
programs written in a high level language can be ported on any computer that is why they
are known as machine independent. The early high-level language comes in third generation
of languages, COBOL, BASIC, APL, etc.
These languages enable the programmer to write instruction using English words and
familiar mathematical symbols which makes it easier than technical details of the computer.
It makes the programs more readable too.
Procedures
Procedures are the reusable code which can be called at any point of the program. Each
procedure is defined by a name and set of instructions accomplishing a particular task. The
procedure can be called by its name with the list of required parameters which should pass
to that procedure.
These are the third generation languages. These are procedure-oriented languages and are
machine independent. Programs are written in English like statements. As high level
languages are not directly executable, translators (compilers and interpreters) are used to
convert them in machine language equivalent.
Advantages
Even though each programming language you use is unique, there are certain concepts
common to all languages, including Live Code’s scripting language. Let's look at three of the
most common concepts and structures used in programming.
FOR FULLNOTES
It is important not only to give the right commands or steps—they must also be
given in the correct sequence. We can easily see in some of our mundane
order is essential to our success. We might call such obvious sequences task order,
because the proper sequence is dictated by the nature of the task.
But there are also procedures in which the order of steps is unimportant. Often in
such procedures, a conventional order emerges to avoid confusion. An excellent
example in the U.S. context is addressing letters for mailing. Every school child
knows that you do it in this order:
Oddly enough, this conventional order is exactly the reverse of how the address is
examined at the various postal distribution points in the mail system. The post office
at the point of origin would look at the country and put the letter in a pile for
international mail. The distribution center in the destination country would look at
the zip code and perhaps state. The local post office would examine the street
address and place it in the proper mail carrier's route, and the mail carrier, upon
arriving on that street, would place it into the mailbox bearing that address. Finally,
whoever checked the mail at the house would look at the name and gives the letter
to the person it was addressed to.
In some countries the conventional order follows the logical task order in addressing
envelopes. In Russia, for example, letters are addressed in exactly the opposite order
to the U.S. convention.
Example: You want to clear your screen of all buttons and fields, show a field with
text, and wait for the user to click, then hide the field and show the former ones.
To work correctly, not only do all the commands have to be there, they have to be in
the right order.
9 Structured Programming
II. Conditional structures (Do certain things based on a true or false, yes or no
decision.)
These provide for one outcome or sequence of events to be executed if a statement
is true, and another outcome or sequence of events to be triggered if the statement
is false.
In most programming languages these structures take the form if . . . then . . . else.
The One Potato, Two Potato game uses lots of conditional decisions.
If the counter lands on your fist on the word "more" then you must remove your fist
from the circle.
If both of your fists are knocked out of the circle then you are out of the game.
Computing examples:
Example 1:
If a word exists in a list, then print it out,
Else tell the user that the word does not exist.
Example 2:
If a sentence contains the word "silly" then put that sentence into the silly list.
Else if it doesn't contain the word "silly" then put it into the serious list.
Again, the One Potato game provides an obvious example of a looping structure. The
rhyme is repeated and fists counted for as many times as needed until just one
person is left.
Another Example:
Given a list of party guests, assign everyone to one of three groups for "ice-breaker"
games.
FOR FULLNOTES
Programming Strategies
Programming can range in complexity from solving small problems—like setting an alarm
time on your watch or cell phone—to very sophisticated instructional or business
applications. For more complex tasks, you can use these strategies to help you think through
the logic of your program before starting to write code.
Top-down design
Top-down design is a way of approaching a complex programming task by first mapping out
the entire program and identifying the major components that it will require. Then the
programmer would use flowcharts and general statements to represent the logical flow of
your program. Once the major components are identified, the programmer then focuses on
each component in greater detail, finally culminating in writing the actual program code for
creating each component.
In the example at right, we have represented the “program” for playing One Potato, Two
Potato using a top-down approach. Each shape in the flowchart represents a major step in
the game. Combinations of shapes and arrows show conditional “if-then” decision points, as
well as looping structures in which segments of the program are repeated, perhaps with
slight variations in each iteration.
11 Structured Programming
Pseudo code
This term, from the prefix pseudo-, 'false' and the root word code, 'programming
instructions', describes a way of representing the detailed steps your program must perform
without having to worry about the specific vocabulary or syntax of a specific programming
language. You use your knowledge of the basic control structures, common sense and logic
to write plain-English statements to explain in detail how you will accomplish each main
step. All of the examples shown here and used in class could be considered forms of pseudo
code.
Here is an example of pseudo code that describes our One Potato, Two Potato “program”.
Notice how it attempts to detail all of the steps, conditional statements and looping
segments using simple statements, indents and minimal punctuation brackets.
Possibly the simplest and easiest method to understand the steps in an algorithm, is by
using the flowchart method. This algorithm is composed of block symbols to represent each
step in the solution process as well as the directed paths of each step.
Find the average of a given set of numbers.
Solution Steps
1. Understanding the problem:
(i) Write down some numbers on paper and find the average manually, noting each step
carefully .e.g. given a list say: 5, 3, 25, 0, 9
(ii) Count numbers | i.e. How many? 5
(iii) Add them up | i.e. 5 + 3 + 25 + 0 + 9 = 42
(iv) Divide result by numbers counted | i.e. 42/5 = 8.4
2. Devising a plan:
Make note of not what you did in steps (i) through (iv), but how you did it. In doing so, you
will begin to develop the algorithm.
How do we count the numbers?
Starting at 0 i.e. set COUNTER to 0
Look at 1st number, add 1 to COUNTER
Look at 2nd number; add 1 to COUNTER and so on, until you reach the end of the list
How do we add numbers?
Let SUM be the sum of numbers in list.
Set SUM to 0.
Look at 1st number, add number to SUM
Look at 2nd number, add number to SUM and so on, until we reach end of list
How do we compute the average?
Let AVG be the average then AVG= total sum of items i.e. SUM number of items COUNTER
3. Identifying patterns, repetitions and familiar tasks.
Familiarity: Unknown number of items? i.e. n item Patterns :look at each number in the list
Repetitions: Look at a number Add number to sum Add 1 to counter
4. Carrying out the plan. Check each step Consider special cases, Check result, Check
boundary conditions: i.e. what if the list is empty?
Division by 0?
Are all numbers within the specified range?
In this example, no range is specified
-No special cases.
Check result by tracing the algorithm with a list of numbers e.g. 7, 12, 1, 5, 13.
If list is empty, we do not want to compute average.
A Systematic Approach to Defining a Problem
Defining the problem is the first step towards a problem solution. A systematic approach to
problem definition, leads to a good understanding of the problem. Here is a tried and tested
FOR FULLNOTES
method for defining (or specifying) any given problem:
Divide the problem into three (3) separate components:
CALL/TEXT:0724 962 477
14 Structured Programming
Purpose
This discusses the basic principles that underlie structured program design and functional
decomposition. The objective of functional decomposition is to design structured programs
that are easy to test, debug, and maintain. The basic idea is to break down (or decompose) a
program into logically independent modules based on the processes or tasks they perform.
Because the detailed computational logic is grouped into independent, single function
modules, well-structured programs are easier to test, debug, and maintain than are
unstructured programs. Independent modules can be independently coded and tested. The
control structure allows the entire program to be tested top down, one module at a time.
When an error occurs, it is often possible to quickly isolate the likely cause to a single
module. During the maintenance phase, independent modules can be replaced or modified
with minimal ripple effects.
Before starting to design a program using functional decomposition, the necessary logical
data structures and the primary processes must be defined during the systems analysis
FOR FULLNOTES
phase (Part IV) of the system development life cycle. Often, the results of analysis are
documented in the requirements specification .Additionally; it is useful to define the
physical data structures, file structures, and database structures. The required input and
output data structures, and the algorithms before starting program design.
The design of a structured program is sometimes documented using the HIPO technique.
Structure charts are useful planning tools. Such tools as logic flowcharts Nassi- Shneiderman
charts, decision trees, decision tables, pseudo code, structured English, and IPO charts are
used to document the modules.
4 Concepts
A module with no children (a lowest-level module) is called a leaf and often implements a
single algorithm. Library modules (e.g., a standard subroutine) are indicated by a rectangle
marked with two vertical lines; see the leaf labeled Library module in Figure1. Note that a
library module can be called by more than one parent.
The modules are often assigned identifying numbers or codes that indicate their relative
positions in the hierarchy. For example, the root might be designated module 1.1, the level-
2 modules might be designated 2.1, 2.2, 2.3, and so on. Other designers use letters (or even
Roman numerals) to designate levels; for example, module A.1 is the root, module B.3 is the
third module at level 2, module C.6 is the sixth module at level 3, and so on. Sometimes,
more complex numbering schemes are used to indicate a path through the hierarchy. The
key is consistency.
The first step in decomposing a program is to define its high-level control structure. The
primary inputs come from the logical models developed during the systems analysis phase
(Part IV) and from the requirements specification. More specifically, the high-level functions
to be performed by a given program can often be obtained from the appropriate
configuration item’s process description.
One approach to designing a control structure is to divide the functions (or sub processes)
into three groups (Figure 2). The afferent processes gather and prepare input data. The
efferent processes structure and transmit output data. In the middle, the transform
processes convert the input data to output form. Identifying the afferent, transform, and
efferent processes suggests a basic input, process, output control structure.
FOR FULLNOTES
CALL/TEXT:0724 962 477
18 Structured Programming
An alternative is to start with the program’s trigger event, the event that activates the
program or causes it to change from a wait to a run state. Some programs are triggered by
an asynchronous event such as the arrival of a transaction or an interrupt. Other
applications are clock driven; for example, a batch program might be run at the same time
every week, and a scientific data collection routine might take a sample every few seconds.
A program’s high-level control structure should reference those tasks that are performed in
direct response to its trigger event.
19 Structured Programming
Another technique for designing a high-level control structure is to analyze the data
structures. The point of any program is to accept the input data and convert them to the
form required for output, so the data actually drive the program design process.
Analyze the output data and determine the order in which the various output substructures
are assembled. Then define the input substructures and the algorithms that generate the
data elements in a given output substructure. The data structures will, essentially, dictate
both the order in which the logical tasks must be performed and the logical structures
needed to support those tasks. Generally, sequential data structures call for sequential
logic, conditional structures call for conditional logic, and repetitive structures call for
repetitive logic.
Logical access maps were initially proposed by Martin to help the designer determine the
logical execution sequences or access paths through a program. This technique recognizes
that each user (or class of users) has a unique logical accessing perspective. For example, a
sales associate might start with a customer order and follow the order through order
fulfillment, shipping, and so on. In contrast, warehouse personnel might start with the
arrival of a shipment from a supplier, track the shipment into inventory, and view a
customer order as nothing more than a transaction that deletes individual items from
inventory. The point of logical access mapping is to examine the logical accessing sequence
of a system’s programs and a program’s modules from multiple perspectives and to use the
consensus view as a design criterion.
4.3.1 Morphology
One way to evaluate a control structure’s design is to examine its morphology (form or
structure). Each module decomposes into several lower-level routines, so the number of
modules should increase from level-2 to level-3, perhaps increase again at level-4, and so
on. Eventually, however, only some modules require additional decomposition, so the
number of routines at each level begins to stabilize and then to decline.
good control structures have a similar shape, with the number of modules at each level
increasing, then stabilizing, and then decreasing.
Morphology is subjective; over the years, people have noticed that good designs tend to
have that characteristic shape. The designer should not consciously try to make the control
structure resemble a mosque. Instead, morphology should be checked after the design is
complete. If the shape seems reasonable, the design is probably a good one. If the design
deviates significantly from the expected shape, it should be restudied.
A well-designed control structure balances two conflicting objectives: depth and breadth.
Depth is the number of levels in the control structure. Because each call to a lower level is a
potential source of error, shallow structures tend to be better than deep structures.
Breadth, or span-of-control, is a measure of the number of modules directly controlled by a
higher-level routine. Too many subordinates add to complexity, so narrow structures tend
to be better than broad structures.
Narrow structures are usually deep, so reducing breadth tends to increase depth, and vice
versa. One rule-of-thumb for balancing these two parameters suggests that no module
should directly control more than seven subordinates. If a given routine has too many
subordinate modules, adding a secondary control structure drops some of them to a lower
level.
Module size is another useful screen; one page of source code is a common limit. If a
module’s logic exceeds roughly 50 lines of code, decompose it. If, on the other hand, the
logic in a subordinate routine can be merged into its parent without violating the single
page rule, merge it. Remember, however, that rules-of- thumb are not absolute. If breaking
FOR FULLNOTES
one means a better design, break it.
Structure clash occurs when corresponding elements of two related data structures are
incompatible. For example, imagine that customer social security number is the key for an
invoice file and customer zip code is the key for a name and address file. Because the files
are (presumably) stored in different record sequences and accessed by different keys, it is
difficult to design a program to efficiently merge them.
A boundary clash occurs when the physical data structures are incompatible. For example, if
a program sets a nonstandard (9 × 12) page size and the printer is loaded with standard (8.5
× 11) paper, the resulting boundary clash produces poorly aligned output.
Structure clashes and boundary clashes lead to errors and inefficiencies. The program
designer should carefully evaluate both the logical and physical data structures and change
the program design, the data structures, or both to eliminate structure clash and border
clash.
4.4.1 Cohesion
The best form of cohesion is called functional cohesion. A functionally cohesive module
performs a single logical function, receives and returns no surplus data, and performs only
essential logical operations. Functional cohesion is the designer’s objective. A module is not
considered function-ally cohesive if it exhibits other forms of cohesion.
Coincidental cohesion is the weakest type. In a coincidentally cohesive module, there is little
or no logical justification for grouping the operations; the instructions are related almost by
chance. In a logically cohesive module, all the elements are related to the same logical
function; for example, all input operations or all data verification operations might be
grouped to form a module. The elements that form a temporally cohesive module are
related by time; for example, a setup module might hold all operations that must be
performed at setup time.
4.4.2 Coupling
With data coupling (or input-output coupling), only data move between the modules. Data
coupling is necessary if the modules are to communicate. Control coupling involves passing
control information (e.g., a switch setting) between the modules. Hybrid coupling is a
combination of data coupling and control coupling. For example, if module A modifies an
instruction in module B, the operation looks like data coupling to module A and control
coupling to module B. Whenever possible, control and hybrid coupling should be eliminated.
With common-environment coupling, two or more modules interact with a common data
environment, such as a shared communication region or a shared file. With content
coupling, some or all of the contents of one module are included in the other. This problem
often occurs when a module is given multiple entry points. Both common-environment and
content coupling can lead to severe ripple effects, and should be avoided.
Binding time, the time at which a module’s values and identifiers are fixed, is another factor
that influences coupling. A module can be fixed (rendered unchangeable) at coding time, at
compilation time, at load time, or at execution time. Generally, the later the binding time
the better the module.
The modules that form a well-structured program are composed of three basic logical
building blocks or constructs: sequence, selection (or decision), and repetition (or iteration).
Go to or branch instructions are not permitted.
Sequence (Figure 5) implies that the logic is executed in simple sequence, one block after
another. Note that each block might represent one or more actual instructions.
Selection (or decision) logic provides alternate paths through the block depending on a run-
time condition. With IF-THEN-ELSE logic (Figure 6), if the condition is true the logic
associated with the THEN branch is executed and the ELSE block is skipped. If the condition
is false the ELSE logic is executed and the THEN logic is skipped. A case structure (Figure 7)
provides more than two logical paths through the block of logic based (usually) on the value
of a control variable.
FOR FULLNOTES
CALL/TEXT:0724 962 477
24 Structured Programming
There are two basic patterns for showing repetitive logic: DO WHILE and DO UNTIL
(Figure 8). In a DO WHILE block, the test is performed first and the associated instructions
are performed only if (while) the test condition is true. In a DO UNTIL block, the associated
instructions are executed first and then the exit condition is tested.
25 Structured Programming
Figure 8 Repetition.
5 Key terms
Afferent process — A process that gathers and prepares input data.
Binding time — The time at which a module’s values and identifiers are fixed; for example,
coding time, compilation time, load time, or execution time.
Breadth (span-of-control) — A measure of the number of modules directly controlled by a
higher-level routine.
Case structure — A selection structure with multiple alternative paths; the path through the
structure is normally based on the value of a control variable.
Child (son) — An immediate lower-level module in a control structure. Control passes from
the parent to the child and then returns to the parent.
Cohesion — A measure of a module’s completeness.
Coincidental cohesion — The weakest type of cohesion in which the elements are related
almost by chance.
Common-environment coupling — A form of coupling in which two or more modules
interact with a common data environment, such as a shared communication region or a
shared file.
Communicational cohesion — A form of cohesion that groups elements that operate on the
same set of input or output data or on the same data structure.
Configuration item — A functional primitive; above the configuration level are the system’s
logical, composite elements. Below the configuration level are the system’s physical
FOR FULLNOTES
components, including the programs.
Content coupling — A form of coupling in which some or all of the contents of one module
are included in the other.
Control coupling — A form of coupling in which control information (e.g., a switch setting) is
passed between the modules.
Control structure — A hierarchical model of the flow of control through a program. The
control structure resembles a military chain of command or an organization chart. At the
top is a main control module that calls secondary control structures. At the bottom are the
computational routines, each of which implements a single algorithm.
Coupling — A measure of a module’s independence; fewer parameters flowing into or out
from a module imply looser coupling.
Data coupling (input-output coupling) — A form of coupling in which only data move
between the modules.
Depth — The number of levels in the control structure.
Efferent process — A process that structures and/or transmits output data.
Function cohesion — The strongest type of cohesion in which a given module performs a
single logical function, receives and returns no surplus data, and performs only essential
logical operations.
Functional decomposition — A program design methodology in which the program is
broken down (or decomposed) into modules based on the processes or tasks they perform.
Hybrid coupling — A combination of data coupling and control coupling.
Leaf — A module in a control structure with no lower-level (child) modules.
Logical access map — A program design tool used to help the designer determine the logical
execution sequences or access paths through a program.
Logical cohesion A form of cohesion in which all the elements are related to the same
logical function.
Morphology — Form or structure.
Procedural cohesion — A type of cohesion in which all the elements in a module are
associated with the same procedural unit, such as a loop or a decision structure.
Repetition (iteration) — A block of logic that is executed repetitively as long as (while) an
initial condition holds or until a terminal condition occurs.
Root — The module at the top of a control structure from which all control flows.
Selection (decision) — A block of logic that provides alternate paths through the block
depending on a run-time condition.
Sequence — A block of logic in which the instructions are executed in simple sequence, one
after another.
Sequential cohesion — A form of cohesion in which the modules form a chain of
transformations, with the output from one module serving as input to the next.
Span-of-control (breadth) — A measure of the number of modules directly controlled by a
higher-level routine.
Temporal cohesion — A type of cohesion in which the elements are related by time.
Transform process — A process that converts the input data to output form.
Trigger event — the event that activates a program or causes it to change from a wait state
to a run state.
27 Structured Programming
(real numbers)
float 32-bit IEEE 754 Single-precision floating point
double 64-bit IEEE 754 Double-precision floating point
(other types)
Char 16-bit Unicode character A single character
Boolean true or false a Boolean value (true or false)
Reference types are called such because the value of a reference variable is a reference (a
pointer in other terminology) to the actual value or set of values represented by the
variable. For example, the character-counting program declares (but never uses) one
variable of reference type, args, which is declared to be an array of String objects. When
used in a statement or expression, the name args evaluates to the address of the memory
location where the array lives. This is in contrast to the name of a primitive variable, the
count variable, which evaluates to the variable's actual value.
FOR FULLNOTES
Besides arrays, classes and interfaces are also reference types. Thus when you create a class
or interface you are in essence defining a new data type. See Objects, Classes, and
Note to C and C++ Programmers: There are three C Data Types Not Supported By the Java
Language. They are pointer, struct, and union. These data types are not necessary in Java;
you use classes and objects instead.
Variable Names
A program refers to a variable's value by its name. For example, when the character-
counting program wants to refer to the value of the count variable, it simply uses the name
count. By convention, variable names begin with a lower case letter (class names begin with
a capital letter).
Scope
A variable's scope is the block of code within which the variable is accessible. Also, a
variable's scope determines when the variable is created and destroyed. You establish the
scope of a variable when you declare it. Scope places a variable into one of these four
categories:
• member variable
• local variable
• method parameter
• exception handler parameter
A member variable is a member of a class or an object and is declared within a class (but not
within any of the class's methods). The character-counting program declares no member
variables.
Local variables are declared within a method or within a block of code in a method. In the
character-counting example, count is a local variable. The scope of count, that is, the code
that can access count, extends from the declaration of count to the end of the main()
method (indicated by the first right curly bracket ('}') that appears in the sample code). In
29 Structured Programming
general, a local variable is accessible from its declaration to the end of the code block in
which it was declared.
Method parameters are formal arguments to methods and constructors and are used to
pass values into methods and constructors. The discussion about writing methods on
the Implementing Methods page in the next lesson talks about passing values into methods
and constructors through method parameters. In the character-counting example, args is a
method parameter to the main() method. The scope of a method parameter is the entire
method or constructor for which it is a parameter. So, in the example, the scope of args is
the entire main method.
Exception handler parameters are similar to method parameters but are arguments to an
exception handler rather than to a method or a constructor. The character-counting
example does not have any exception handlers, so it doesn't have any exception handler
parameters. Handling Errors using Exceptions talks about using Java exceptions to handle
errors and shows you how to write an exception handler with its parameter.
Variable Initialization
Local variables and member variables can be initialized when they are declared. The
character-counting program provides an initial value for count when declaring it: int count =
0;
The value assigned to the variable must match the variable's type.
Method parameters and exception handler parameters cannot be initialized in this way. The
value for a parameter is set by the caller
Additional notes
At the core of any program are variables. Variables are where the dynamic information is
stored. When you type your name into a web form and send it, your name is a variable.
Not all variables are the same though. In fact, there are many different types of variables
that nearly every programming language has. Let’s look at a small selection of them, as well
as their short names if they have one:
Character (char): This is a single character, like X, £, 4, or *. You don’t often create single
character variables, but they are at the core of the language so you need to know what they
are String: This is a “string” of characters (see how they’re at the core?) of any length. In my
previous example – your name on web form – your name would be stored as a String
variable.
Integer (int): A whole number – whole meaning there are no digits after a decimal point. So
65 would be a valid integer; 65.78 would not.
Floating-point number (float): A number that may have digits after the decimal place. 65.00
is technically a floating point number, even though it could be represented just as easily as
an integer as 65. It takes more memory to store a float, which is why there is a distinction
FOR FULLNOTES
instead of just creating a “number” data type.
Boolean (bool): A variable to represent true or false (or it could also mean 0 or 1, on or off).
The simplest data type and commonly used – get used to this one!
Array: These are essentially lists of other variables. There are a variety of array types
depending on the language, but basically they’re just a collection of variables in a sequential
list. For example: 1, 2,3,4,5 might be stored as an array (of length 5) containing integer
variables. Each variable in the array can then be accessed using an index – but you should
know the first item in the list has an index of 0 (yes, that can be confusing sometimes). By
storing them as an array, we make it easy to send a collection of variables around the
program and do things with them as a whole – such as counting how many things are in the
array or doing the same thing to each item (which is called an iteration, and we’ll get to that
another time). You should also know that a string is actually just an array of characters.
Moving on, programming languages can be divided into those that are strongly-typed, and
those that are weakly-typed. A strongly typed language (such as Java) requires that you
explicitly declare what type of variable you are creating, and they get very upset if you start
trying to do things with them that you shouldn’t. For example, a strongly typed language
would give you errors if you tried to add an integer and a string together. “How on earth am
I supposed to mathematically add together a word and a number?” it would cry – even
though you as a human clearly understand a string “5” is semantically the same as an
integer with the value of 5.
A weakly typed language on the other hand would just say “whatever”, and give it a shot
without complaint – but the answer could go either way. Perhaps “5+5” = 10, perhaps it’s
“55” – who knows! It might seem at first like weakly-typed languages are easier to write, but
they can often result in curious errors and unexpected behavior that take you a while to
figure out.
Nothing to do with socialism…Instead, it’s a concept that catches out many programming
newbies so I wanted to address it now. There is a difference between assigning and testing
for equality. Consider the following, both of which you would probably read as “A is equal
to 5”:
A = 5;
A == 5;
Can you tell the difference? The first is known as assignment. It means assign the value of 5
to variable A. You are “setting” the variable value. The second statement is one of equality.
It’s a test – so it actually means “is A equal to 5?” – The answer given back to you would be a
Boolean value, true or false. You’ll see how this can mess up your programs in later lessons.
THIS IS A FREE SAMPLE OF THE ACTUAL NOTES
FOR FULLNOTES
CALL/TEXT:0724 962 477