0% found this document useful (0 votes)
8 views13 pages

Program Construction and Structured Programming

The document describes the steps to build programs in a structured way, including analyzing the problem, specifying the solution, designing the solution, implementing the design, testing the program, and maintaining the program. It also describes the advantages of structured programming, such as facilitating understanding, debugging, and maintenance of programs. Finally, it explains the three basic structures of control flow: sequence, selection, and iteration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Program Construction and Structured Programming

The document describes the steps to build programs in a structured way, including analyzing the problem, specifying the solution, designing the solution, implementing the design, testing the program, and maintaining the program. It also describes the advantages of structured programming, such as facilitating understanding, debugging, and maintenance of programs. Finally, it explains the three basic structures of control flow: sequence, selection, and iteration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program construction:

Software development is a process through which, given a problem, a program is found (or
a set of programs) that solves it efficiently.

Currently, it is widely accepted that the process of building programs must


divide into at least two stages: the problem specification stage and the stage of
programming or software development.

The result of the first stage is a formal specification of the problem, which will remain
abstract (not detailed) but it will be written accurately in some language whose semantics is
rigorously defined. The second stage will result in a program and a demonstration of
that the program is correct with respect to the given specification. The separation into two stages,
it allows us to discern now whether a program whose results are not as expected is incorrect or if, in
The change is that the specification does not describe the program in the appropriate way.

There are many methodologies for building programs, but here we will apply one methodology.
simple, which is suitable for building small programs, and can be summarized in
the following steps:

1. Analyze the problem. Understand deeply what the problem is that is being addressed.
including the context in which it will be used.

Once the problem has been analyzed, document the analysis in writing.

2. Specify the solution. This is the point at which it is described what the program must do, without
import how. In the case of the simple problems we will address, we must decide which
they are the input data that is provided to us, what are the outputs we must produce, and what is
the relationship between all of them.

When specifying the problem to be solved, document the specification in writing.

3. Design the solution. This is the point at which we tackle how we are going to solve the problem.
what are the algorithms and data structures that we will use. We analyze possible variants, and
We make decisions using the context in which it will be applied as the data of reality.
solution, and the costs associated with each design.

After designing the solution, set the design down in writing, ensuring that it is complete.

4. Implement the design. Translate into a programming language (in our case, and for the
moment, Python) the design we chose in the previous point.

The implementation should also be documented, with comments inside and outside the code, at
regarding what the program does, how it does it, and why it does it that way.

5. Test the program. Design a set of tests to test each of its parts.
separated, and also the correct integration between them. Use the debugger as a tool to
discover where certain errors occur.

When conducting the tests, document the results obtained.


6. Maintain the program. Make changes in response to new demands.

When changes are made, it is necessary to document the analysis, the specification, the design,
implementation and the tests that arise to carry out these changes.

PROGRAM CODING:

Coding, also known as software programming, consists essentially of


transform into source code, in the chosen programming language, the requirements and the
Proposed Functional Design.

In the lifecycle of a program, once the algorithms of an application have been


designed, the coding phase can now begin. In this stage, it is necessary to translate these
algorithms to a specific programming language; that is, the actions defined in the
algorithms must be converted into instructions.

Structured programming:

It is a programming paradigm aimed at improving clarity, quality, and development time.


a computer program, using only subroutines and three structures: sequence,
selection (if and switch) and iteration (for and while loops), considering it unnecessary and counterproductive
the use of the unconditional transfer instruction (GOTO), which could lead to 'code
espagueti", que es mucho más difícil de seguir y de mantener, y era la causa de muchos errores de
programming.

The structured program theorem, proposed by Böhm-Jacopini, demonstrates that every program
can be written using only the following three control instructions:

Sequence

Conditional instruction.

Iteration (instruction loop) with condition at the beginning.

Only with these three structures can all possible programs and applications be written.
Si bien los lenguajes de programación tienen un mayor repertorio de estructuras de control, éstas
They can be constructed using the three basic ones mentioned.

An important feature of a structured program is that it can be read in sequence,


