Ss1 2nd Term Week 4 Lesson Notes-2
Ss1 2nd Term Week 4 Lesson Notes-2
WEEK:4
qbasic
Copy code
PRINT "Hello, World!"
o Explanation:
The PRINT command displays text or values on the screen.
The string "Hello, World!" is the text that will appear in the output.
Statements:
oEach line of code is called a statement and usually ends with a command like
PRINT, INPUT, or a loop structure like FOR...NEXT.
Comments:
o Comments are used to annotate code. In QBASIC, comments begin with a single
quote (').
qbasic
Copy code
' This is a comment
PRINT "This is a message"
Example Program:
qbasic
Copy code
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
sum = num1 + num2
PRINT "The sum is: "; sum
Explanation:
Syntax:
qbasic
Copy code
IF condition THEN
' Action to perform if condition is true
END IF
Example:
qbasic
Copy code
INPUT "Enter your age: ", age
IF age >= 18 THEN
PRINT "You are an adult."
ELSE
PRINT "You are a minor."
END IF
Explanation:
The program checks if the user's age is greater than or equal to 18.
If true, it prints "You are an adult."
If false, it prints "You are a minor."
When there are multiple conditions, the SELECT CASE structure is useful.
Syntax:
qbasic
Copy code
SELECT CASE variable
CASE condition1
' Action for condition1
CASE condition2
' Action for condition2
CASE ELSE
' Action for all other cases
END SELECT
Example:
qbasic
Copy code
INPUT "Enter a number (1-3): ", num
SELECT CASE num
CASE 1
PRINT "You selected option 1."
CASE 2
PRINT "You selected option 2."
CASE 3
PRINT "You selected option 3."
CASE ELSE
PRINT "Invalid selection."
END SELECT
Explanation:
The program checks the number entered by the user and prints a corresponding message.
The CASE ELSE section handles any invalid input.
6. Loops in QBASIC
For...Next Loop:
qbasic
Copy code
FOR i = 1 TO 5
PRINT "Iteration number "; i
NEXT i
Explanation:
Do...Loop:
A DO...LOOP can repeat code while a condition is true or until it becomes true.
qbasic
Copy code
DO
INPUT "Enter a number (0 to stop): ", num
PRINT "You entered: "; num
LOOP UNTIL num = 0
Explanation:
Writing to a File:
qbasic
Copy code
OPEN "data.txt" FOR OUTPUT AS #1
WRITE #1, "Hello, World!"
CLOSE #1
Explanation:
qbasic
Copy code
OPEN "data.txt" FOR INPUT AS #1
INPUT #1, data$
PRINT "The file contains: "; data$
CLOSE #1
Explanation:
Variables: Used to store data that can change during the program's execution.
o Example:
qbasic
Copy code
DIM num AS INTEGER
num = 10
qbasic
Copy code
CONST Pi = 3.14159
Data Types:
o INTEGER: Whole numbers (e.g., 5).
o SINGLE: Floating-point numbers (e.g., 3.14).
o STRING: Text (e.g., "Hello").
Classwork Activities
1. Activity 1: Write a program that calculates the area of a rectangle (length * width).
o Solution:
qbasic
Copy code
INPUT "Enter length: ", length
INPUT "Enter width: ", width
area = length * width
PRINT "The area of the rectangle is: "; area
qbasic
Copy code
INPUT "Enter a number: ", num
IF num > 0 THEN
PRINT "The number is positive."
ELSEIF num < 0 THEN
PRINT "The number is negative."
ELSE
PRINT "The number is zero."
END IF
3. Activity 3: Write a program that uses a FOR loop to print the first 10 even numbers.
o Solution:
qbasic
Copy code
FOR i = 2 TO 20 STEP 2
PRINT i
NEXT i
4. Activity 4: Write a program that stores the name and age of a person into a file and then
reads and displays the content.
o Solution:
qbasic
Copy code
OPEN "person.txt" FOR OUTPUT AS #1
INPUT "Enter your name: ", name$
INPUT "Enter your age: ", age
WRITE #1, name$, age
CLOSE #1
Summary
Students should practice writing more complex programs and understand how to structure their
code effectively.
Homework:
1. Write a program that calculates the factorial of a number using a FOR loop.
2. Create a program that asks the user to input their name and age, then writes this
information into a file and reads it back to the user.