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 (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).