from the beginning to the end without losing the continuity of the task that the program fulfills, it
contrary to what happens with other programming styles. This fact is important because
it is much easier to fully understand the work done by a specific function if
all the instructions that influence its action are physically contiguous and enclosed by a
block. The ease of reading, from start to finish, is a consequence of using only three
control structures, and to eliminate the goto control transfer statement.
Structured programming is a programming theory that involves building programs to
easy to understand, it is especially useful when corrections or modifications need to be made
after having completed a program or application. When using structured programming, it is
much easier to understand the program coding, which will have been done in different
sections.

It indicates that the instructions of a program are executed one after the other, in the same order.
in which they appear in the program.

It is graphically represented as one box after another, both with a single input and a single output.
exit.

Boxes A and B can be defined to execute from a simple instruction to a module or


complete program, as long as they are also appropriate programs.

Structures:

Sequential structure

A program structure is sequential if the instructions are executed one after another, in a manner of
linear sequence; that is to say, one instruction does not execute until the previous one is completed, it does not
the flow of the program repeats itself.

Example inQBASIC:

I input the data x and y


INPUTx
INPUTy

I exchange the values x and y


auxiliary=x' I keep a copy of the value of x in auxiliary
x=y I store the value of y in x, losing its previous value, but retaining a
copy to auxiliary.
y = auxiliary; I copy to y the value of auxiliary, which is the initial value of x.

I print the variables (which now have the exchanged values)


PRINTx
PRINTy

Selective or selection structure

The selective structure allows the execution of the program to branch to an instruction (or
set of them). According to a logical criterion or condition, only one of the paths at the fork
It will be taken to be executed.

Example in QBASIC:
If A is greater than B then if a is greater than b
PRINT a;" is greater than "; bprint "a is greater than b"
ELSE otherwise
PRINT a; " is not greater than "; bprint "a is not greater than b"
END IF

The keywords IF, THEN, ELSE, and END IF constitute the structure of the statement.
conditional.

Iterative structure

An iterative loop or iteration of a sequence of instructions causes it to repeat.


execution while a condition is met. The number of iterations is usually
determined by a change in the condition within the same loop, although it can be forced or
explicit for another condition.

Example inQBASIC:

a= 0
b=7
DO WHILE b > awhile b is greater than a
PRINTa prints the value of a
a = a + 1 ' increments 'a' by 1
LOOP

Advantages of structured programming:

Programs are easier to understand, they can be read sequentially and there are no
need to make cumbersome follow-ups on line jumps (GOTO) within the blocks
of code to try to understand the logic.
The structure of the programs is clear, since the instructions are more closely linked or
related to each other.
Reduction of effort in testing and debugging. Tracking of failures or errors of
The program ('debugging') is facilitated by its simpler and more understandable structure, because of
that errors can be detected and corrected more easily.
Reduction of maintenance costs. Similarly to the streamlining, during the phase of
Maintenance, modification or extension of programs becomes easier.
The programs are simpler and faster to create.
The performance of programmers increases compared to the previous way that
use GOTO.

Programming styles:
Programming style (also called coding standards or code convention) The style of
programming is frequently dependent on the programming language that has been chosen for
writing. For example, the style of the C programming language will vary compared to that of
BASIC language.

The 'programming style' refers to the way the source code is formatted. For C, this
it involves how the braces are positioned, how the code is indented, and how parentheses are used.
GNOME has a mix of programming styles and does not require the use of any of them. It
the most important thing is that the code is consistent within a program or a library—
Code with a disorganized format is unacceptable because it is difficult to read.

Criterios para un buen estilo de programación:

Appropriate variable names

A key piece for a good style is the appropriate choice of variable names.
Poorly named variables hinder the reading of the source code and its understanding.

As an example, consider the following extract frompseudocode:

get a b c
if a < 24 and b < 60 and c < 60
return true
else
return false

Due to the choice of variable names, it is difficult to understand the function of the code.
Now compare yourself with the following version:

Due to the variable naming, it is difficult to realize the function of the code.
Now compare yourself with the following version:

hours minutes seconds


if hours < 24 and minutes < 60 and seconds < 60
return true
else
return false

