Nested Select case in QBASIC
In QBASIC, you can use the SELECT CASE statement to
perform different actions based on the value of an expression. You can nest
SELECT CASE statements by placing one SELECT CASE block inside another. Here is
an example of how you can use nested SELECT CASE statements in QBASIC.
Syntax:
SELECT CASE expression1
CASE value1
SELECT CASE
expression2
CASE
value2
statements
CASE
value3
statements
CASE
ELSE
statements
END SELECT
CASE value4
SELECT CASE
expression3
CASE
value5
statements
CASE
value6
statements
CASE
ELSE
statements
END SELECT
CASE ELSE
statements
END SELECT
Note: Here, the inner SELECT CASE statement will be
executed based on the value of expression2, and the outer SELECT CASE statement
will be executed based on the value of expression1.
Example: 1
INPUT "Enter a number:", num
SELECT CASE num
CASE 1
PRINT
"You entered 1."
CASE 2
SELECT CASE
num
CASE 2
PRINT "You entered 2."
CASE
ELSE
PRINT "You did not enter 1 or 2."
END SELECT
CASE 3
SELECT CASE
num
CASE 3
PRINT "You entered 3."
CASE
ELSE
PRINT "You did not enter 1, 2, or 3."
END SELECT
CASE ELSE
PRINT
"You did not enter a valid number."
END SELECT
In above
example, the outer SELECT CASE block evaluates the value of num. If num
is equal to 1, the program prints "You entered 1." If num is equal to
2, the program enters the first inner SELECT CASE block. If num is equal to 3,
the program enters the second inner SELECT CASE block. If num is not equal to
any of the specified values, the program prints "You did not enter a valid
number."
Output:
Example 2
DIM letter AS STRING
INPUT "Enter a letter:", letter
SELECT CASE letter
CASE "A", "a"
PRINT "You entered A."
CASE "B", "b"
SELECT CASE letter
CASE "B", "b"
PRINT "You entered
B."
CASE ELSE
PRINT "You did not enter A
or B."
END SELECT
CASE "C", "c"
SELECT CASE letter
CASE "C", "c"
PRINT "You entered
C."
CASE ELSE
PRINT "You did not enter
A, B, or C."
END SELECT
CASE ELSE
PRINT "You did not enter a valid letter."
END SELECT
In above example, the outer SELECT CASE block
evaluates the value of letter. If letter is equal to "A", the program
prints "You entered A." If letter is equal to "B", the
program enters the first inner SELECT CASE block. If letter is equal to
"C", the program enters the second inner SELECT CASE block. If letter
is not equal to any of the specified values, the program prints "You did
not enter a valid letter."
Output:
No comments:
Post a Comment