Translate

Search This Blog

Total Pageviews

Thursday, October 18, 2018

Basic Programming Concepts: Program Structure


A computer program is a set of statements that is used to create an output, such as a screen display, a printed report, a set of data records, or a calculated set of numbers.
The statements in the programs may be statements that are executed in sequence.
A program is written using the statements of a programming language.

Individual statements perform simple operations such as printing an item of text, calculating a single value, and comparing values to determine which set of statements to execute.

Simple instructions are performed in hardware by the computer’s central processing unit(CPU).
Complex instructions are written in programming languages and translated into the machine's internal instruction set by another program(assembler, compiler, interpreter).

Computer memory is generally composed of bytes, which are data items that contain a binary number. These values can range from 0 to 255 (base 2 raised to 8 --the latter is the number of bits that compose a byte--).
Memory locations are referred to by number, known as an address.
A memory location can be used to record information such as a small number, data from a graphics image, part of a memory address, a program instruction, a numeric value representing a single letter(e.g. ASCII or lately Unicode).
Program instructions and data are stored in memory while a program is executing.

                Procedural Languages

Programs written in procedural languages involve a set of statements that are performed in sequence.
Third generation languages are languages that operate at the level of individual data items, “if” statements, loops and subroutines.
A large proportion of actual programs are written using third generation languages.


Data

Data Types

Basic data types include numeric values and strings.

A string is a short text item, and may contain information such as a name or a report heading.

Numeric data may be stored internally as a binary number, which is a distinct format from a set of individual digits stored in a text format.
Several numeric data types may be available.

These may include integer data types, floating point data types and other formats.
Integers are whole numbers and integer data types cannot record fractional numbers.
However, operations with integer data types are generally faster than operations with other numeric data types.
Floating point data types store the digits within a number separately from the magnitude, and can store widely varying values from smallest to huge.
Some languages also support a range of other numeric data types with varying range and precision.

Dates are supported as a separate date type in some languages.

A Boolean data type is a type that records only two values, “true” and “false”.
Boolean data types and boolean expressions are used in checking conditions and performing different actions in different circumstances.

The language COBOL is used in data processing. Data items within COBOL are effectively fields within database  records, and may contain a combination of text and numeric digits.
Individual positions within a data field in COBOL can be defined as holding an alphabetic, alphanumeric or numeric character. Calculations can be performed with numeric fields.

Data Type Conversion

Languages generally provide facilities for converting between data types, such as between two different numeric data types, or between numeric data in binary format and a text string of digits.
This may be done automatically within expressions, through the use of an operator symbol, or through a subroutine call.

When different numeric data types are mixed within an  expression, the value with the lower level of precision is generally promoted to the higher level of precision before the calculation is performed.
The details of type promotion vary with each language.

Variables

A variable is a data item used within a program, and identified by a variable name(identifier).
Variables may consist of fundamental data types such as strings and numeric data types, or a variable name may refer to multiple individual data items(e.g. array variable).

Variables can be used in expressions for calculations, and also for comparisons to perform different sections of code under different conditions.
The value of a variable can be changed using an assignment statement, which changes the value of a variable(on the left of assignment) to equal the value of an expression(on the right of assignment).

Constants

Constants such as fixed numbers and strings can be included directly within program code.
Constants can also be given a name, similar to a variable name, and used in several places with the program.
The value of a constant is fixed and cannot be changed without recompiling the program.

Data Structures

Variables can be defined as a collection of individual data items.
A structure type, also known as a record(or structure), is a collection of several different data items.
An array is a variable that contains multiple data items of the same type. Each item is referred to by number.Arrays can contain structures, and structures can contain arrays and other structures.
An object is an element of object oriented programs. An object is referred to by name and contains individual data items. Subroutines known as methods are also defined within an object.
Some languages support other data structures such as lists.


Pointers & References

