Comprehensive Note on QBasic programming
By: Mr. Evans Djimatey
Core QBASIC Programming Concepts QBasic control structure syntaxes:
🔁Control Structures 🔁 Looping Structures
a. Conditional Statements: 1️⃣ FOR...NEXT
i. IF...THEN, IF...THEN...ELSE,
IF...THEN...ELSEIF FOR counter = start TO end [STEP
ii. SELECT CASE for multiple conditions increment]
'Statements
NEXT counter
b. Loops:
i. FOR...NEXT
2️⃣ WHILE...WEND
ii. WHILE...WEND
iii. DO...LOOP WHILE condition
'Statements
📥 Input/Output Handling WEND
a. INPUT, LINE INPUT for user data 3️⃣ DO...LOOP
b. PRINT for displaying results
DO
c. CLS to clear the screen 'Statements
LOOP UNTIL condition
🧮 Math & String Functions
or
a. Math: SQR(), INT(), ABS(), EXP()
b. String: LEFT$(), RIGHT$(), MID$(), LEN(), DO WHILE condition
'Statements
CHR$(), ASC() LOOP
🧠 Advanced Concepts ✅ Conditional Structures
a. Arrays: Single and multi-dimensional 4️⃣ IF...THEN
b. Subroutines & Functions: SUB, FUNCTION,
CALL, DECLARE IF condition THEN
c. File Handling: OPEN, CLOSE, INPUT #, 'Statement
PRINT # END IF
d. Graphics & Sound: SCREEN, CIRCLE, LINE,
BEEP, PLAY 5️⃣ IF...THEN...ELSE
IF condition THEN
🧪 Practical Application Areas 'Statements if true
ELSE
Topic Sample Program 'Statements if false
Area of shapes Circle, rectangle, triangle END IF
Simple interest, compound
Finance 6️⃣ Nested IF
interest
Conversion Celsius to Fahrenheit IF condition1 THEN
Guess the number, palindrome IF condition2 THEN
Games
checker 'Nested block
END IF
String Separate letters, numbers, END IF
manipulation symbols
Comprehensive Note on QBasic programming
By: Mr. Evans Djimatey
7️⃣ IF...THEN...ELSEIF...ELSE
IF condition1 THEN 🔢 4. Count from 1 to N
'Statements
ELSEIF condition2 THEN
(WHILE...WEND Loop)
'Other statements
ELSE 1. INPUT "Enter N: ", N
'Fallback 2. I = 1
END IF 3. WHILE I <= N
4. PRINT I
5. I = I + 1
88️⃣SELECT CASE 6. WEND
SELECT CASE variable 🧮 5. Factorial of a Number
CASE value1 (DO...LOOP Until)
'Statements
CASE value2 TO value3
'Statements 1. INPUT "Enter a number: ", N
CASE ELSE 2. F = 1
3. DO
'Default block
4. F = F * N
END SELECT 5. N = N - 1
6. LOOP UNTIL N = 0
7. PRINT "Factorial: "; F
Here are 10 beginner-friendly QBasic programs
that demonstrate control structures like loops and 👨👩👧👦 6. Age Classification (SELECT CASE)
conditionals
1. INPUT "Enter age: ", Age
🔁 1. Multiplication Table (FOR...NEXT 2. SELECT CASE Age
3. CASE IS < 13
Loop) 4. PRINT "Child"
5. CASE 13 TO 19
6. PRINT "Teenager"
1. FOR I = 1 TO 10 STEP 1 7. CASE IS >= 20
2. PRINT "5 x "; I; " = "; 5 * I 8. PRINT "Adult"
3. NEXT I 9. END SELECT
🎯 2. Even or Odd Checker (IF...THEN) 🔎 7. Largest of Three Numbers (Nested IF)
1. INPUT "Enter a number: ", Num 1. INPUT "Enter A: ", A
2. IF Num MOD 2 = 0 THEN 2. INPUT "Enter B: ", B
3. PRINT "Even" 3. INPUT "Enter C: ", C
4. ELSE 4.
5. PRINT "Odd" 5. IF A > B THEN
6. END IF 6. IF A > C THEN
7. PRINT "A is largest"
8. ELSE
📈 3. Simple Interest Calculator 9. PRINT "C is largest"
(IF...THEN...ELSE) 10.
11. ELSE
END IF
12. IF B > C THEN
1. INPUT "Enter principal: ", P 13. PRINT "B is largest"
2. INPUT "Enter rate: ", R 14. ELSE
3. INPUT "Enter time: ", T 15. PRINT "C is largest"
4. IF P > 0 AND R > 0 AND T > 0 THEN 16. END IF
5. SI = (P * R * T) / 100 17. END IF
6. PRINT "Simple Interest: "; SI
7. ELSE
8. PRINT "Invalid input" 🔄 8. Reverse Count (FOR...NEXT with
9. END IF STEP -1)
10.
1. FOR I = 10 TO 1 STEP -1
Comprehensive Note on QBasic programming
By: Mr. Evans Djimatey
2. PRINT I 4. PRINT "Absolute Value: "; ABS(X)
3. NEXT I
🎰 9. Guess the Number Game (WHILE
Loop with IF)
🔤 String Function Programs
1. Target = 7
2. DO 5️⃣ Extract First 3 Characters from a String
3. INPUT "Guess a number: ", Guess (LEFT$())
4. IF Guess = Target THEN
5. PRINT "Correct!"
6. EXIT DO 1. INPUT "Enter your name: ", Name$
7. ELSE 2. PRINT "First 3 letters: "; LEFT$(Name$, 3)
8. PRINT "Try again"
9. END IF
10. LOOP WHILE Guess <> Target 6️⃣ Calculate Length of a Sentence (LEN())
📏 10. Sum of Even Numbers (FOR Loop 1. INPUT "Type a sentence: ", Line$
2. PRINT "Character count: "; LEN(Line$)
with IF condition)
1. Sum = 0
7️⃣ Extract Middle Part of a Word (MID$())
2. FOR I = 1 TO 20
3. IF I MOD 2 = 0 THEN 1. INPUT "Enter a word: ", Word$
4. Sum = Sum + I 2. PRINT "Middle 3 letters: "; MID$(Word$, 2, 3)
5. END IF
6. NEXT I
7. PRINT "Sum of even numbers: "; Sum 8️⃣ Combine All Three (LEFT$, LEN, MID$)
1. INPUT "Enter your school name: ", School$
2. PRINT "First letter: "; LEFT$(School$, 1)
🧮 Math Function Programs 3. PRINT "Total letters: "; LEN(School$)
4. PRINT "Middle part: "; MID$(School$, 2, 4)
1️⃣ Find the Square Root of a Number (SQR())
🔁 Mixed Programs (Math + String)
1. INPUT "Enter a number: ", Num
2. PRINT "Square root is: "; SQR(Num)
9️⃣ Length of a Name and Root of Its Length
2️⃣ Round Down a Decimal to Nearest Integer 1. INPUT "Enter name: ", N$
(INT()) 2. L = LEN(N$)
3. PRINT "Length: "; L
4. PRINT "Square root of length: "; SQR(L)
1. INPUT "Enter a decimal number: ", Val
2. PRINT "Rounded down: "; INT(Val)
🔟 Check Character Count and Round It Down
3️⃣ Find the Absolute Value (ABS())
1. INPUT "Write a phrase: ", Phrase$
2. Count = LEN(Phrase$)
1. INPUT "Enter a number: ", N 3. PRINT "Total characters: "; Count
2. PRINT "Absolute value: "; ABS(N) 4. PRINT "Rounded down: "; INT(Count)
4️⃣ Root + Rounded + Absolute Combined
Arrays
1. INPUT "Enter a number: ", X
2. PRINT "Square Root: "; SQR(X)
3. PRINT "Rounded Down: "; INT(X) 1️⃣ Single-dimensional Array
Comprehensive Note on QBasic programming
By: Mr. Evans Djimatey
1. DIM scores(5) 7. result = result * i
2. scores(1) = 78 8. NEXT i
3. scores(2) = 85 9. Factorial = result
4. PRINT scores(2) 10. END FUNCTION
Array Example: Calculating Average of Test
Scores
1. DIM testScores(4)
2. testScores(1) = 72 📁 File Handling
3. testScores(2) = 88
4. testScores(3) = 94
5. testScores(4) = 76 5️⃣ OPEN, INPUT #, CLOSE
6.
7. total = 0
8. FOR i = 1 TO 4 1. OPEN "[Link]" FOR INPUT AS #1
9. total = total + testScores(i) 2. INPUT #1, username$
10. NEXT i 3. PRINT "Hello, "; username$
11. 4. CLOSE #1
12. average = total / 4
13. PRINT "Average score is: "; average
6️⃣ OPEN, PRINT #, CLOSE
1. OPEN "[Link]" FOR OUTPUT AS #2
2️⃣ Multi-dimensional Array 2. PRINT #2, "Session started"
3. CLOSE #2
1. DIM marks(3, 2)
2. marks(1, 1) = 75
3. marks(1, 2) = 80 🎨🔊 Graphics & Sound
4. PRINT marks(1, 2)
7️⃣ SCREEN
🧩 Subroutines & Functions
1. SCREEN 12 ' Switch to high-res graphics mode
3️⃣ SUB...CALL
8️⃣ CIRCLE
1. DECLARE SUB Greet()
2. CALL Greet
1. CIRCLE (320, 240), 50 ' Draw circle at center
3.
of screen
4. SUB Greet
5. PRINT "Welcome to QBasic!"
6. END SUB
9️⃣ LINE
4️⃣ FUNCTION 1. LINE (100, 100)-(200, 200), 4 ' Draw line
with color 4
1. DECLARE FUNCTION AddNums(x, y)
2. PRINT AddNums(5, 3)
3.
🔟 BEEP
4. FUNCTION AddNums(x, y)
5. AddNums = x + y BEEP ' Simple tone from the PC speaker1.
6. END FUNCTION
1️⃣1️⃣PLAY
Function Example: Calculating Factorial
PLAY "CDEFGAB" ' Play musical notes1.
1. DECLARE FUNCTION Factorial(n)
2. PRINT Factorial(5) ' Output: 120
3.
4. FUNCTION Factorial(n)
5. result = 1
6. FOR i = 1 TO n