0% found this document useful (0 votes)
2 views11 pages

QBasic WASSCE Study Guide

The document is a comprehensive revision guide for QBasic, covering essential topics such as data types, control structures, loops, and file handling. It includes detailed explanations, examples, and common WASSCE-style questions to aid in understanding and application. Additionally, it provides quick reference keywords and exam tips for effective study preparation.

Uploaded by

princekporvi71
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)
2 views11 pages

QBasic WASSCE Study Guide

The document is a comprehensive revision guide for QBasic, covering essential topics such as data types, control structures, loops, and file handling. It includes detailed explanations, examples, and common WASSCE-style questions to aid in understanding and application. Additionally, it provides quick reference keywords and exam tips for effective study preparation.

Uploaded by

princekporvi71
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

QBasic

Complete WASSCE Revision Guide

Topics Covered

Introduction & Features of QBasic

Data Types, Variables & Constants

Operators & Expressions

Input / Output Statements

Control Structures (IF, SELECT CASE)

Loops (FOR…NEXT, WHILE…WEND, DO…LOOP)

Arrays & Subscripted Variables

String Functions

Numeric / Mathematical Functions

Subroutines & Functions (SUB / FUNCTION)

File Handling

Common WASSCE-Style Questions & Tips


1. Introduction to QBasic
QBasic (Quick Beginners All-purpose Symbolic Instruction Code) is a high-level structured programming
language developed by Microsoft. It is a simplified, interactive version of BASIC that was included with
MS-DOS.

Key Features of QBasic

Feature Description

<b>Easy to learn</b> Uses English-like keywords making it beginner-friendly.

<b>Interpreted/Compiled</b> QBasic interprets code line-by-line; QuickBASIC can compile.

<b>Structured</b> Supports SUB and FUNCTION procedures for modular programming.

<b>Interactive IDE</b> Has a built-in editor, debugger and immediate window.

<b>No line numbers required</b>Unlike old BASIC, QBasic does not require line numbers.

<b>Case-insensitive</b> Keywords can be typed in uppercase or lowercase.

<b>Free-form</b> Code can be written freely without strict column requirements.

2. Data Types, Variables & Constants

2.1 Data Types

Data Type Suffix Description Example

Integer % Whole numbers –32,768 to 32,767 DIM x AS INTEGER

Long & Whole numbers –2,147,483,648 to 2,147,483,647


DIM n AS LONG

Single ! Single-precision floating point (7 digits) DIM pi AS SINGLE

Double # Double-precision floating point (15 digits) DIM d AS DOUBLE

String $ Text / characters DIM name AS STRING

Boolean — TRUE or FALSE (stored as –1 / 0) DIM flag AS INTEGER

2.2 Declaring Variables


Use DIM to declare variables before use:
DIM studentName AS STRING DIM age AS INTEGER DIM score AS SINGLE DIM marks(10) AS
INTEGER ' Array of 10 integers

2.3 Constants
Constants are values that do not change during program execution. Declared using CONST:
CONST PI = 3.14159 CONST SCHOOL_NAME = "Ghana Secondary School" CONST MAX_STUDENTS
= 50

Note: String constants are enclosed in double quotes. Numeric constants are written directly.
3. Operators & Expressions

3.1 Arithmetic Operators

Operator Operation Example Result

+ Addition 5+3 8

- Subtraction 10 - 4 6

* Multiplication 6*7 42

/ Division 15 / 4 3.75

\ Integer Division 15 \ 4 3

MOD Modulus (Remainder) 15 MOD 4 3

^ Exponentiation 2^8 256

3.2 Relational (Comparison) Operators

Operator Meaning

= Equal to

<> Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

3.3 Logical Operators


AND – Both conditions must be TRUE OR – At least one condition must be TRUE NOT –
Reverses the condition XOR – One condition TRUE, but not both

IF age >= 18 AND citizen = "Yes" THEN PRINT "Can vote"

3.4 String Concatenation


