STRUCTURED PROGRAMMING
| Site: | 2025cs167767.gnomio.com |
| Course: | 2025cs167767.gnomio.com |
| Book: | STRUCTURED PROGRAMMING |
| Printed by: | |
| Date: | Tuesday, 7 July 2026, 7:40 PM |
1. Introduction to structured programming
Structured programming is a programming paradigm characterized by source code that uses block-based source code structure to encode control flow such as sequence, selection (i.e. if-then-else and switch) and iteration (i.e. for and while).
Originally, the central goal of the structured programming movement was to eliminate the need for and use of the goto statement. As goto provides powerful and flexible flow control, it can be used to write any arbitrarily complex algorithm, but the resulting code often has significant quality issues, commonly described as spaghetti code. Structured programming replaces goto with constructs that tend to result in better code. The paradigm became popular and for the most part achieved the goal of supplanting goto. In fact, its ubiquity is so thorough that for much of software development, it is simply the way code is written, no longer a topic of discussion as it once was.
Structured programming is sometimes associated with modular programming even though they are different. In a general sense, structured implies a sense of modularity and of being written to be efficient and easy to understand and modify, but this is not what structured programming means in a narrow sense.
After structured programming became popular, the style of programming that preceded it was retroactively called non-structured programming. Although technically a programming paradigm, it differs from other paradigms in that it was not intentionally designed. It was simply the state-of-the-art before structured programming was envisioned.
1.1. Types of programming language
Programming languages can be grouped into several major categories based on their paradigms and intended use cases. While many languages overlap categories, understanding these types helps in choosing the right tool for a project.
1. Procedural Languages Follow a sequence of instructions (procedures) to perform tasks. They emphasize structured programming and step-by-step execution. Examples: C, C++, Java, Pascal, BASIC.
2. Functional Languages Focus on mathematical functions, immutability, and avoiding side effects. Ideal for concurrent and data-heavy computations. Examples: Haskell, Scala, Elixir, F#, Erlang.
3. Object-Oriented Languages (OOP) Organize code into reusable objects containing data (attributes) and behavior (methods). Great for large, scalable systems. Examples: Java, Python, Ruby, C++, PHP.
4. Scripting Languages Often interpreted, used for automation, web development, and rapid prototyping. They excel in flexibility and ease of use. Examples: Python, JavaScript, PHP, Bash, Perl.
5. Logic Languages Express rules and facts for the computer to infer solutions, common in AI and computational linguistics. Examples: Prolog, Datalog, Absys.
1.2. History of programming languages
The paradigm emerged in the late 1950s with the appearance of the ALGOL 58 and ALGOL 60 programming languages,[3] with the latter including support for block structures.
Contributing factors to its popularity and widespread acceptance, at first in academia and later among practitioners, include the publication of what is now known as the structured program theorem in 1966,[4] and the publication of the influential "Go To Statement Considered Harmful" open letter in 1968 by Dutch computer scientist Edsger W. Dijkstra, who coined the term structured programming.
2. Program development design
Program development is the organized process of creating a computer program to solve a specific problem. It involves a systematic approach known as the Program Development Life Cycle (PDLC).
2.1. The Program Development Cycle (PDLC)
This cycle outlines the stages a programmer goes through from identifying a problem to the final implementation and maintenance of the solution.
-
Stage 1: Problem Recognition & Definition
-
Identifying that a problem exists and determining if a computer solution is feasible.
-
Clearly defining the boundaries of the problem (what the program should and shouldn't do).
-
-
Stage 2: Problem Analysis
-
Decomposing the problem to understand its requirements.
-
Identifying:
-
Inputs: What data is needed?
-
Processes: What calculations or logical operations are required?
-
Outputs: What information must the program produce?
-
-
-
Stage 3: Program Design
-
Planning the solution logic before writing any code.
-
Tools used include algorithms, flowcharts, and pseudocode (detailed below).
-
Focuses on the logical flow and data structures.
-
-
Stage 4: Coding
-
Translating the design logic into a specific programming language (e.g., C, Pascal, Java).
-
Adhering to the syntax rules of the chosen language.
-
-
Stage 5: Testing and Debugging
-
Testing: Running the program with sample data to ensure it produces correct results.
-
Debugging: The process of detecting, locating, and correcting errors (syntax, logic, or runtime errors).
-
-
Stage 6: Implementation
-
Installing the software for the end-user.
-
Includes training users and converting from the old system to the new one.
-
-
Stage 7: Maintenance
-
Updating the program to fix previously undiscovered bugs or adding new features as user needs change.
-
-
Stage 8: Documentation
-
Creating written descriptions of the program (User Manuals for users, Technical Manuals for developers). This is done throughout the cycle.
-
2.2. Program design tools
Design tools help programmers visualize the logic of a solution before coding.
A. Algorithms
-
Definition: A step-by-step procedure or set of rules to be followed in calculations or other problem-solving operations.
-
Characteristics:
-
Finite: Must end after a specific number of steps.
-
Definite: Each step must be clear and unambiguous.
-
Effective: Steps must be basic enough to be carried out.
-
B. Flowcharts
-
Definition: A diagrammatic representation of an algorithm using standard symbols connected by arrows to show the flow of control.
-
Common Symbols:
-
Oval (Terminal): Start or Stop.
-
Parallelogram (Input/Output): Reading data or printing results.
-
Rectangle (Process): Calculations or assignments (e.g.,
x = a + b). -
Diamond (Decision): A condition with Yes/No branches (e.g.,
Is x > 10?). -
Arrows: Indicate the direction of flow.
-
C. Pseudocode
-
Definition: A high-level description of an algorithm that uses structural conventions of programming languages but is intended for human reading rather than machine reading.
-
Style: It looks like code (using indentation and keywords like
IF,WHILE,PRINT) but ignores strict syntax rules (like semicolons). -
Example:
PlaintextSTART READ age IF age >= 18 THEN PRINT "Eligible to vote" ELSE PRINT "Not eligible" ENDIF STOP
4. Structured Design Concepts
A. Top-Down Design (Stepwise Refinement)
-
The process of breaking a complex problem into smaller, manageable sub-problems (modules).
-
You start with the main goal and break it down level by level until you reach simple tasks that can be easily coded.
B. Modular Programming
-
Writing code in separate, independent blocks (modules/functions) rather than one massive file.
-
Advantages:
-
Reusability: A module (e.g., a "CalculateTax" function) can be used in different parts of the program.
-
Easier Debugging: You can test one module at a time.
-
Teamwork: Different programmers can work on different modules simultaneously.
-
3. Program structure
1. General Structure of a Structured Program
A structured program is not written as one continuous block of text. It is divided into distinct sections, usually following this "skeleton":
A. Documentation Section (Comments)
-
Purpose: This section consists of comments (lines ignored by the computer) that explain what the program does, who wrote it, and when.
-
Syntax:
-
Single line:
// This is a comment -
Multi-line:
/* This is a comment */
-
B. Link / Preprocessor Section
-
Purpose: Instructs the compiler to link functions from the system library.
-
Example:
#include <stdio.h>tells the program to include standard input/output functions (likeprintforscanf).
C. Definition & Global Declaration Section
-
Definition: Defines symbolic constants (e.g.,
#define PI 3.142). -
Global Declaration: Declares variables that are visible to the entire program (all functions), not just the main block.
D. The Main Function (main())
-
Purpose: This is the entry point of the program. Every structured program must have one main function. Execution starts here.
-
Structure:
Cint main() { // Declaration part // Executable part return 0; }-
Declaration Part: All variables used in the function must be declared here.
-
Executable Part: Contains the statements (logic) to be executed.
-
E. Sub-Program Section (User-Defined Functions)
-
Purpose: These are additional functions created by the user to perform specific tasks. They are usually placed after the
main()function (or before, depending on the language rules). -
Example: A function named
calculateSum()might be written here and called insidemain().
2. Format of a Structured Language
Structured languages (like C) generally follow these formatting rules:
-
Case Sensitivity: Most are case-sensitive (e.g.,
Gradeandgradeare different variables). -
Statement Termination: Every executable statement must end with a terminator, usually a semicolon (
;). -
Blocks: Code belonging to a specific function or loop is enclosed in braces
{ }(in C/C++/Java) orBegin...End(in Pascal). -
Free-Form: You can write multiple statements on one line or split one statement across multiple lines (though not recommended for readability).
3.1. Operators
3.2. Data types
1. Classification of Data Types
Data types in C are broadly classified into three categories:
A. Primary (Primitive/Fundamental) Data Types
These are the basic built-in types provided by the C language.
| Data Type | Keyword | Description | Typical Size (bytes) | Format Specifier |
| Integer | int |
Stores whole numbers (positive or negative). | 2 or 4 | %d |
| Character | char |
Stores a single character (letter/symbol). | 1 | %c |
| Float | float |
Stores numbers with decimal points (single precision). | 4 | %f |
| Double | double |
Stores large decimal numbers (double precision). | 8 | %lf |
| Void | void |
Represents the absence of value (used in functions). | 0 | - |
B. Derived Data Types
These are derived from the fundamental data types to handle complex data.
-
Arrays: A collection of elements of the same data type (e.g., a list of 10 integers).
-
Pointers: Variables that store the memory address of another variable.
-
Functions: A block of code that returns a value of a specific data type.
C. User-Defined Data Types
These allow the programmer to define their own data structures.
-
Structure (
struct): A collection of variables of different data types grouped together under a single name (e.g., a "Student" structure containing name, age, and marks). -
Union (
union): Similar to structures, but all members share the same memory location. -
Enumeration (
enum): Consists of named integer constants (e.g.,enum Week {Mon, Tue, Wed}).
2. Data Type Modifiers
Modifiers are used with basic data types (specifically int and char) to alter the size or range of data they can hold.
-
signed: The variable can hold both positive and negative numbers (default forint). -
unsigned: The variable can hold only positive numbers (doubles the positive range). -
short: Reduces the storage size (usually 2 bytes). -
long: Increases the storage size (usually 4 or 8 bytes).
Example of Modifier Usage:
short int age = 20; // Uses less memory
unsigned long int distance; // Uses more memory, positive values only
3. Variable Declaration Syntax
In structured programming, you must declare a variable's data type before using it.
Syntax: data_type variable_name;
Examples:
int score = 95;
float pi = 3.142;
char grade = 'A';
double salary = 50000.50;
4. Choosing the Right Data Type
-
Use
intfor counters or counting items (e.g., number of students). -
Use
floatfor measurements needing precision (e.g., temperature, currency). -
Use
doublefor high-precision scientific calculations. -
Use
charfor menu options (e.g., Press 'Y' for Yes).