EXIT statement in QBASIC
This statement allows you to
exit from program pre-maturely from a block of statement in a control
structure, from a loop or procedure.
EXIT FOR Statement:
This statement is used to quit/exit
from a FOR…..NEXT loop before the condition is satisfied.
Example: 1
CLS LET k = 7 FOR i = 1 TO 15 PRINT k IF i = 10 THEN EXIT FOR r = k MOD 2 IF r = 0 THEN k = k / 2 ELSE k = k * 3 + 1 END IF NEXT END |
Output 7 22 11 34 17 52 26 13 40 20 |
Note: In above example loop is to be continued for 15 times
but when the value of ‘i’ is 10 the loop control will be out of the FOR…NEXT immediate even though the condition is not
satisfied.
EXIT DO Statement
This statement terminate the
loop statement immediately before the sentinel value becomes equal to initial
value.
Example: 2
REM
Example of DO EXIT statement CLS a
= 1 b
= 1 c
= 1 DO PRINT
a; b; a
= a + b b
= a + b c
= c + 1 n
= n + 1 IF
n = 5 THEN EXIT DO LOOP
WHILE c <= 15 END |
Output 1 1 2 3 5 8 13 21 34 55 |
Example 3 : Write a program to display the factors of input
number.
CLS
INPUT "Enter any
number"; n
LET k = 2
DO
r = n MOD k
IF r = 0 THEN
PRINT k;
IF k = n THEN EXIT DO
n = n / k
k = 2
ELSE
k = k + 1
END IF
LOOP WHILE n > 0
END
No comments:
Post a Comment