Programming Basics (Copy)
1. Introduction to Programming Basics
- Writing a program requires designing a solution and then coding it using appropriate programming tools.
- Key programming tools include:
- Basic statements
- Programming constructs
- Control structures
- Understanding variables, constants, and functions is crucial for structured programming.
2. Constants and Variables
- Constant:
- A named value that does not change during program execution.
- Always assigned a value upon declaration.
- Variable:
- A named value that can change during program execution.
- Should be declared before use to ensure predictable behavior.
- Best practice:
- Maintain an identifier list to track declared variables and constants.
- Assign values to variables before performing operations.
- Some languages, such as Python, do not explicitly support constants, but assigning an initial value ensures intended usage.
3. Declaring Constants and Variables in Pseudocode
- Example of pseudocode for declaring constants and variables:
DECLARE radius : REAL DECLARE volume : REAL DECLARE surfaceArea : REAL CONSTANT pi ← 3.142 - Example algorithm to calculate and output the volume and surface area of a sphere:
OUTPUT "Please enter the radius of the sphere" INPUT radius WHILE radius <= 0 DO OUTPUT "Please enter a positive number" INPUT radius ENDWHILE volume ← (4 / 3) * pi * radius * radius * radius surfaceArea ← 4 * pi * radius * radius OUTPUT "Volume is ", volume OUTPUT "Surface area is ", surfaceArea - Implementation in different languages:
- Python: Uses inline variable assignments without explicit declarations.
- Visual Basic (VB): Requires explicit declarations of variables.
- Java: Uses
finalfor constants, emphasizing immutability.
4. Library Routines
- Library routines: Predefined functions in a programming language’s development system.
- Advantages:
- Fully tested and reliable.
- Enhance efficiency and ease of programming.
- Common standard library functions include:
- Input/output operations
- Mathematical computations
- String manipulation
- Data structure handling (e.g., sorting, searching)
5. Example of Built-in Functions
- Mathematical operations:
DIV(x, y): Returns the integer division result.MOD(x, y): Returns the remainder of division.
- String manipulation functions:
LENGTH(anyString): Returns the length of the string.LEFT(anyString, x): Returns the leftmostxcharacters.RIGHT(anyString, x): Returns the rightmostxcharacters.MID(anyString, x, y): Extractsycharacters from positionx.
6. Password Validation Example Using String Functions
- Objective: Check if an input password meets specific criteria.
- Algorithm:
CONSTANT storedPassword ← "Secret" DECLARE inputPassword : STRING DECLARE size : INTEGER OUTPUT "Please enter your password" INPUT inputPassword size ← LENGTH(inputPassword) IF size = LENGTH(storedPassword) THEN IF (LEFT(inputPassword, 1) = LEFT(storedPassword, 1)) AND (RIGHT(inputPassword, 1) = RIGHT(storedPassword, 1)) THEN OUTPUT "Password entered has correct first and last letters" ELSE OUTPUT "Password entered is incorrect" ENDIF ELSE OUTPUT "Password entered is incorrect" ENDIF - Implementation Considerations:
- Case sensitivity should be considered (e.g., checking both uppercase and lowercase versions).
- Additional security measures like hashing can improve password handling.
7. Summary and Best Practices
- Always declare variables and constants clearly to ensure readability and maintainability.
- Use library routines whenever possible to optimize code efficiency.
- Follow structured programming practices, such as:
- Using functions and procedures to modularize code.
- Implementing loops and conditionals effectively.
- Ensuring clear input and output handling.
- Testing programs thoroughly with different input data, including edge cases.
