💡 1. Fundamentals of QBasic
- QBasic stands for Quick Beginners All-purpose Symbolic Instruction Code.
- It is a high-level programming language developed by Microsoft for MS-DOS in 1991.
- QBasic is an interpreter-based language that executes programs line by line.
- Each QBasic program is made of statements telling the computer what to do.
📘 2. Some Basic Statements in QBasic
| Statement | Purpose | Syntax | Example |
|---|---|---|---|
| LET | Assigns a value to a variable | LET variable = expression | LET A = 10 |
| Displays output | PRINT expression | PRINT "HELLO" | |
| INPUT | Takes input from user | INPUT "Prompt", variable | INPUT "Enter name"; N$ |
| END | Stops execution | END | END |
| REM | Adds comments | REM comment | REM This is a comment |
⚙️ 3. Control Statements in QBasic
➥ Control statements change the sequence of execution in a program. They are of two types:
- Conditional Control Statements
- Looping (Repetition) Statements
🧩 A. Conditional Control Statements
➯Used to test a condition.
➯ If the condition is true, a statement is executed; otherwise, it is skipped.
(i) IF...THEN Statement
IF condition THEN statement
Example: IF A > B THEN PRINT "A is greater"
(ii) IF...THEN...ELSE Statement
➯ Used when there are two possible actions (True or False).
IF condition THEN
statement1
ELSE
statement2
END IF
➭ Example:-
IF A > B THEN PRINT "A is greater" ELSE PRINT "B is greater" END IF
(iii) IF...THEN...ELSEIF...ELSE Statement
➯Used when there are multiple conditions to be tested one after another.
IF condition1 THEN
statement1
ELSEIF condition2 THEN
statement2
ELSE
statement3
END IF
➭ Example:-
IF marks >= 90 THEN PRINT "Grade A" ELSEIF marks >= 75 THEN PRINT "Grade B" ELSEIF marks >= 50 THEN PRINT "Grade C" ELSE PRINT "Fail" END IF
(iv) SELECT CASE Statement
➯ Used when we want to test one variable for many possible values.
SELECT CASE variable
CASE value1
statement1
CASE value2
statement2
CASE ELSE
statement3
END SELECT
➭ Example:-
SELECT CASE day$ CASE "Mon" PRINT "Monday" CASE "Tue" PRINT "Tuesday" CASE ELSE PRINT "Invalid day" END SELECT
🔁 B. Looping Statements in QBasic
➥ Looping means repeating a set of instructions until a condition is met.
(i) FOR...NEXT Loop
➯ Executes a block of statements a fixed number of times.
FOR counter = start TO end
statement(s)
NEXT counter
➭ Example:-
FOR I = 1 TO 5 PRINT I NEXT I
(ii) FOR...NEXT with STEP
➯ Used to increase or decrease the counter by a specific value.
FOR counter = start TO end STEP value
statement(s)
NEXT counter
➭ Example:-
FOR I = 2 TO 10 STEP 2 PRINT I NEXT I (Prints even numbers 2, 4, 6, 8, 10)
(iii) Nested FOR...NEXT
➯ When one loop is inside another loop.
FOR i = 1 TO n
FOR j = 1 TO m
statement(s)
NEXT j
NEXT i
➭ Example:-
FOR i = 1 TO 3 FOR j = 1 TO 3 PRINT i; j NEXT j NEXT i
(iv) WHILE...WEND Loop
➯ Executes a block of statements as long as a condition is true.
WHILE condition
statement(s)
WEND
➭ Example:-
N = 1 WHILE N <= 5 PRINT N N = N + 1 WEND
(v) DO...LOOP
➯ Repeats statements while or until a condition is true/false.
DO
statement(s)
LOOP WHILE condition
➭ Example:-
DO WHILE condition statement(s) LOOP
🧠 Summary Table
| Type | Statement | Purpose |
|---|---|---|
| Conditional | IF...THEN | Executes statement if true |
| Conditional | IF...THEN...ELSE | Chooses one of two actions |
| Conditional | SELECT CASE | Tests variable for many values |
| Looping | FOR...NEXT | Fixed repetitions |
| Looping | WHILE...WEND | Repeats while true |
| Looping | DO...LOOP | Repeats until/while true |

0 Comments