0% found this document useful (0 votes)
6 views6 pages

QBasic Looping Statements Explained

Uploaded by

shahid
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)
6 views6 pages

QBasic Looping Statements Explained

Uploaded by

shahid
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

📘 Topic: Looping Statements (Page 1)

What is Looping / Iteration?

Looping means:

Repeating the same task again and again until a condition becomes false.

Example in real life:


👉 Writing numbers from 1 to 10 without writing PRINT 10 times.

The PDF says that QBasic has three main looping statements:

1. FOR … NEXT
2. WHILE … WEND
3. DO … LOOP

Among these, FOR…NEXT is the most common loop.

🔁 FOR … NEXT Loop (Page 1–2)


What is FOR…NEXT?

 Used when the number of repetitions is already known


 Automatically increases or decreases the counter

Syntax explained:
FOR x = start TO end STEP n
statements
NEXT x

 x → loop control variable


 start → starting value
 end → ending value
 STEP n → increase/decrease value (optional, default = 1)

Example 1: Natural numbers 1 to 10


FOR x = 1 TO 10
PRINT x;
NEXT x

✔ Prints numbers from 1 to 10


✔ Runs exactly 10 times
Example 2: Even numbers 1 to 20
FOR i = 2 TO 20 STEP 2
PRINT i;
NEXT i

✔ Starts from 2
✔ Jumps by 2 (even numbers)

Example 3: Descending order (20 to 1)


FOR a = 20 TO 1 STEP -1
PRINT a;
NEXT a

✔ STEP -1 means counting backward

Example 4: Print name 10 times


FOR i = 1 TO 10
PRINT "Your Name"
NEXT i

✔ Loop runs 10 times


✔ Same text printed repeatedly

Example 5: Multiplication table


INPUT "Enter a number "; n
FOR c = 1 TO 10
PRINT n; "x"; c; "="; n * c
NEXT c

✔ Uses loop for table


✔ Avoids writing 10 lines manually

🔁 WHILE … WEND Loop (Page 3–4)


What is WHILE…WEND?

 Used when the number of repetitions is not fixed


 Loop runs as long as the condition is TRUE
 Condition is checked before entering the loop
Syntax:
WHILE condition
statements
WEND

Example 1: Natural numbers 1 to 10


n = 1
WHILE n <= 10
PRINT n;
n = n + 1
WEND

✔ Manual increment required


✔ If increment is missing → infinite loop ❌

Example 2: Even numbers


n = 2
WHILE n <= 20
PRINT n;
n = n + 2
WEND

Example 3: Descending order


n = 20
WHILE n >= 1
PRINT n;
n = n - 1
WEND

Example 4: Print name 10 times


n = 1
WHILE n <= 10
PRINT "Your Name"
n = n + 1
WEND

Example 5: Multiplication table


i = 1
INPUT "Enter any number "; n
WHILE i <= 10
PRINT n; "x"; i; "="; i * n
i = i + 1
WEND
🔁 DO … LOOP (Page 4–6)
What is DO…LOOP?

 Loop runs at least once


 Condition can be checked at the start or at the end
 More flexible than WHILE

Four Syntax Types (as in PDF):

1️⃣ DO … LOOP WHILE


2️⃣ DO WHILE … LOOP
3️⃣ DO … LOOP UNTIL
4️⃣ DO UNTIL … LOOP

Example 1: Natural numbers


n = 1
DO
PRINT n;
n = n + 1
LOOP WHILE n <= 10

✔ Loop body executes first


✔ Condition checked at the end

Example 2: Even numbers


n = 2
DO
PRINT n;
n = n + 2
LOOP WHILE n <= 20

Example 3: Descending order using UNTIL


n = 20
DO
PRINT n;
n = n - 1
LOOP UNTIL n < 1

✔ UNTIL means loop stops when condition becomes TRUE

Example 4: Name 10 times


n = 1
DO WHILE n <= 10
PRINT "Your Name"
n = n + 1
LOOP

Example 5: Multiplication table


i = 1
INPUT "Enter any number "; n
DO WHILE i <= 10
PRINT n; "x"; i; "="; i * n
i = i + 1
LOOP

🔁 NESTED LOOP (Page 6–7)


What is a Nested Loop?

A loop inside another loop

 Outer loop → rows


 Inner loop → columns

Example 1: Outer & Inner loop text


FOR i = 1 TO 4
PRINT "This is an outer loop "; i
FOR j = 1 TO 3
PRINT "Inner Loop "; j
NEXT j
PRINT
NEXT i

Example 2: Number pattern


FOR i = 1 TO 5
FOR j = 1 TO i
PRINT j;
NEXT j
PRINT
NEXT i

Output:

1
12
123
1234
12345

Other examples show:


 Printing i instead of j
 Reverse triangle using STEP -1

📝 Final Exam Memory Lines


 FOR…NEXT → fixed number of times
 WHILE…WEND → condition checked first
 DO…LOOP → runs at least once
 Nested loop → loop inside loop

You might also like