Before going into details for each Keywords and Functions on QBasic, I like to put out a quick review. You can also call it a little summary. Later on, you can check back for better explained posts with examples.

**OUTLINE** ## LINE NUMBERING

Qbasic program requires numbering for each line. It must be positive integers (not having decimals).

E.g.

INTEGERS = 1, 05, 09, 10, 20, etc.

## REMARKS

This is used to make invisible comments (won't show when the program is run) meant to explain the concept of the program. Usually placed at the beginning of the program.

E.g.

Remarks This is a Qbasic program.

**Remarks can also be written as Rem.** E.g.

Rem Y in the program means year.

## END and STOP

Both terminates the program. But END is placed at the end of the program while STOP can be placed anywhere. If the program executes that line where the STOP is, it terminates the program.

## SOME FUNCTIONS

- SQR(X) means square root of X.
- ABS(X) means absolute value of X (positive).
- SGN(X) sign of X. E.g. - if X<0, 0 if X=0 and + if X>0.
- RND(X) means generate random mind between 0 and X.
- EXP(X) means exponential of X.
- We also have SIN(X), COS(X), TAN(X), LOG(X), MOD.

E.g.

10 LET X = 25

20 LET ANS = SQR(X)

30 PRINT ANS

40 END

**MOD:** This helps to find a remainder in a division. E.g.

- 4 MOD 2 = 0
- 5 MOD 2 = 1
- 11 MOD 4 = 3

This come in handy for checking leap year. A Leap year is divisible by 4 without a remainder. E.g. 1960, 2000, 2004, 2012, 2016, etc.

E.g.

40 cal = Year MOD 4

50 If cal = 0 THEN 60

55 GOTO 80

60 PRINT "LEAP YEAR"

70 GOTO 90

80 PRINT "NOT LEAP YEAR"

90 END

**POWER:** To raise to power, we use

****** or

**^** (like in calculator).

E.g. 2**3 = 8 or 2^3 = 8.

## VARIABLES

Variables are used for values. It can be alphabets, alphabet with number or underscore but not numbers only and shouldn't have spacing.

E.g. X, A, A1, name, year, age, root1, root2, neg_sum, etc.

## INPUT and PRINT

Input reads in values from the outside environment (user) while Print displays values or characters to the user.

10 INPUX X

20 PRINT "YOU ENTERED", X

25 PRINT "The square of X is"; X^2

30 END

## READ and DATA

Data is used to store values in the program while Read reads them.

E.g.

10 Data 343 310 566

20 Read w,x,y,z

END

This will make w = 343, x = 310, y = 566 but z has no value.

## LET

LET is use to assign values or formulae.

E.g.

10 Let A = 3

15 Let B = 2

20 LET C = A + B

30 PRINT C

E.g. For quadratic formula,

10 INPUT a, b, c

15 LET d = b**2 - 4*a*c

20 LET root1 = -b + SQR(d)/(2*a)

25 LET root2 = -b - SQR(d)/(2*a)

30 PRINT " The roots are", root1; root2

40 END

## CONTROL STATEMENTS

Computer executes the program line by line. Control statements are used to change this by controlling its movement. They are sometimes used for looping.

- GOTO (unconditional)
- IF-THEN
- ON-GOTO

E.g.

10 INPUT A, B

20 IF A=B THEN 50

30 PRINT "A IS NOT B"

40 GOTO 60

50 PRINT "A IS B"

60 END

The GOTO 60 prevents the computer from executing line 50 after executing line 30 (because that will result to an error).

10 ON X GOTO 50, 80, 120, 120

The above tells the compiler to GOTO line(s):

- 50 if X ranges from is 1 to 2
- 80 if X ranges from 2 to 3
- 120 if X ranges from 3 to 4
- 120 if X ranges from 4 to 5

15 ON A/10 GOTO 50, 100, 140.

The above tells the compiler to GOTO line(s):

- 50 if A ranges from 10 to 19
- 100 if A ranges from 20 to 29
- 140 if A ranges from 30 to 39

# ADDITIONAL KEYWORDS

The Above are basic keywords and operations that we use in QBasic. The following are additional keywords that are also used oftentimes.

## FOR-NEXT (looping)

E.g.

10 FOR X = 1 TO 3

20 PRINT X, X**2

30 NEXT X

The above program computes loops from 1 to 3 displaying X and the square of X for each repetition. The output will look like this:

1 1

2 4

3 9

By default a For-Next loop increases count by one. You can change this by adding a

**STEP**.

E.g.

10 FOR X = 1 TO 10 STEP 2

20 PRINT X

30 NEXT X

The above program computes even numbers between 1 and 10. The output will be 2, 4, 6, 8 and 10.

## DEF (to define your own functions)

E.g.

10 DEF FNA(R) = 3.142 * R**2

20 PRINT "ENTER RADIUS"

30 INPUT Y

40 PRINT "AREA ="; FNA(Y)

## DIM (dimension, used for arrays)

E.g.

10 DIM X(10), Y(10,5)

X becomes an an array with 10 elements while Y becomes an array with 10 rows and 5 columns, i.e. 2-dimensional array.

Take a look at this:

05 552, 635, 454, 655, 334

10 DIM P(5)

20 FOR N = 1 TO 5

30 READ P(N)

40 NEXT N

This will assign the following in an array:

P(1) = 552

P(2) = 635

P(3) = 454

P(4) = 655

P(5) = 334

## GOSUB and RETURN (for subroutines)

E.g.

30 GOSUB 100

40 .

.

.

100 LET K = A + B

.

.

.

150 RETURN

Line 30 will transfer the compiler to the subroutine at line 100 to 150. On reaching 150, it is transferred back to line 40.

In successive posts, each of the functions and keywords will be given detailed explanations with examples. This post is just to provide a quick overview and give you an idea of what we'll be learning under QBasic.