A pointer is a variable that contains a reference to another variable. The second variable can be accessed indirectly by referring to the pointer variable.
Pointers are used to link data items together, when data structures are dynamically created as a program executes.
In some languages, pointers can be increased and decreased(pointer arithmetic) to scan through memory
and access different elements within an array, or individual bytes within a block of data.
A reference to a variable is also known as an address, and refers to the location of the variable in memory.
The value of a pointer variable can be set to the address of another data item by using a reference operator with the data item.
The data item that a pointer points to can be accessed by using a de-referencing operator.


Variable Scope

Individual variables can only be accessed within certain sections of a program.
 An independent copy of the local variables is created each time that a subroutine is called.
Where a local variable has the same name as a global variable, the name would refer to the variable with the tightest scope, which in that case would be the local variable(is said the local variable hides the global variable).

Parameters are data values or variables that are passed to a subroutine when it is called.
Parameters can be accessed from within the subroutine.
Some languages have multiple levels of scope. In these cases, subroutines may be defined within other subroutines, and variables may be defined within inner code blocks.
Variables within the current level of scope and outer levels of scope can be accessed, but not variables within an inner level of scope or in an independent part of the system.
Modules and objects may have public and private subroutines and variables.
Public variables are accessible outside the module, while private variables are only accessible within the module.
The use of global variables can lead to interactions between different parts of the code, which may make debugging and modifying the code more difficult.

Variable Lifetime

Global variables exist for the period of time that the program is running.
Local variables are created when a subroutine is called, and expire when the subroutine terminates.
Static variables may have a scope that applies within a single subroutine, however they have a lifetime that exists for the full period that the program is executing, and they retain their value from one call to the subroutine to the next.
Dynamically created data items exist until they are freed. Dynamic memory allocation involves creating data items while a program is running.
This may be done explicitly, or it may occur automatically when the last remaining variable that points to the item is assigned a different value, or expires as its level of scope terminates.


 Execution

Expressions

An expression is a combination of constants, variables and operators that is used to calculate a value.
An assignment operation involves a variable name and an expression. The expression is evaluated, and the value of the variable is changed to equal the result of the expression.
Expressions are also used within control flow statements such as “if” statements and loops.

Numeric expressions include the standard arithmetic operations of addition, subtraction, multiplication and division and exponentiation.

The basic string operations are concatenating two strings to form a single string, extracting a substring, and comparing strings.

String expressions may include constant strings(string literals), string variables, and operators such as a
concatenation operator.

Boolean variables and expressions have only two possible values, “true” and “false”.
An expression containing a relational operator, such as “<=”, is a Boolean expression. e.g. “5 < 3” has the value “false”.
The Boolean operators “and”, “or” and “not” can also be used in expressions.
An “and” expression has the value “true” when both parts are true, an “or” expression has the value “true” when either value is true, and a “not” expression reverses the value.
Boolean expressions are used within “if” statements to execute code under certain conditions and within loops to repeat a series of statements while a condition remains
true.


Statements

Assignment Statements

An assignment statement contains a variable name, an assignment symbol such as an “=” sign, and an expression.
The expression is evaluated, and the value of the variable is set to equal the result of the expression.
Some languages are expression- focused rather than statement-focused. In these languages, an assignment operation may itself be an expression, and may be used within other expressions.


Control Flow

If Statements

An “if” statement contains a Boolean expression and an associated block of code.
The expression is evaluated, and if the result is true then the statements within the block are executed, otherwise they are skipped.
An “if” statement may also have a block of code attached to an “else ” section. If the expression is false, then the code within the “else” section is executed, otherwise it is
skipped.


Loops

A loop statement may contain a Boolean expression. The expression is evaluated, and if it is true then the code within the block is executed. The control flow then returns to the beginning of the loop, and the cycle repeats the loop each time that the condition evaluates to true.

Other loop statements may also be available, such as statements that specify a fixed number of iterations(for), or statements that loop through all items in a language data structure(enhanced for).
 

Goto

Some languages support a “goto” statement. A goto statement causes a jump to a different point in the program to continue execution.
Code that uses goto statements can develop very complex control flow and may be very difficult to debug and modify.
Some languages also support structured goto operations, such as a statement that terminates the current loop mid-way through the loop code(break).
These operations do not complicate the control flow to the same extent as general goto statements, however these operations can be easily missed when code is being read.