firstName$ = "Kwame" lastName$ = "Mensah" fullName$ = firstName$ + " " + lastName$
PRINT fullName$ 'Output: Kwame Mensah
4. Input and Output Statements

4.1 PRINT Statement


PRINT "Hello, World!" 'Prints text PRINT score 'Prints variable value PRINT "Name:
"; name$ 'Semicolon: no space between PRINT "Score:", score 'Comma: tab separation
PRINT 'Blank line

Semicolon vs Comma in PRINT:


Semicolon (;) – Output continues immediately after previous output (no gap).
Comma (,) – Output moves to next print zone (every 14 characters).

4.2 INPUT Statement


INPUT "Enter your name: ", name$ INPUT "Enter your age: ", age INPUT age 'No prompt
displayed

4.3 LINE INPUT


Used to accept a full line of text including commas and spaces:
LINE INPUT "Enter your address: ", address$

4.4 READ and DATA


READ assigns values from DATA statements to variables:
READ name$, score PRINT name$, score DATA "Ama", 85

4.5 CLS, LOCATE, COLOR


CLS 'Clears the screen LOCATE 5, 10 'Move cursor to row 5, column 10 COLOR 14, 1
'Foreground yellow (14), background blue (1)

5. Control Structures

5.1 IF…THEN…ELSE

Single-line IF:
IF score >= 50 THEN PRINT "Pass" ELSE PRINT "Fail"

Block IF (most common in WASSCE):


IF score >= 70 THEN PRINT "Distinction" ELSEIF score >= 50 THEN PRINT "Pass" ELSE
PRINT "Fail" END IF

5.2 SELECT CASE


Used when checking a variable against multiple values:
SELECT CASE grade CASE "A" PRINT "Excellent" CASE "B" PRINT "Very Good" CASE "C"
PRINT "Good" CASE ELSE PRINT "Below average" END SELECT

SELECT CASE with ranges:


SELECT CASE score CASE 80 TO 100 PRINT "Grade A" CASE 60 TO 79 PRINT "Grade B" CASE
IS < 60 PRINT "Grade C or below" END SELECT
6. Loops (Repetition Structures)

6.1 FOR…NEXT Loop


Used when the number of repetitions is known:
FOR counter = 1 TO 10 PRINT counter NEXT counter 'With STEP: FOR i = 0 TO 100 STEP
5 PRINT i NEXT i 'Counting down: FOR i = 10 TO 1 STEP -1 PRINT i NEXT i

6.2 WHILE…WEND Loop


Repeats while a condition is TRUE (pre-test loop):
count = 1 WHILE count <= 5 PRINT count count = count + 1 WEND

6.3 DO…LOOP

Four variations:
'Pre-test (checks before executing): DO WHILE condition 'statements LOOP 'Post-test
(executes at least once): DO 'statements LOOP WHILE condition 'DO UNTIL (loop until
condition is TRUE): DO UNTIL condition 'statements LOOP 'Exit early: DO WHILE x <
100 IF x = 50 THEN EXIT DO x = x + 1 LOOP

Key Difference: WHILE…WEND always checks at the top. DO…LOOP WHILE checks at the bottom and
always runs at least once.

7. Arrays (Subscripted Variables)


An array stores multiple values of the same type under one variable name.

7.1 Declaring Arrays


DIM marks(5) AS INTEGER 'Indices 0 to 5 (6 elements) DIM names(1 TO 30) AS STRING
'Indices 1 to 30 DIM matrix(3, 3) AS INTEGER '2D array (4x4)

7.2 Assigning and Accessing Values


marks(1) = 75 marks(2) = 88 PRINT marks(1) '75

7.3 Looping Through an Array


DIM scores(5) AS INTEGER total = 0 FOR i = 1 TO 5 INPUT "Enter score: ", scores(i)
total = total + scores(i) NEXT i PRINT "Average: "; total / 5
8. String Functions
Function Description Example Output

LEN(s$) Length of string LEN("WASSCE") 6

