Conditional
or Branching statement
A conditional statement transfers the control from one
part to another by testing the condition.
IF…THEN Statement:
IF-THEN statement control flows
according to the given condition. IF the condition is true the statement of the IF
block is executed. If it is not true, the statement in the IF block is not
executed.
Single Line IF…. THEN
statement
This statement is written in a single line.
This statement terminates itself when the entire line will be finished so that it
does not require an END IF statement like block IF…THEN…ELSE.
Syntax: IF condition THEN statement_for_true
Example:
To input any number and displayed
whether input number is odd or even.
CLS
INPUT “Enter any no”;n
r=n MOD 2
IF r=0 THEN PRINT “Even no”
IF r=1 THEN PRINT “Odd no”
END
IF…THEN...ELSE Statement:
It
is a two-way branching statement. If the condition is true the statement in IF
block is executed, if it is not true then the statement in the ELSE block is
executed.
Syntax: (Single line IF….ELSE)
IF
<condition> THEN <statement> ELSE <statement>
OR
IF
(condition) THEN
Statement
ELSE
Statement
END
IF
Example:1
WAP to input
any number and check whether the input number is odd or even
CLS
INPUT
"enter any number="; nu
IF nu MOD 2 = 0 THEN PRINT "Number is even"
ELSE PRINT "Number is Odd"
END
Example 2:
CLS
INPUT
"enter any number="; n
r
= n MOD 2
IF
r = 0 THEN
PRINT
"Even no"
ELSE
PRINT
"Odd no"
END
IF
END
IF…ELSEIF...ELSE Statement:
It
is a multi-way branching statement. This statement is useful when there are
motile conditions we have to check. This statement can be used when there is
more than one condition to test. This statement tests the first condition, if
the condition is correct then its respective code will be executed, if not
correct the previous condition, then it will check the second condition, if it
is correct then it execute its respective block, if the second condition is not
also correct then the else code will be executed. This block end with END
IF" line code.
Syntax:
IF
(condition 1) THEN
[statement
block 1]
ELSEIF
(condition 2) THEN
[statement block 2]
…
ELSE
[statement block n]
END IF
Example:
Program
to check whether input number is odd or even
CLS
INPUT "enter any no"; n
r = n MOD 2
IF r = 0 THEN
PRINT "even"
ELSEIF r = 1 THEN
PRINT "odd"
ELSE
PRINT "invalid no"
END IF
END
SELECT CASE Statement:
It is a multi-branching conditional statement that controls the
branching to one of the several statements based on the value of an expression.
Syntax:
SELECT
CASE test expression
CASE
expression 1
[statement
block 1]
CASE
expression 2
[statement
block 2]
CASE
expression 3
[statement
block 3]
…………………….
CASE
ELSE
[statement
block]
END SELECT
Example
1: Program to check whether input number is odd or even
CLS
INPUT "Enter any no"; n
r = n MOD 2
SELECT CASE r
CASE 1
PRINT "odd no"
CASE 0
PRINT "even no"
CASE ELSE
PRINT "invalid no"
END SELECT
END
Example
2: To
check the grade from the input mark
CLS
INPUT “Enter marks : “
; m
SELECT CASE m
CASE 90 TO 100
PRINT
“A+”
CASE 80 TO 89.9
PRINT
“A”
CASE 70 TO 79.9
PRINT
“B+”
CASE 60 TO 69.9
PRINT
"B"
CASE
50 to 59.9
PRINT
"C+"
CASE
40 TO 49.9
PRINT
"C"
CASE
35 TO 39.9
PRINT
"D"
CASE
ELSE
PRINT
"NG"
END SELECT
END
Note: Always write smaller to
bigger value, when give expression (CASE 90 TO 100)
which indicates range.
Example: Program to find the area of
circle, area or rectangle and area of triangle with the help of menu structure.
CLS
PRINT
"c. Area of circle"
PRINT
"r. area of rectangle"
PRINT
"t. Area of triangle"
INPUT
"Enter your choice"; ch$
SELECT
CASE ch$
CASE
"C", "c"
INPUT
"Emter radois"; r
a
= 3.14 * r * r
PRINT
"Area of circle"; a
CASE
"R", "r"
INPUT
"Enter length:"; l
INPUT
"Enter breadth"; b
ar
= l * b
PRINT
"Area of rectangle"; ar
CASE
"T", "t"
INPUT
"Enter base:"; b
INPUT
"Enter height:"; h
at
= 1 / 2 * b * h
PRINT
"Area of triangle:"; at
CASE
ELSE
PRINT
"invalid choice"
END
SELECT
END
You may also Read
QBASIC program to count the total number of vowel from input string
Thank you for creating a space to provide understanding in these concept in Qbasic. Please i would like you to help me with NESTING SELECT CASE statement
ReplyDeleteThank you for reply. For nested select case please go through.....
ReplyDeletehttps://neb-see-solution.blogspot.com/2022/12/nested-select-case-statement-qbasic.html