Programming Concepts (Copy)
1. Declaring and Using Variables and Constants
- Variable
- A named storage location in memory whose value can change during program execution.
- Each variable has:
- A name (identifier)
- A data type
- A value
- Example in pseudocode:
DECLARE age : INTEGER age ← 16 age ← age + 1 - Purpose: Used for storing values that may be updated or recalculated.
- Constant
- A named value that does not change during program execution.
- Improves program maintainability — changes made to the constant definition affect all occurrences.
- Example in pseudocode:
CONSTANT Pi = 3.14159 area ← Pi * radius^2
- Declaration rules:
- Variables must be declared before they are used (depending on language).
- Constants are declared once, then used without modification.
2. Basic Data Types
- Integer
- Whole numbers, positive or negative, without decimal points.
- Examples: −15, 0, 27
- Pseudocode declaration:
DECLARE score : INTEGER
- Real
- Numbers with fractional parts (decimals).
- Examples: 3.14, −0.5, 10.0
- Pseudocode:
DECLARE temperature : REAL
- Char (Character)
- Single alphanumeric character or symbol.
- Stored inside single quotes.
- Examples: ‘A’, ‘7’, ‘%’
- Pseudocode:
DECLARE grade : CHAR
- String
- Sequence of characters.
- Stored inside double quotes.
- Examples: “Hello”, “CS2210”
- Pseudocode:
DECLARE name : STRING
- Boolean
- Logical data type that can hold only two values: TRUE or FALSE.
- Example:
DECLARE isLoggedIn : BOOLEAN
3. Input and Output
- Input – Reading data from the user or an external source.
- Example in pseudocode:
OUTPUT "Enter your name: " INPUT name
- Example in pseudocode:
- Output – Displaying data to the screen or writing to a file.
- Example in pseudocode:
OUTPUT "Welcome ", name
- Example in pseudocode:
- Purpose:
- Input allows interaction between the user and the program.
- Output provides feedback and results.
4. Sequence, Selection, Iteration, Totalling, and Counting
(a) Sequence
- Execution of instructions one after another in the order they are written.
- No branching or looping.
- Example:
INPUT length INPUT width area ← length * width OUTPUT area
(b) Selection
- Execution of different code depending on a condition.
- IF statements:
IF age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Minor" ENDIF - CASE statements:
CASE grade OF "A": OUTPUT "Excellent" "B": OUTPUT "Good" "C": OUTPUT "Pass" OTHERWISE OUTPUT "Fail" ENDCASE
(c) Iteration
- Repeating a block of code multiple times.
- Count-controlled loop:
FOR i ← 1 TO 10 OUTPUT i NEXT i - Pre-condition loop (WHILE):
WHILE answer <> "yes" INPUT answer ENDWHILE - Post-condition loop (REPEAT UNTIL):
REPEAT INPUT number UNTIL number > 0
(d) Totalling
- Adding up values over iterations.
- Example:
total ← 0 FOR i ← 1 TO 5 INPUT mark total ← total + mark NEXT i OUTPUT total
(e) Counting
- Keeping track of how many times a condition occurs.
- Example:
count ← 0 FOR i ← 1 TO 5 INPUT mark IF mark >= 50 THEN count ← count + 1 ENDIF NEXT i OUTPUT count
5. String Handling
- Length function: Returns number of characters in a string.
length ← LENGTH("Hello") // returns 5 - Substring: Extracts part of a string.
sub ← SUBSTRING("Computer", 1, 4) // returns "Comp"- Starting position can be 0 or 1 depending on language.
- Upper: Converts string to uppercase.
UPPER("hello") // returns "HELLO" - Lower: Converts string to lowercase.
LOWER("HELLO") // returns "hello"
6. Arithmetic, Logical, and Boolean Operators
Arithmetic Operators:
+(Addition)−(Subtraction)/(Division)*(Multiplication)^(Power of, e.g., 2^3 = 8)MOD(Modulus – remainder after division)DIV(Integer division – quotient without remainder)
Logical Operators:
=(Equal to)<(Less than)<=(Less than or equal to)>(Greater than)>=(Greater than or equal to)<>(Not equal to)
Boolean Operators:
AND(True if both conditions are true)OR(True if at least one condition is true)NOT(Reverses logical value)
7. Nested Statements
- Selection or iteration statements inside other selection or iteration blocks.
- Nested selection example:
IF grade = "A" THEN OUTPUT "Excellent" ELSE IF grade = "B" THEN OUTPUT "Good" ELSE OUTPUT "Needs improvement" ENDIF ENDIF - Nested iteration example:
FOR i ← 1 TO 3 FOR j ← 1 TO 2 OUTPUT i, j NEXT j NEXT i
8. Procedures, Functions, and Parameters
- Procedure:
- A reusable block of code that performs a task but does not return a value.
- Example:
PROCEDURE DisplayMenu() OUTPUT "1. Start" OUTPUT "2. Exit" ENDPROCEDURE
- Function:
- A reusable block of code that returns a value.
- Example:
FUNCTION Square(x : INTEGER) RETURNS INTEGER RETURN x * x ENDFUNCTION
- Parameters:
- Data passed into a procedure or function.
- Can be with parameters or without parameters.
- Max two parameters in syllabus requirements.
- Local Variables:
- Declared inside a procedure/function.
- Only accessible within that procedure/function.
- Global Variables:
- Declared outside all procedures/functions.
- Accessible by all parts of the program.
9. Library Routines
- Pre-written and built-in functions in the programming language.
- Examples:
MOD– ModulusDIV– Integer divisionROUND– Round decimal to nearest integerRANDOM– Generates random number
10. Creating Maintainable Programs
- Meaningful Identifiers:
- Names that describe their purpose.
- Example:
studentNameinstead ofx1.
- Commenting:
- Use comments to explain purpose of code sections.
- Syntax depends on language (e.g.,
//orREM).
- Procedures and Functions:
- Divide code into smaller parts to improve readability.
- Consistent Formatting:
- Indentation, spacing, and naming conventions.
