0% found this document useful (0 votes)
8 views7 pages

Ss1 2nd Term Week 4 Lesson Notes-2

This document provides lesson notes on BASIC programming using QBASIC, covering topics such as getting started with the QBASIC editor, basic program structure, the IPO model, decision-making with IF/THEN statements, loops, file handling, and variables. It includes examples and explanations for each concept, along with classwork activities and homework assignments for practice. The lesson aims to equip students with foundational programming skills and encourage them to write more complex programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Ss1 2nd Term Week 4 Lesson Notes-2

This document provides lesson notes on BASIC programming using QBASIC, covering topics such as getting started with the QBASIC editor, basic program structure, the IPO model, decision-making with IF/THEN statements, loops, file handling, and variables. It includes examples and explanations for each concept, along with classwork activities and homework assignments for practice. The lesson aims to equip students with foundational programming skills and encourage them to write more complex programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SS1 2ND TERM LESSON NOTES

WEEK:4

TOPIC: BASIC Programming Using QBASIC

Introduction to BASIC Programming

BASIC (Beginner’s All-purpose Symbolic Instruction Code) is a high-level programming


language designed to be easy for beginners to learn and use. QBASIC is a version of BASIC that
comes with a built-in editor to write and execute programs.

1. Getting Started with QBASIC

 Opening QBASIC Editor:


o To start programming in QBASIC, open the QBASIC editor by clicking on the
QBASIC icon (installed on your computer or accessed through DOS-based
systems).
o The QBASIC window consists of several areas:
 Editor Window: Where you type your code.
 Output Window: Where the results of the program will be displayed.
 Menu Bar: Where options like saving, opening, and running programs are
available.
 Writing a Simple Program:
o The simplest program in QBASIC will display a message on the screen:

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.

2. Basic Program Structure in QBASIC

Programs in QBASIC are structured with the following basic components:

 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"

3. Input, Processing, and Output (IPO Model)

Every program generally follows the IPO model:

 Input: Taking input from the user using INPUT.


 Processing: Performing operations on the input.
 Output: Displaying the result using PRINT.

Example Program:

qbasic
Copy code
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
sum = num1 + num2
PRINT "The sum is: "; sum

Explanation:

 The INPUT command gets values from the user.


 The variables num1 and num2 store the numbers entered.
 The arithmetic operation num1 + num2 is performed and stored in the variable sum.
 The PRINT command outputs the result.

4. Decision-Making: IF/THEN Statements

IF/THEN statements allow a program to make decisions based on conditions.

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."

5. Case Decision Structure: SELECT CASE

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

Loops are used to repeat a block of code multiple times.

For...Next Loop:

A FOR...NEXT loop repeats a set number of times.

qbasic
Copy code
FOR i = 1 TO 5
PRINT "Iteration number "; i
NEXT i

Explanation:

 The loop starts at i = 1 and increments i until it reaches 5.


 Each iteration prints the current value of i.

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:

 This loop continues until the user enters 0.


 The INPUT command is used to get new input from the user each time.

7. File Handling in QBASIC


In QBASIC, files can be created, read, and written using commands like OPEN, WRITE, INPUT,
and CLOSE.

Writing to a File:

qbasic
Copy code
OPEN "data.txt" FOR OUTPUT AS #1
WRITE #1, "Hello, World!"
CLOSE #1

Explanation:

 The OPEN command opens the file data.txt for output.


 The WRITE command writes the text "Hello, World!" into the file.
 The CLOSE command closes the file after writing.

Reading from a File:

qbasic
Copy code
OPEN "data.txt" FOR INPUT AS #1
INPUT #1, data$
PRINT "The file contains: "; data$
CLOSE #1

Explanation:

 The OPEN command opens the file data.txt for input.


 The INPUT command reads the data from the file.
 The PRINT command displays the data on the screen.
 The CLOSE command closes the file.

8. Variables, Constants, and Data Types

 Variables: Used to store data that can change during the program's execution.
o Example:

qbasic
Copy code
DIM num AS INTEGER
num = 10

 Constants: Values that do not change during program execution.


o Example:

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

2. Activity 2: Create a program that checks if a number is positive, negative, or zero.


o Solution:

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

OPEN "person.txt" FOR INPUT AS #1


INPUT #1, name$, age
PRINT "Name: "; name$
PRINT "Age: "; age
CLOSE #1

Summary

In this lesson, We learned:

1. How to use the QBASIC editor to write and execute programs.


2. The basic program structure including input, processing, and output (IPO).
3. How to make decisions using IF/THEN and SELECT CASE.
4. How to use loops to repeat actions.
5. How to create, read, and write to files in QBASIC.
6. How to work with variables, constants, and data types.

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.

You might also like