Program Construction and Structured Programming
Program Construction and Structured Programming
Software development is a process through which, given a problem, a program is found (or
a set of programs) that solves it efficiently.
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.
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 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:
Structured 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.
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.
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.
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:
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
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
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.
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.
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:
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){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.
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:
In many languages, the frequently used pattern "for each element in a range" can be
shortened to:
forcuenta = 0to5
printaccount * 2
Spacing
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.
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:
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 keys are indented by 2 spaces, and the code they contain is indented by 2 spaces.
additional.
Example
Other programming techniques:
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 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);
}
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.
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.
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.
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.