CSC 412Advanced Programming LECTURENOTES
CSC 412Advanced Programming LECTURENOTES
Key Concepts:
qbasic
A=5:B=10:IF A<B THEN PRINT "A is smaller" ELSE PRINT "B is smaller"
LET a = 5
LET b = 10
IF a < b THEN
ELSE
END IF
Python Example
python
1
# Good Style
if a < b:
print("A is smaller")
else:
print("B is smaller")
compare_numbers(5, 10)
• A set of rules and best practices for writing clean, maintainable, and efficient
code used by organizations (e.g., Google, Microsoft) to ensure consistency
across teams.
Naming Conventions
1 General Rules
2
Key Principles
python
# Good
if discount_rate > 0:
return price
# Bad
def calc(p,dr):
if dr>0:return p*(1-dr)
return p
Types of Comments
java
python
3
2. def compute_average(scores):
3. """
5. Args:
7. Returns:
9. """
• Redundant comments:
java
• Bad Example:
python
def process_user_data_and_send_email(user):
Good Example:
python
4
• def save_user(user): ...
Key Strategies
Example (Java)
java
if (b == 0) {
return a / b;
Techniques
Example (JavaScript)
5
javascript
const PI = 3.14;
7 Performance Considerations
Common Pitfalls
Optimization Example
python
# Bad: O(n²)
other_set = set(other_list)
Critical Rules
6
• Sanitize inputs (prevent SQL injection, XSS).
python
# Bad (vulnerable)
# Good (parameterized)
cursor.execute(query, (user_input,))
• Check for:
o Performance impacts.
7
• Single Entry, Single Exit (SESE): Functions should have one entry and exit
point.
qbasic
DO
python
while True:
if num >= 0:
break
8
Key Structures:
qbasic
CASE "A"
PRINT "Excellent!"
CASE "B"
PRINT "Good!"
CASE ELSE
END SELECT
python
match grade:
case "A":
print("Excellent!")
case "B":
print("Good!")
9
case _:
print("Try harder!")
Definition: A condition that remains true before and after each loop iteration.
qbasic
FUNCTION Factorial(n)
LET result = 1
LET i = 1
WHILE i <= n
result = result * i
i=i+1
WEND
Factorial = result
END FUNCTION
Python Example
python
def factorial(n):
result = 1
i=1
while i <= n:
10
result *= i
i += 1
return result
Top-Down Approach:
Bottom-Up Approach:
python
# High-level design
def calculator():
num1 = get_number()
num2 = get_number()
operation = get_operation()
display_result(result)
# Lower-level functions
def get_number():
# ...
qbasic
FUNCTION Add(a, b)
Add = a + b
END FUNCTION
FUNCTION Subtract(a, b)
Subtract = a - b
END FUNCTION
SUB Calculator
END SUB
Methods:
python
return a + b
print("Test Passed!")
qbasic
LET x = 10
LET y = 20
SWAP x, y
Techniques:
qbasic
FUNCTION IsPrime(n)
FOR i = 2 TO n \ 2
13
IF n MOD i = 0 THEN IsPrime = 0: EXIT FUNCTION
NEXT
IsPrime = 1
END FUNCTION
python
import random
def test_add():
for _ in range(5):
a = random.randint(1, 100)
b = random.randint(1, 100)
assert add(a, b) == a + b
14
APPENDIX I
QBASIC
CLS
FOR i = 1 TO n
INPUT numbers(i)
NEXT i
FOR i = 1 TO n - 1
FOR j = 1 TO n - i
END IF
NEXT j
NEXT i
15
' Display sorted numbers
FOR i = 1 TO n
PRINT numbers(i);
NEXT i
END
Explanation (QBasic):
1. Input: Takes the number of elements (n) and then the numbers.
python
numbers = []
# Input numbers
for i in range(n):
num = int(input())
numbers.append(num)
16
# Built-in sort function
numbers.sort()
Explanation (Python):
1. Input: Takes the number of elements (n) and then the numbers.
python
numbers = []
for i in range(n):
num = int(input())
17
numbers.append(num)
# Bubble Sort
Key Differences:
1. QBasic Implementation
18
CLS
DIM studentNames$(100)
DIM marks(100)
DIM grades$(100)
FOR i = 1 TO numStudents
NEXT i
FOR i = 1 TO numStudents
CASE IS >= 90
grades$(i) = "A"
CASE 80 TO 89
grades$(i) = "B"
CASE 70 TO 79
grades$(i) = "C"
19
CASE 60 TO 69
grades$(i) = "D"
CASE ELSE
grades$(i) = "F"
END SELECT
NEXT i
PRINT "----------------------------------"
PRINT "----------------------------------"
FOR i = 1 TO numStudents
NEXT i
END
2. Python Implementation
python
def calculate_grade(mark):
return "A"
20
elif mark >= 80:
return "B"
return "C"
return "D"
else:
return "F"
# Main program
students = []
for i in range(num_students):
print(f"\nStudent #{i+1}")
while True:
try:
break
else:
21
print("Please enter a value between 0 and 100")
except ValueError:
grade = calculate_grade(mark)
# Display results
print("\nGRADING REPORT")
print("-" * 40)
print(f"{'Name':<20}{'Mark':<10}{'Grade':<10}")
print("-" * 40)
print(f"{student['name']:<20}{student['mark']:<10}{student['grade']:<10}")
Key Features
2. Grading Scale:
o A: 90-100
o B: 80-89
o C: 70-79
22
o D: 60-69
o F: Below 60
3. Output Format:
4. Data Structure:
Appendix II
23