Chapter 7: Statements in QBasic

Notes — Chapter 7: Statements in QBasic | CBSE Class 7 Computer

💡 1. Fundamentals of QBasic

  1. QBasic stands for Quick Beginners All-purpose Symbolic Instruction Code.
  2. It is a high-level programming language developed by Microsoft for MS-DOS in 1991.
  3. QBasic is an interpreter-based language that executes programs line by line.
  4. Each QBasic program is made of statements telling the computer what to do.

📘 2. Some Basic Statements in QBasic

StatementPurposeSyntaxExample
LETAssigns a value to a variableLET variable = expressionLET A = 10
PRINTDisplays outputPRINT expressionPRINT "HELLO"
INPUTTakes input from userINPUT "Prompt", variableINPUT "Enter name"; N$
ENDStops executionENDEND
REMAdds commentsREM commentREM This is a comment

⚙️ 3. Control Statements in QBasic

➥ Control statements change the sequence of execution in a program. They are of two types:

  1. Conditional Control Statements
  2. 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

TypeStatementPurpose
ConditionalIF...THENExecutes statement if true
ConditionalIF...THEN...ELSEChooses one of two actions
ConditionalSELECT CASETests variable for many values
LoopingFOR...NEXTFixed repetitions
LoopingWHILE...WENDRepeats while true
LoopingDO...LOOPRepeats until/while true

😊
🔔 Subscribe to My Channel

Post a Comment

0 Comments