LEFT$(s$,n) Leftmost n characters LEFT$("Hello",3) "Hel"

RIGHT$(s$,n) Rightmost n characters RIGHT$("Hello",3) "llo"

MID$(s$,p,n) n chars from position p MID$("Hello",2,3) "ell"

UCASE$(s$) Convert to uppercase UCASE$("hello") "HELLO"

LCASE$(s$) Convert to lowercase LCASE$("HELLO") "hello"

LTRIM$(s$) Remove leading spaces LTRIM$(" hi") "hi"

RTRIM$(s$) Remove trailing spaces RTRIM$("hi ") "hi"

TRIM$(s$) Remove both-side spaces TRIM$(" hi ") "hi"

STR$(n) Number to string STR$(42) " 42"

VAL(s$) String to number VAL("42") 42

INSTR(s$,t$) Position of t$ in s$ INSTR("abcd","bc") 2

STRING$(n,c$) Repeat char n times STRING$(5,"*") "*****"

SPACE$(n) n spaces SPACE$(3) " "

CHR$(n) Character from ASCII CHR$(65) "A"

ASC(s$) ASCII code of 1st char ASC("A") 65

9. Numeric / Mathematical Functions


Function Description Example Output

ABS(x) Absolute value ABS(-7) 7

INT(x) Largest integer ≤ x INT(3.9) 3

CINT(x) Round to nearest integer CINT(3.5) 4

FIX(x) Truncate decimal part FIX(-3.9) -3

SQR(x) Square root SQR(16) 4

LOG(x) Natural logarithm (base e) LOG(1) 0

EXP(x) e raised to power x EXP(1) 2.71828

SIN(x) Sine (x in radians) SIN(0) 0

COS(x) Cosine (x in radians) COS(0) 1

TAN(x) Tangent (x in radians) TAN(0) 0

ATN(x) Arctangent ATN(1)*4 PI ≈ 3.14159

RND Random number 0 to <1 RND e.g. 0.7253

SGN(x) Sign: -1, 0, or 1 SGN(-5) -1


Random Integer Trick: INT(RND * n) + 1 gives a random integer from 1 to n. Use RANDOMIZE TIMER to
seed differently each run.

RANDOMIZE TIMER dice = INT(RND * 6) + 1 PRINT "You rolled: "; dice


10. Subroutines (SUB) and Functions (FUNCTION)

10.1 SUB Procedures


A SUB performs a task but does NOT return a value. Called using CALL or just by name.
'Declare / Define the SUB: SUB Greet(name AS STRING) PRINT "Hello, "; name END SUB
'Call the SUB: CALL Greet("Abena") Greet "Abena" 'Also valid

10.2 FUNCTION Procedures


A FUNCTION performs a calculation and RETURNS a value. The function name holds the return value.
FUNCTION Square(n AS INTEGER) AS INTEGER Square = n * n END FUNCTION 'Use it:
result = Square(5) PRINT result '25

10.3 Passing Arguments

Method Keyword Effect

By Reference (default) — Changes to parameter affect original variable

By Value BYVAL Original variable is protected from changes

SUB AddOne(BYVAL x AS INTEGER) x = x + 1 'Original variable unchanged END SUB

11. File Handling


QBasic can read from and write to sequential text files.

11.1 Opening a File


Mode Purpose Statement

OUTPUT Write to file (creates/overwrites) OPEN "[Link]" FOR OUTPUT AS #1

INPUT Read from file OPEN "[Link]" FOR INPUT AS #1

APPEND Add to end of file OPEN "[Link]" FOR APPEND AS #1

11.2 Writing to a File


OPEN "[Link]" FOR OUTPUT AS #1 PRINT #1, "Kofi" PRINT #1, 90 CLOSE #1

11.3 Reading from a File


OPEN "[Link]" FOR INPUT AS #1 DO WHILE NOT EOF(1) INPUT #1, name$, score
PRINT name$, score LOOP CLOSE #1

EOF(n) returns TRUE when end of file #n is reached. Always CLOSE files after use.
12. Common WASSCE-Style Programs

