IF … THEN Statement

It is the most simple form of the control statements which executes a block of statements only if the given expression or condition is true. If the condition is false, then the IF block will skipped and execution continues with the rest of the program.

Syntax :
IF [conditional expression] THEN [statement block]
OR
IF [conditional expression] THEN
[statement]
END IF

Example: 1

CLS
INPUT "Enter your marks : "; m
IF m >= 35 THEN PRINT "You are passed !!"
IF m < 35 THEN PRINT "You are failed .."
END

Example: 2

CLS
INPUT "Enter a number "; n
IF n MOD 2 = 0 THEN PRINT "It is Even Number"
IF n MOD 2 = 1 THEN PRINT "It is Odd Number"
END

IF … THEN …. ELSE Statement

It is also a common control statement which is used to execute the multiple statements depending on the condition. It is also called two way decision statement. In this statement if the condition is true the the statements after THEN will be executed and if the condition is false, the statements in the ELSE block will be executed.
Syntax :
IF [conditional expression] THEN
[statement 1]
ELSE
[statement 2]
END IF

Example: 1

CLS
INPUT "Enter your marks "; m
IF m >= 35 THEN
     PRINT "You are passed !!"
ELSE
     PRINT "You are failed .."
END IF
END

Example: 2

CLS
INPUT "Enter a number "; n
IF n MOD 2 = 0 THEN
     PRINT "Even Number"
ELSE
     PRINT "Odd Number"
END IF
END

IF … THEN … ELSEIF Statement

It is also same as IF .. THEN statement, but only difference is that, by using IF .. THEN statement we can test only one condition, and based on the condition given block of statement will be executed. But what ? If we have more then one condition? At that time we have to use IF … THEN … ELSEIF statement. It is also called multi way decision statement.
Syntax :
IF [condition] THEN
        [statement 1]
ELSEIF
        [statement 2]
ELSEIF
 [statement n]
……………………
…………………..
ELSE
        [statement n]
END IF

Example: 1

CLS
INPUT "Enter First Number "; x
INPUT "Enter Second Number "; y
INPUT "Enter Third Number "; z
IF x > y AND x > z THEN
     PRINT "Greatest number is "; x
ELSEIF y > x AND y > z THEN
     PRINT "Greatest number is "; y
ELSE
     PRINT "Greatest number is "; z
END IF
END

Example: 2

CLS
INPUT "Enter your percentage "; p
IF p >= 80 THEN
     div$ = "Distinction"
ELSEIF p >= 60 THEN
     div$ = "First Divistion"
ELSEIF p >= 40 THEN
     div$ = "Second Division"
ELSEIF p >= 35 THEN
     div$ = "Third Division"
ELSE
    div$ = "Fail"
END IF
PRINT "Division ::: "; div$
END

SELECT CASE Statement

Select-Case statements work like IF … THEN … ELSEIF statements. The difference is that the Select-Case statement can make the code simpler to read and work with than IF .. THEN … ELSEIF statements.

Syntax :
SELECT CASE
       CASE test 1
        [statement 2]
CASE test 2
 [statement 2]
……………………
…………………..
CASE ELSE
        [statement]
END SELECT

Example: 1

CLS
INPUT "Enter your percentage "; p
SELECT CASE p
CASE 80 TO 100
     div$ = "Distinction"
CASE 60 TO 80
     div$ = "First Division"
CASE 40 TO 60
     div$ = "Second Division"
CASE 35 TO 40
     div$ = "Third Division"
CASE ELSE
     div$ = "Fail"
END SELECT
PRINT "Division :: "; div$
END

Example: 2

CLS
INPUT "Enter First Number "; x
INPUT "Enter Second Number "; y
PRINT "****** MENU *******"
PRINT "1. Addition"
PRINT "2. Subtraction"
PRINT "3. Multiplication"
PRINT "4. Division"
INPUT "Enter your choice ! (1/2/3/4) "; ch
SELECT CASE ch
CASE 1
     PRINT "Sum = "; x + y
CASE 2
     PRINT "Difference = "; x - y
CASE 3
    PRINT "Product = "; x * y
CASE 4
    PRINT "Quotient = "; x / y
CASE ELSE
    PRINT "Incorrect choice !!!"
END SELECT
END

GOTO Statement

It is a only one unconditional branching statement in QBASIC Programming Language. It transfer the flow of program from one statement line to another statement line without checking any condition.

Syntax :     GOTO [line number / line label]
Example: 1

CLS
n = 1
top:
PRINT n;
n = n + 1
IF n <= 20 THEN GOTO top
END

Example: 2

CLS
INPUT "Enter your marks "; m
IF m >= 35 THEN GOTO p
IF m < 35 THEN GOTO f
p: PRINT "You are Passed"
END
f: PRINT "You are Failed!"
END

<< Table of Content >>