For example, a statement in an early part of a complex loop may result in the loop being exited when it is executed.
This statement complicates the control flow and may make interpreting the loop code more difficult.

Exceptions

In some languages, exception handling subroutines and sections of code can be defined.
These code sections are automatically executed when an error occurs.


Subroutine Calls

Including the name of a subroutine within a statement causes the subroutine to be called. The subroutine name may be part of an expression, or it may be an individual
statement.

When the subroutine is called, program execution jumps to the beginning of the subroutine and execution continues at that point. When the code in the subroutine has been executed, or a termination statement(return) is performed, the subroutine terminates and execution returns to the next statement following the original subroutine call.


Subroutines

Subroutines are independent blocks of code that are referred to by name.
Programs are composed of a collection of subroutines.
When execution reaches a subroutine call the program execution jumps to the beginning of the subroutine.
Control flow returns to the point following the subroutine call when the subroutine terminates.

Subroutines may include parameters. These are variables that can be accessed within the subroutine. The value of the parameters is set by the calling code when the subroutine call is called.
Calling code can pass constant data values or variables as the parameters to a subroutine call.
Parameters are passed in various ways.
  • “Call-by-value” passes the value of the data to the subroutine. 
  • “Call-by-reference” passes a reference to the variable in the calling routine, and the subroutine can alter the value of a parameter variable within the calling routine.
Call by value leads to fewer unexpected effects in the calling routine, however returning more than one value from a subroutine may be difficult.
Subroutines may also contain local variables. These variables are accessible only within the subroutine, and are created each time that the subroutine is called.
In some languages, subroutines can also call themselves. This is known as recursion and does not erase the previous call to the subroutine. 
A new set of local variables is created, and further calls can be made.
This process is used for functions that involve branching to several points at each stage in a process. As each subroutine call terminates, execution returns to the previous level.


Comments
Comments are included within program code for the benefit of a human reader.
Comments are identified as separate text items, and are ignored when the program is compiled.
Comments are used to include additional information within the code that is relevant to a particular calculation or process, and to describe details of the function within a
complex section of code.


                    Declarative Languages
A declarative program defines structures and patterns, and may contain a set of information and facts.
In contrast, procedural code specifies a set of operations that are executed in sequence.
Declarative code is not executed directly, but is used as input to other processes.
For example, a declarative program may define a set of patterns, which is used by a parser to identify patterns and sub-patterns within a set of input data.
Other declarative systems use a set of facts to solve a problem that is presented.
Declarative languages are also used to define sets of items, such as records within data queries.

Declarative programs are very powerful in the operations that can be performed, in comparison to the size and complexity of the code.
For example, all possible programs can be compiled using a definition of the language grammar.
Also, a problem solving engine can solve all problems that fall within the scope of the information that has been provided.
Facts may include basic data, and may also specify that two things are equivalent.
For example:
x+ y= z* 2
Month30Days = April OR June OR September OR November

FieldName = 342-???-453

expression: number “+” expression
The first example is a mathematical statement that two expressions are equivalent,
the second example specifies that “Month30Days” is equal to a set of four months,
the third example matches the set of field names beginning with 342 and ending with 453,
and the fourth example specifies a pattern in a language grammar.

Patterns may be recursively defined, such as specifying that brackets within an expression may contain an entire expression, with potentially infinite levels of sub-expressions.

Declarative code may involve patterns, which have a fixed structure, and sets, which are unordered collections of items.


Code Structure

Declarative code may contain keywords, names, constants, operators and statements.

Keywords are language keywords that may be used to separate sections of the program and identify the type of information that is recorded.

The names may identify patterns, while the operators may be used to create a new pattern from other patterns.

Statements may be entered in the form of specifying that two expressions are equivalent.

The chain of connections is defined by the appearance of names within different statements. There is no order within a statement or from one statement to the next.


                           Other Languages

Programming languages appear in a wide variety of forms and structures.
In the language LISP, for example, all processing is performed with lists, and a LISP program consists of multiple brackets within brackets defining lists of data and
instructions.

Resources

The black art of programming by Mark McIlroy

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.