Structured Programming (Copy)
Introduction to Structured Programming
- Definition:
- A programming paradigm that emphasizes breaking down a program into smaller, manageable sections, improving readability, maintainability, and reducing errors.
- It follows principles such as modularity, top-down design, and control structures (sequence, selection, iteration).
- Key Benefits:
- Enhances code clarity by making it easier to read and debug.
- Encourages reusability of code components.
- Reduces redundancy through modular structures.
- Supports collaborative development by dividing tasks among programmers.
- Ensures logical organization of code, improving efficiency.
Key Concepts in Structured Programming
1. Procedures
- Definition:
- A procedure is a reusable block of code that performs a specific task but does not return a value.
- It is also known as:
- Void functions in Python
- Subroutines in VB
- Methods in Java
- Advantages of Procedures:
- Avoids repetition by defining a block once and calling it multiple times.
- Enhances program structure by dividing it into meaningful components.
- Simplifies debugging and updates.
- Syntax in Pseudocode:
PROCEDURE <identifier> <statements> ENDPROCEDURE - Calling a Procedure:
CALL <identifier> - Example:
- Procedure to print stars:
PROCEDURE stars (Number : INTEGER) FOR Counter ← 1 TO Number OUTPUT "*" NEXT Counter ENDPROCEDURE
2. Functions
- Definition:
- A function is a block of code similar to a procedure but always returns a value.
- Known as:
- Fruitful functions in Python
- Functions in VB
- Methods with returns in Java
- Syntax in Pseudocode:
FUNCTION <identifier> RETURNS <data type> <statements> ENDFUNCTION - Calling a Function:
myValue ← <identifier>(parameters) - Example:
- Function to convert Fahrenheit to Celsius:
FUNCTION celsius (temperature : REAL) RETURNS REAL RETURN (temperature – 32) / 1.8 ENDFUNCTION - Function vs Procedure:
Feature Procedure Function Returns a value No Yes Part of an expression? No Yes Can modify variables? Yes (if using reference parameters) No (generally)
3. Parameters and Arguments
- Parameter:
- A variable in the function or procedure definition that receives a value.
- Argument:
- The actual value passed to a function or procedure.
Types of Parameter Passing
- By Value (
ByVal):- The procedure/function gets a copy of the value.
- Changes to the variable do not affect the original value.
- Default method in Python and Java.
- By Reference (
ByRef):- The procedure/function gets the actual memory address of the variable.
- Changes affect the original variable.
- Used in VB explicitly with
ByRef.
- Example:
- Procedure to convert Fahrenheit to Celsius using
ByRef:
PROCEDURE celsius(BYREF temperature : REAL) temperature ← (temperature – 32) / 1.8 ENDPROCEDURE - Procedure to convert Fahrenheit to Celsius using
4. Modular Programming and Top-Down Design
- Definition:
- A method of structuring a program where it is broken down into smaller, self-contained modules.
- Each module (procedure/function) solves a specific part of the problem.
- Example:
- Main Program
- Calls a function to read input.
- Calls a function to process data.
- Calls a function to display results.
- Main Program
5. Control Structures in Structured Programming
5.1 Sequence
- Definition:
- The simplest control structure where instructions are executed one after another.
- Example:
OUTPUT "Enter name" INPUT name OUTPUT "Hello ", name
5.2 Selection (Decision Making)
- Definition:
- A structure that allows decisions based on conditions.
- Types:
- IF…THEN…ELSE
- CASE (Switch Statement)
- Example (IF…THEN…ELSE):
IF age >= 18 THEN OUTPUT "You can vote" ELSE OUTPUT "You cannot vote" ENDIF - Example (CASE Statement):
CASE OF option 1: OUTPUT "Option 1 selected" 2: OUTPUT "Option 2 selected" OTHERWISE: OUTPUT "Invalid option" ENDCASE
5.3 Iteration (Loops)
- Definition:
- Loops are used to repeat a block of code multiple times.
- Types:
- FOR…NEXT – Used when the number of iterations is known.
- WHILE…DO…ENDWHILE – Used when the condition is checked before entering the loop.
- REPEAT…UNTIL – Used when the condition is checked after executing the loop.
- Examples:
- FOR Loop:
FOR Counter ← 1 TO 10 OUTPUT "Hello" NEXT Counter- WHILE Loop:
WHILE score < 100 DO score ← score + 10 ENDWHILE- REPEAT Loop:
REPEAT OUTPUT "Enter value" INPUT value UNTIL value >= 0
6. Library Routines
- Definition:
- Pre-written functions available in a programming language that help with common tasks like:
- Mathematical operations (e.g.,
sqrt(),abs()) - String manipulation (e.g.,
len(),substring()) - File handling (e.g.,
open(),read())
- Mathematical operations (e.g.,
- Pre-written functions available in a programming language that help with common tasks like:
- Advantages:
- Saves time and effort.
- Reduces coding errors.
- Enhances performance.
7. Practical Applications
- Example Application: Car Depreciation Calculation
- A program to predict a car’s value based on depreciation:
- First year: 40% depreciation.
- Each subsequent year: 20% depreciation.
- Stops when the car is 9 years old or value < $1000.
- Algorithm Outline:
- Get initial car price.
- Loop for up to 9 years.
- Apply correct depreciation rate.
- Display updated car value.
- Exit when conditions are met.
- A program to predict a car’s value based on depreciation:
Conclusion
- Structured programming ensures clear, efficient, and maintainable code.
- The use of procedures, functions, loops, and selection statements improves modularity.
- Following structured programming principles helps in building large-scale applications with ease.
