Programming Concepts
1. Control Structures
Control structures manage the flow of a program based on conditions or repetition.
a. Repetition / Iteration / Loop
Used to repeat a block of code multiple times.
• Types of Loops:
o For Loop: Repeats a block for a specific number of times.
▪ Example:
for i in range(5): print(i)
o While Loop: Repeats while a condition is true.
▪ Example:
while count < 5: count += 1
o Do-While Loop: Executes at least once, then repeats while a condition is
true (common in C, Java).
▪ Syntax Example (in pseudocode):
cpp
CopyEdit
do {
// code
} while (condition);
b. Selection
Allows a program to make decisions and execute different paths.
• Types:
o If Statement: Executes a block if a condition is true.
▪ if age >= 18: print("Adult")
o If-Else: One block runs if true, another if false.
▪ if score >= 50: print("Pass") else: print("Fail")
o If-Elif-Else (Multiple Conditions):
▪ if mark >= 75: print("Distinction") elif mark >= 50: print("Pass") else:
print("Fail")
o Switch/Case (in some languages like C, JavaScript): Used for multi-way
branching.
2. Functions
A function is a reusable block of code that performs a specific task.
• Why use Functions?
o Increases code reusability and readability
o Makes programs modular and easier to debug/test
• Function Parts:
o Definition: Declares what the function does
o Parameters: Inputs to the function
o Return Value: Output from the function
• Example (Python):
python
CopyEdit
def add(a, b):
return a + b
result = add(3, 4)
• Types of Functions:
o Built-in (e.g., print(), len())
o User-defined (custom functions)
o Recursive (calls itself)
3. Testing and Debugging
a. Testing
Checking if the program works correctly and meets requirements.
• Types of Testing:
o Unit Testing: Testing individual parts (functions/modules)
o Integration Testing: Testing combined parts
o System Testing: Testing the entire system
o User Acceptance Testing (UAT): Final test with real users
• Common Test Cases:
o Normal input
o Boundary values (e.g., edge of valid ranges)
o Invalid input (e.g., letters where numbers are expected)
b. Debugging
Finding and fixing errors in the code.
• Types of Errors:
o Syntax Errors: Mistakes in code structure (e.g., missing colon)
o Runtime Errors: Errors during program execution (e.g., divide by zero)
o Logic Errors: Program runs but gives incorrect results
• Debugging Techniques:
o Use print statements to trace values
o Use debugging tools (breakpoints, watches)
o Review error messages
o Test in small chunks