QBAIC LOOP
The term looping means repeating execution of a sequence of statements in a program. The loop is repeated as long as the given condition is satisfied. It is terminated when given condition is not satisfied. A loop becomes endless loop if it is not terminated. In QBASIC there are several statements for creating a loop like:- FOR... NEXT
- WHILE.... WEND
- DO.... LOOP
It repeats a statement or a block of statements for given number of times which is written in between FOR and NEXT.
Syntax:
FOR<counter>=<Initial value>TO <sentinel value><Step+/-n>
Statement/ Statement blocks
NEXT<counter>
Here, counter is a numeric variable that controls the loop. It is also called control variable. The execution of the loop begins with assigning the initial and sentinel value with the step to increase or decrease the value of the counter variable by n. If step is not defined then by default QBASIC assumes STEP as '1' and will increase the value of the counter variable by 1.
REM Program to print first 20 natural numbers horizontally.
CLS
FOR I= 1TO 20
PRINT I;
NEXT I
END
REM Program to print Odd numbers from 49 to 5.
CLS
FOR I=49 TO 5 step -2
PRINT I;
NEXT I
END
REM Program to print your school's name for 20 times.
CLS
INPUT "Enter your school's name"; N$
FOR I= 1 to 20
PRINT "My school's name is "; N$
NEXT I
END
EXIT FOR Statement
This statement exits a FOR loop.
Syntax:
EXIT FOR
Example:
FOR A= 1 TO 20 STEP 1
PRINT A;
IF A+ 14 THEN EXIT FOR
NEXT A
WHILE... WEND Loop
This kind of looping structure executes a series of statements as long as the given condition is true. If the condition is false, then the loop will skip and other statements following WHILE... WEND statements will be executed. The 'WHILE' keyword id followed by the condition.
Syntax:
WHILE<condition>
Statements/ Statements block
WEND
REM Program to print Even numbers from 2 to 50.
CLS
I= 2
WHILE I<= 50
PRINT I;
I + I+2
WEND
END
REM Program to generate the series 7, 14, 21.....70.
CLS I= 7
WHILE I<70
PRINT I;
I=I+7
WEND
END
REM Program to print sum of the numbers up to the entered number.
CLS
INPUT "Enter any number"; N
I = 1
S= 0
WHILE I<= N
S= S+1
I= I+1
WEND
PRINT S
END
DO... LOOP
It repeats a statement or a block of statements while a condition is true or until a condition becomes false. The WHILE or UNTIL keyword(any one of them) is used with DO... LOOP. If we use "WHILE" the loop terminates when the given condition evaluates to be false whereas if we use "UNTIL" the loop terminates when the given condition evaluates to be free.
Syntax:
DO{WHILE/ UNTIL}
Statements or Statements block
LOOP
Or
DO
Statements or Statements block
LOOP{WHILE/ UNTIL}
Example:
Program to print first 20 odd numbers.
a. Using DO WHILE .. LOOP
CLS
I= 1
DO WHILE I<=19
PRINT I
I=I+2
LOOP
END
b. Using DO UNTIL .. LOOP
CLS
I=1
DO UNTIL I>19
PRINT I
I=I+2
LOOP
END
c. Using DO... LOOP WHILE
CLS
I= 1
DO
PRINT I
I = I+2
LOOP WHILE I<=19
END
d. Using DO .. LOOP UNTIL
CLS
I=1
DO
PRINT I
I=I+2
LOOP UNTIL I>19
END
EXIT DO Statement
This statements ends a DO loop.
Syntax:
EXIT DO
Example:
N=1
DO
PRINT N;
IF N= 8
THEN EXIT DO
N =N+1
LOOP WHILE N<=10
NESTED LOOP
A loop within a loop is called nested loop. One kind of looping statement can contain another same or different types of statement. The looping statements can be nested at any level deep. We can make nested loop of any looping statements.
Syntax:
FOR <counter>= <Initial value> TO <sentinel value> [step increment/ decrement]
[statement block]
FOR<counter2>=<Initial value>TO<Sentinel value> [step increment/ decrement]
[statement block]
Next[counter2
[Statement block]
Next[counter1]
Example:
Program to print the following series.
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
CLS
FOR I= 5 TO STEP -1
FOR J= 5 TO I STEP -1
PRINT I;
NEXT J
PRINT
NEXT I
END
Program to generate the following series.
3, 33, 333, 3333, 33333
CLS
A=3
FOR I = 1 TO 5
PRINT A;
A= A*10 + 3
NEXT I
END
0 Comments
If you have any doubts, please let me know.