IF...THEN is one of the most powerful features of QBASIC. Using IF...THEN can make your programs very interesting. It lets your program make decisions and do something appropriate.
IF Statements syntax in QBasics:
IF <conditions> THEN
Do something
ELSEIF <conditions> THEN
Do something
ELSE <conditions> THEN
do something
END IF
The IF and THEN commands
The IF and THEN commands are used to compare an expression and then perform some task based on that expression.
x = 5
IF x = 5 THEN PRINT "x equals 5"
Since X does equal 5 in this case, the program outputs:
x equals 5
Expression signs
You can also enter the following statements, instead of the equals sign:
x < 5 (x is less than 5)
x > 5 (x is greater than 5)
Run the following:
x = 16
IF (x > 5) THEN PRINT "x is greater than 5"
Output:
x is greater than 5
You can also combine the signs like this:
x <= 5 (x is less than or equal to 5)
x >= 5 (x is greater than or equal to 5)
x <> 5 (x does not equal 5)
Run the following example:
CLS
x = 5
IF (x >= 5) THEN PRINT "x is greater than or equal to 5"
IF (x <= 5) THEN PRINT "x is less than or equal to 5"
IF (x <> 5) THEN PRINT "x does not equal 5"
Output:
x is greater than or equal to 5
x is less than or equal to 5
ELSE
Using the ELSE command, you can have the program perform a different action if the statement
is false.
x = 3
IF x = 5 THEN PRINT "Yes" ELSE PRINT "No"
Since X doesn't equal 5, the output is:
No
END IF
END IF allows you to have multiple commands after the IF...THEN statement, but they must
start on the line after the IF statement. END IF should appear right after the list of commands.
x = 5
IF (x = 5) THEN
INPUT a$
PRINT a$
END IF
The following program uses ELSE with the END IF command:
x = 16
IF (x = 5) THEN
INPUT a$
PRINT a$
ELSE
PRINT x * 2
END IF
Output:
32
ELSEIF
The ELSEIF command allows you to perform a secondary action if the first expression was false.
Unlike ELSE, this task is only performed if a specified statement is true.
X = 6
IF (X = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (X = 6) THEN
PRINT "Statement 2 is true"
END IF
Output:
Statement 2 is true
You can have multiple ELSEIF commands, along with ELSE.
x = 8
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
ELSEIF (x = 7) THEN
PRINT "Statement 3 is true"
ELSE
PRINT "No above statements are true"
END IF
Output:
No above statements are true
Multiple expressions
You can have more than one expression in IF...THEN by using either the OR operator or the AND operator. The OR operator only requires one expression to be true in order to print "Yes" in the following program:
x = 20
IF (x = 5 OR x = 20) THEN PRINT "Yes"
Output:
Yes
The AND operator requires both expressions to be true.
x = 7
IF (x > 5 AND x < 10) THEN PRINT "True"
Output:
True
This is a slightly more complex example:
x = 16
y = 3
IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT "Correct"
Output: (since Y is 3)
Correct
Strings in IF...THEN
So far in this chapter, we've only been dealing with numbers, but you can also use strings with the IF...THEN command.
x$ = "Hello"
IF (x$ = "Hello" OR x$ = "World") THEN PRINT x$
Output:
Hello
You can also compare two variable strings:
x$ = "Hello"
y$ = "World"
IF (x$ <> y$) THEN PRINT x$; " "; y$
Output:
Hello World
Another Example: Run the following Programs
DIM FirstName AS STRING
DIM Num1 AS INTEGER
DIM Num2 AS INTEGER
CLS
INPUT "Enter First Name: ", FirstName
IF FirstName = "Mike" THEN
PRINT "You are not allowed beyond this screen"
ELSE PRINT "Welcome to QBasic, "; FirstName; ". How are you doing today?"
PRINT
INPUT "Enter First Number: ", Num1
INPUT "Enter Second Number: ", Num2
PRINT
PRINT Num1; "+"; Num2; "="; Num1 + Num2
END IF
Output: When you insert "Mike" as the name requested
Output: when another name is used.
Stay tuned for more Tutorials, kindly comment to show appreciation.
No comments:
Post a Comment