STRUCTURED PROGRAMMING

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 (like printf or scanf).

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:

    C
    int 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 inside main().


2. Format of a Structured Language

Structured languages (like C) generally follow these formatting rules:

  • Case Sensitivity: Most are case-sensitive (e.g., Grade and grade are 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) or Begin...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).