Lesson 3 | An unstructured program |
Objective | Identify the disadvantages of unstructured programs |
Definition of an Unstructured Program
In computer science, an unstructured program is characterized by a lack of clear, modular organization and the extensive use of goto statements, leading to a flow of control that is difficult to follow and understand. This programming style contrasts sharply with the principles of structured programming, which emphasizes readability, maintainability, and logical structure through the use of well-defined control structures such as loops and conditional branches.
Unstructured programming typically manifests in programs where the code does not adhere to a coherent structure, making extensive use of jumps or branches that lead to what is colloquially known as "spaghetti code." This term reflects the tangled and convoluted nature of the program flow, akin to the strands of spaghetti. The main characteristics of unstructured programs include:
- Extensive Use of Goto Statements: Unstructured programs heavily rely on goto statements for control flow, resulting in a non-linear and unpredictable execution path. This makes the program logic hard to follow and debug.
- Lack of Modularization: Unlike structured programs, which are divided into functions or subroutines, unstructured programs often consist of long sequences of code without clear division into logical units. This lack of modularity hampers reusability and testing.
- Global Variables and Side Effects: Unstructured programs tend to make indiscriminate use of global variables, leading to unintended side effects where changes in one part of the program unexpectedly affect other parts.
- Poor Maintainability: The tangled nature of the code makes it difficult to understand, modify, or extend the program. Even minor changes can have unforeseen consequences, increasing the risk of introducing bugs.
- Limited Scalability: As the size and complexity of an unstructured program increase, the inherent difficulties in understanding and managing the code exacerbate, severely limiting the program's scalability.
- Difficulty in Debugging and Testing: The unpredictable flow of control and intertwined logic complicate the debugging process, making it challenging to isolate and fix errors. Similarly, the lack of modularity and extensive side effects hinder effective testing.
Unstructured programming is generally considered an outdated and inefficient approach to software development, particularly for large or complex projects. Modern programming practices and languages encourage structured programming techniques, which provide a more organized, maintainable, and scalable framework for software development.
Example Unstructured Program
Before digging into structured programming, let us have a look at an unstructured program.
Here is a program written in the BASIC programming language which averages the numbers that are input:
5 LET S = 0
10 MAT INPUT V
20 LET N = NUM
30 IF N = 0 THEN 99
40 FOR I = 1 TO N
45 LET S = S + V(I)
50 NEXT I
60 PRINT S/N
70 GO TO 5
99 END
Example 1: Simple example of an infinite loop in Basic.
10 PRINT "INFINITE LOOP"
20 GOTO 10
Example 2:
Second example of an infinite loop in Visual Basic:
dim x as integer
do while x < 5
x = 1
x = x + 1
loop
Explanation of Example 2:
This creates a situation where x will never be greater than 5, because at the beginning of the loop code x is given the value of 1,
hence, the loop will always end in 2 and the loop will never break. This can be fixed by moving the x = 1
instruction outside the loop.
Essentially what this infinite loop does is to instruct a computer to keep on adding 1 to 1 until 5 is reached. Since 1+1 always equals 2, the condition will not be fulfilled.
Computer Science: Structured Approach Using C++
The important point to appreciate in these 2 unstructured examples is, that while both programs perform exactly the same task (that is they are infinite loops), the code must be written so that a structured walkthrough will be possible.
This principle becomes more pronounced as the program increases in size.
Large programs without structure are much more difficult and expensive to develop and maintain than structured programs, and thus are often referred to as spaghetti code.
In the next lesson we will examine pseudocode.
This is used to describe the logic of a program in a way that is independent of any particular programming language.