The intention of the code is now easier to discern, 'given a time in 24 hours, it
it will return true if valid and false if not.

Indentation style
Indentation style in programming languages that use braces for indentation or delimiting.
logical code blocks, such as C, a good style is also a key point. Using
A logical and consistent style makes one's code more readable. Compare:

if(hours < 24 && minutes < 60 && seconds < 60) {


returntrue;
}
returnfalse;
}

with something like

if(hours<24&&minutes<60&&seconds<60){return true;}
else{return false;}

The first example is much easier to read because it is well indented, and the blocks
Code logic is grouped and represented together more clearly.

Loops and control structures

The use of logical control structures for loops is also part of good style.
programming. Help someone who is reading the code to understand the execution sequence (in
imperative programming). For example, the following pseudocode

count = 0
while count < 5
print account * 2
count = count + 1
endwhile

The previous extract meets the two previous style recommendations, but the following use
from the construction makes the code much easier to read:

forcuenta = 0, cuenta < 5, cuenta=cuenta+1


printcount * 2

In many languages, the frequently used pattern "for each element in a range" can be
shortened to:

forcuenta = 0to5
printaccount * 2

Spacing

Free format languages often ignore whitespace. The good use of


spacing in the code layout of one is, therefore, considered a good style of
programming.

Compare the following excerpt of C code:

int count; for(count=0;count<10;count++){printf("%d",count*count+count);}

with:

int count;
for (counter = 0; counter < 10; counter++)
{
printf("%d", account * account + account);
}

In programming languages of the C family, it is also recommended to avoid the use of characters
tabulator in the middle of a line, as different text editors display its width differently
different.

Most used programming styles:

Gnu Style
K&R style: The K&R style is the most used in the C and PHP languages. The K&R style was named after
this form because it was used by Kernighan and Ritchie in their book The C Programming Language.

It is about opening the bracket on the same line as the order statement, indenting the following ones.
steps at the same level as the key and closing the key at the same level as the declaration.

Example

Allman style: The Allman style was defined by Eric Allman. It involves creating a new line.
for the braces, and indent the code underneath them.

The closing key has the same indentation as the opening one.

Example:

Style BSD KNF: Also known as Kernel style


Normal Form, es la manera más usada para el código de la distribución del software del sistema
Berkeley operation. It is an extension of the K&R style.
Se define un tabulador duro (8 espacios) el cual es usado para indentar bloques de código, mientras
a soft tab (4 spaces) for all continuous lines that exceed the viewport
the console.

Example:

Whitesmiths style

The Whitesmiths style, also known as the Wishart style. This style places the braces associated with the
indented control instructions on the following line.

This style places the key that follows a block statement indented on the line.
next. Instructions within the block are indented at the same level as the brace.

Example

GNU Style

The GNU style places a brace on the next line.

The keys are indented by 2 spaces, and the code they contain is indented by 2 spaces.
additional.

Example
Other programming techniques:

Modular programming: It is a programming paradigm that consists of dividing a program into


modules or subprograms in order to make it more readable and manageable. When applying programming
modular, a complex problem must be divided into several simpler subproblems, and these to their
once in other simpler subproblems. This must be done until obtaining subproblems that
sufficiently simple to be easily solved with some programming language
programming. This technique is called successive refinement, divide and conquer, or top-down analysis
(Top-Down).

Procedural programming or by procedures: it is a programming paradigm.


It is often applicable in both low-level programming languages and in languages of
high level. In the case that this technique is applied in high-level languages, it will be called
functional programming. This technique consists of relying on a very low number of expressions
repeated, group them all into a procedure or function and call it each time it is needed
to be executed.

This programming technique offers very good response in relation to the size of the programs.
and in raw form it is almost not noticeable in the execution speed of them (while the variables,
constants or vector indices are in memory, as is usually normal, these will relate between
yes, without a considerably high memory load for modern processors); although it is
it is very complicated to obtain a pure procedural programming.

As an example, if we want to show the previous, the following, and a number of our own from a list
(vector), a pseudocode by procedures or functions (at a high level) would be:

