IF Statements in QBASIC
1. IF …. THEN Statement
IF THEN Statement
Or,
IF THEN
Block of statements
END IF
Where, condition is a
logical expression that evaluates either true or false.
2. IF….. THEN….. ELSE
Statement
Syntax:
IF THEN
Statement1
ELSE
Statement2
END IF
3. IF….. THEN…..
ELSEIF….. ELSE Statement
Syntax:
IF THEN
Statement1
ELSEIF THEN
Statement2
ELSEIF THEN
Statement3
…….
……….
………
……….
ELSE
Statement_N
END IF
REM find the greatest
number among any 10 numbers.
CLS
FOR j=1 TO 10
INPUT “Enter a number”;n
IF n>g THEN g=n
END IF
PRINT “The largest
number is “; g
END
REM finds the middle
number among any three numbers.
CLS
INPUT “Enter any three
numbers”; a, b, c
IF (a>b AND ac) THEN
PRINT a; “ is the middle number.”
IF (b>c AND ba) THEN
PRINT b; “ is the middle number.”
ELSEIF (c>a AND cb)
THEN
PRINT c; “is the middle number.”
END IF
END
SELECT CASE Statement
Syntax:
SELECT CASE
testexpression
CASE
expressionlist-1
[Statements Block-1]
[CASE
expressionlist-2
[Statements Block-2]]
………
……….
………
……….
[CASE ELSE
[Statements Block-N]]
END SELECT
REM counts total number
of vowels and consonants in a word.
CLS
INPUT “Enter a word“;
W$
R$= UCASE$(W$)
FOR K= 1 TO LEN(R$)
E$= MID$(R$, K, 1)
SELECT CASE E$
CASE “A”, “E”, “I”, “O”, “U”
x=x+1
CASE ELSE
y=y+1
END SELECT
NEXT K
PRINT “The number of
vowels in the word :”;x
PRINT “The number of
consonants in the word:”; y
END
Loops in QBASIC
The repetition of the
statement block time and again till the condition is satisfied is known as
loop. The looping statements supported by QBASIC program are
1. For Next Loop
Syntax:
FOR Variable = start TO
end STEP (increment/decrement)
[Block of statements]
NEXT Variable
REM displays first 10
even numbers and sum of those even numbers.
CLS
N=2
FOR P = 1 TO 10
PRINT N;
S=S+ N
N = N + 2
NEXT P
PRINT “Sum of the first
ten even numbers:”;S
END
2. WHILE …. WEND Loop
Syntax:
WHILE
[Statements block]
WEND
3. DO…LOOP
Syntax-I (Entry Control
Loop Structure):
DO WHILE|UNTIL
[Statements block]
LOOP
Syntax-II (Exit Control
Loop Structure):
DO
[Statements block]
LOOP WHILE|UNTIL
Where, Condition is the
logical expression that evaluates as true or false.
REM Sum of digits of an
integer
CLS
INPUT “Enter an
integer”; N
DO
R = N MOD 10
S =S+ R
N=INT(N/10)
LOOP WHILE N<>0
PRINT “Sum of digits”;
S
END
REM Sum of digits of an
integer
CLS
INPUT “Enter an
integer”; N
DO
R = N MOD 10
S =S+ R
N=INT(N/10)
LOOP UNTIL N=0
PRINT “Sum of digits”;
S
END
Nested Loop
The loop inside a loop
is called nested loop.
Syntax:
FOR Variable1 = start
TO final STEP (Increment or Decrement)
FOR Variable2 = start
TO final STEP (Increment or Decrement)
[Statements block]
NEXT Variable2
NEXT Variable1
CLS
FOR j= 1 TO 5
FOR k = 1 TO j
PRINT k;
NEXT k
PRINT
NEXT j
END
No comments:
Post a Comment