You are on page 1of 5

DIM FirstName AS STRING

DIM LastName AS STRING

DIM Num1 AS INTEGER

DIM Num2 AS INTEGER

CLS

INPUT Enter First Name: , FirstName

INPUT Enter Last Name: , LastName

PRINT Prints a line of nothing

PRINT Nice To Meet You, ; FirstName ; ; LastName

PRINT

INPUT Enter First Integer To Add: , Num1

INPUT Enter Second Integer To Add: , Num2

PRINT

PRINT Num1; + ; Num2; = ; Num1 + Num2

QBasic Tutorial 7 - IF Statements - QB64

If statements are used to check conditions in the program. The structure of the if statement is as
follows:

If <condition> Then

Do something

ElseIf <condition> Then

Do something>

Else

Do something

End If
The Expression Signs (Relational Operators) For If Statements Are:

< Less than <= Less than or equal to > Greater than >= Greater than or equal to

= Equal to <> Not Equal to

When the condition is met, the code associated with condition will trigger. For example, in a new file
type:

CLS

IF 5 > 2 THEN

PRINT 5 Is Greater Than 2

END IF

The output will be: 5 Is Greater Than 2

If the greater than changed to a less than, the computer will not output any information because the
condition is not met:

CLS

IF 5 < 2 THEN

PRINT 5 Is Less Than 2

END IF

In a new file type:

CLS

IF 5 > 10 THEN

PRINT 5 Is Greater Than 10

ELSE

PRINT 5 Is Not Greater Than 10

END IF

Since 5 is not greater than 10, the output will be:

5 Is Not Greater Than 10

The next example will prompt the user to enter two numbers. The program will check for the greatest
number and give a report on its findings.
DIM Num1 AS INTEGER

DIM Num2 AS INTEGER

CLS

INPUT "Enter First Number: ", Num1

INPUT "Enter Second Number: ", Num2

IF Num1 > Num2 THEN

PRINT Num1; "Is Greater Than"; Num2

ELSEIF Num2 > Num1 THEN

PRINT Num2; " Is Greater Than"; Num1

ELSE

PRINT The Numbers Are The Same

END IF

The output is dependent on the user input.

Select Case Statements -

Select-Case statements work like If statements. The difference is that the Select-Case statement can
make the code simpler to read and work with than If statements. In some computer languages and
computer systems, the Select-Case statements run faster than the If statements. Creating a Select-Case
statement is simple to do. The next program will prompt the user to select the key A-D and the program
will respond by telling the user what key was entered. We will create a Select-Case statement for the A-
D keys entered.

DIM KeyPressed AS STRING

CLS

PRINT

PRINT

INPUT "Please Enter A Key (A,B,C,D): ", KeyPressed

KeyPressed = UCASE$(KeyPressed)

PRINT
SELECT CASE KeyPressed

CASE "A"

PRINT "A Was Entered"

CASE "B"

PRINT "B Was Entered"

CASE "C"

PRINT "C Was Entered"

CASE "D"

PRINT "D Was Entered"

CASE ELSE

PRINT "Some Other Key Was Entered"

END SELECT

The next program asks for a score then prints the grade assigned to the score.

DIM Score AS INTEGER

CLS

PRINT

PRINT

INPUT "Enter The Test Score: ", Score

PRINT

SELECT CASE Score

CASE IS >= 97

PRINT "Grade A+"

CASE 93 TO 96

PRINT "Grade A"


CASE 90 TO 92

PRINT "Grade A-"

CASE 87 TO 89

PRINT "Grade B+"

CASE 83 TO 86

PRINT "Grade B"

CASE 80 TO 82

PRINT "Grade B-"

CASE 77 TO 79

PRINT "Grade C+"

CASE 73 TO 76

PRINT "Grade C"

CASE 70 TO 72

PRINT "Grade C-"

CASE 67 TO 69

PRINT "Grade D+"

CASE 63 TO 66

PRINT "Grade D"

CASE 60 TO 62

PRINT "Grade D-"

CASE ELSE

PRINT "Fail"

END SELECT

You might also like