function anterior_posterior( number ) {


out(number-1);
out(number);
out(number+1);
}

for(i = 0; i < size(list); i++) {


anterior_posterior( list[i] );
}

Or if we want to divide the number by 2, 3, 4, 5, 6, 7, 8, 9, and 10 into [Link]:

function div(number) {
out(number/2);
out(number/3);
out(number/4);
out(number/5);
out(number/6);
out(number/7);
out(number/8);
out(number/9);
out(number/10);
}

for( i = 0; i < size(list); i++) {


div( list[i] );
}

Using this type of programming can be very useful when programming large
projects, since it creates an immense library of special functions for procedures
frequently used within the program.

Object-oriented programming: it is a programming paradigm that comes to innovate the


way to obtain results. Objects manipulate input data to obtain data
specific outputs, where each object provides a special functionality.

Many of the pre-designed objects of current programming languages allow for the
grouping in libraries or bookstores, however, many of these languages allow the user to
creation of their own libraries.

Está basada en varias técnicas, incluyendo herencia, cohesión, abstracción, polimorfismo,


coupling and encapsulation. Their use became popular in the early 1990s. In the
Currently, there is a wide variety of programming languages that support object-oriented programming.
objects.

Object-oriented programming differs from traditional structured programming, in which


the data and the procedures are separate and unrelated, since the only thing being sought is the
processing some input data to obtain other output data. Structured programming
encourages the programmer to think primarily in terms of procedures or functions, and secondly
place in the data structures that those procedures manage. In structured programming
only functions that process data are written. Programmers who use OOP, on the other hand,
first they define objects to then send them messages requesting them to execute their methods by
themselves.

With OOP we have to learn to think about things in a different way, to write our
programs in terms of objects, properties, methods, and other things that we will quickly see
to clarify concepts and provide a small foundation that allows us to loosen up a bit with this type of
programming.

Thinking in terms of objects is very similar to how we would do it in real life. For example
let's think about a car to try to model it in an OOP scheme. We would say that the
car is the main element that has a series of characteristics, such as color,
model or brand. It also has a series of associated functionalities, such as being able to put on
in motion, stop or park.

Well, in an OOP scheme, the car would be the object, the properties would be the characteristics such as the
color or the model and the methods would be the associated functionalities such as getting started or
stop.

To give another example, let's see how we would model a fraction in an OOP schema.
to say, that mathematical structure that has a numerator and a denominator that divides
numerator, for example 3/2.

The fraction will be the object and will have two properties, the numerator and the denominator. Then it could
to have several methods such as simplifying, adding with another fraction or number, subtracting with another
fraction, etc.

These objects can be used in programs, for example in a math program.


you will use fraction objects and in a program that manages a car workshop you will use objects
car. Object-Oriented programs use many objects to perform the actions that are
they want to carry out and they themselves are also objects. That is to say, the car workshop will be an object that
will use car objects, tool, mechanic, spare parts, etc.

Event-driven programming: it is a programming paradigm in which both the structure


as the execution of the programs is determined by the events that occur in the system,
defined by the user or that they themselves cause.

To understand event-driven programming, we can oppose it to what it is not: while in


sequential (or structured) programming is where the programmer defines what the flow will be.
of the program, in event-driven programming it will be the user himself—or whatever it is that is
running the program—the one that directs the flow of the program. Although in programming
sequentially there can be intervention by an external agent to the program, these interventions
will occur when the programmer has determined it, and not at any moment as it could be
en el caso de la programación dirigida por eventos.
In event-driven programming, when the program starts executing, the following will take place
the initializations and other initial code and then the program will be blocked until
that some event takes place. When one of the events expected by the program occurs,
the program will execute the code of the corresponding event handler. For example,
if the event consists of the user having clicked on the play button of a player
movies, the event manager code will run, which will make the movie
show on screen.

A clear example is found in the Lexical programming systems and Visual Basic, in which
each element of the program (objects, controls, etc.) is assigned a series of events that
it will generate said element, like pressing a mouse button on it or redrawing the
control.

You might also like