12.1 Sum & Average of N Numbers


INPUT "How many numbers? ", n total = 0 FOR i = 1 TO n INPUT "Enter number: ", num
total = total + num NEXT i PRINT "Sum: "; total PRINT "Average: "; total / n

12.2 Largest of Three Numbers


INPUT "Enter three numbers: ", a, b, c IF a > b AND a > c THEN PRINT "Largest is ";
a ELSEIF b > c THEN PRINT "Largest is "; b ELSE PRINT "Largest is "; c END IF

12.3 Multiplication Table


INPUT "Enter a number: ", n FOR i = 1 TO 12 PRINT n; " x "; i; " = "; n * i NEXT i

12.4 Grading System (with SELECT CASE)


INPUT "Enter your score: ", score SELECT CASE score CASE 80 TO 100: PRINT "Grade A
- Excellent" CASE 70 TO 79: PRINT "Grade B - Very Good" CASE 60 TO 69: PRINT "Grade
C - Good" CASE 50 TO 59: PRINT "Grade D - Pass" CASE 0 TO 49: PRINT "Grade F -
Fail" CASE ELSE: PRINT "Invalid score" END SELECT

12.5 Factorial Using a Loop


INPUT "Enter a positive integer: ", n factorial = 1 FOR i = 1 TO n factorial =
factorial * i NEXT i PRINT n; "! = "; factorial

12.6 Count Even and Odd Numbers


evenCount = 0 : oddCount = 0 FOR i = 1 TO 20 IF i MOD 2 = 0 THEN evenCount =
evenCount + 1 ELSE oddCount = oddCount + 1 END IF NEXT i PRINT "Even: "; evenCount
PRINT "Odd: "; oddCount
13. Quick Reference Card

13.1 Key Keywords Summary


Keyword Purpose

DIM Declare variables/arrays

CONST Declare constants

LET Assign value (optional in QBasic)

INPUT Get value from keyboard

PRINT Display output on screen

CLS Clear the screen

REM / ' Comment / remark (not executed)

IF…THEN…ELSE…END IF Conditional branching

SELECT CASE…END SELECT Multi-way branching

FOR…NEXT Count-controlled loop

WHILE…WEND Pre-test condition loop

DO…LOOP Flexible pre/post-test loop

SUB…END SUB Define a procedure (no return value)

FUNCTION…END FUNCTION Define a function (returns value)

CALL Invoke a SUB procedure

OPEN…AS Open a file

CLOSE Close a file

EOF(n) End-of-file check

RANDOMIZE TIMER Seed random number generator

END Terminate the program

GOTO Jump to a label (avoid in structured code)

EXIT FOR / EXIT DO Exit loop early

13.2 Common WASSCE Exam Tips


1. Always trace through programs step-by-step, tracking variable values in a table.
2. Know the difference between INT() (floor) and CINT() (rounding).
3. Remember: MOD gives the REMAINDER, \ gives INTEGER DIVISION result.
4. In FOR loops, the STEP can be negative to count downward.
5. WHILE…WEND checks condition BEFORE executing — if false from start, body never runs.
6. DO…LOOP WHILE checks AFTER — body executes at least once.
7. String subscript functions start counting characters from position 1, not 0.
8. MID$(s$, p, n) — p is start position, n is number of characters.
9. INSTR returns 0 if substring is NOT found.
10. A FUNCTION must assign its return value to the FUNCTION NAME inside the body.
11. Files must always be CLOSEd after use to save data properly.
12. Use RANDOMIZE TIMER before RND so numbers differ each run.
13. In SELECT CASE, CASE ELSE handles all unmatched values.
14. Array indices in QBasic default from 0; use OPTION BASE 1 or DIM arr(1 TO n) for 1-based.
15. Comments start with REM or a single quote (') and are ignored by the interpreter.

Good luck in your WASSCE exams! Study hard, practice writing programs by hand, and always
trace through your code carefully.

You might also like