Download - Basic computer-programming-2

Transcript

What is QBasic?

BASIC stands for Beginner’s All Purpose Symbolic

Instruction Code. It was invented in 1963, at

Darmouth College, by the mathematicians John

George Kemeny and Tom Kurtzas.

BASIC is an interpreter which means it reads every

line, translates it and lets the computer execute it

before reading another. Each instruction starts with

a line number.

QBasic Data

Data is a collection of facts and figures that is

entered into the computer through the keyboard.

Data has 2 Types:

1) : Data whose value does not change or

remains fixed. There are two types of constants:

(a) : Numbers – negative or positive used for

mathematical calculations

e.g. -1,20,0

(b) : Numbers or alphabets

written within double quotes (inverted commas ” “)

e.g. “Computer”, “Operating System”

2) : Data whose value is not constant and may change due to some calculation during the program execution. It is a location in the computer’smemory, which stores the values. Depending on what value is held, variables are of 2 Types:

(a) : The variable that holds a Numeric Constant for arithmetic calculations (+,-,*,/) is called a Numeric Variable.

e.g. A = 50, here A is the Numeric Variable.

(b) : The variable that holds an Alphanumeric Constant, which cannot be used for arithmetic calculations, is called Alphanumeric Variable or String Variable. An Alphanumeric variable must end with a $ sign and the Alphanumeric constant must be enclosed in inverted commas.

e.g. Name$ = “Akanksha”, here Name$ is an Alphanumeric Value.

Types of Mode in QBasicOnce QBASIC program is loaded into the computer memory, it displays Ok

prompt. Ok means it is ready to accept the commands. QBASIC can be

made to translate your instructions in two modes:

• Direct Mode

• Program Mode

Direct Mode: The mode accepts single line instructions from the user and the

output is viewed as soon as enter key is pressed. The instruction are not

stored in the memory. This mode can be used to do quick calculation.

They do not have line numbers. E.g. Print 3+4

Program Mode: The mode is used to type a program which is stored in the

memory. They have line numbers. We have to give the command to get

the output.

E.g.

10 Print 3+4

20 End

RUN

Program is a set of step-by-step instructions that

tells or directs the computer what to do. It

sequences the tasks and user wants to be done

and produces results or output needed.

The set of rules or instructions that tells the

computer what to perform is done through a

Programming Language. There are various types

of Programming Languages you can choose from.

A Programmer is the person who designs a

program. It converts problem solution into

instructions for the computer. The programmer

designs the program, decides which of the

programs or set of instructions to use and tests

the program to see if it is working as designed.

Retrieving keyboard

input from the user

One way to receive input from the keyboard is with

the INPUT command. The INPUT command allows

the user to enter either a string or a number, which

is then stored in a variable.

When this program is executed, the INPUT command

displays a question mark, followed by a blinking

cursor. And when you enter text, the program stores

that text into the variable data which is printed to

the screen.

If you place a string and a semi-colon between input and the variable, the program will print the string

Example:

INPUT “Enter some text :”; data

To receive a number, use a non-string variable.

INPUT number

PRINT number

PROGRAM

OUTPUT

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.

Example:

x = 5

IF x = 5 THEN PRINT “ x is equals 5”

Since x does equal 5 in this case , the program outputs;

x equals 5

Expression SignsYou 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 >= (x is greater than or equal to 5)

ELSEUsing the ELSE command, you can have the program

perform a different action of the statement if

FALSE.

Example:

x = 3

IF x = 5 THEN PRINT “Yes” ELSE PRINT “No”

Since ‘x’ is not equal to 5, the output is:

No

END IFEND 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 off commands.

Example:

x = 5

IF (x = 5) THEN

INPUT a$

PRINT a$

END IF

Multiple ExpressionsYou 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”.

The AND operator requires both expressions to be true.

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…THENSo far in this topic, we’ve only been dealing with

numbers, but you can also use strings with the IF. .

.THEN command.

Example:

x = “Hello”

IF (x = “Hello” OR x = “World”) THEN PRINT x

OUTPUT:

Hello

Guessing GameThe following is a simple guessing game:

CLSstart :

PRINT “Guess a number between 1 and 10: “ ;

INPUT num

If (num < 1 OR num > 10) THEN PRINT “That is not between 1 and 10”

GOTO start

IF (num=6) THEN PRINT ”CORRECT! ! !”

ELSE

PRINT “TRY AGAIN”

PRINT

GOTO START

END IF

OUTPUT (may be sligthly different):

Guess a number between 1 and 10: ? 2

Try Again

Guess a number between 1 and 10: ? 7

Try Again

Guess a number between 1 and 10: ? 6

Correct! ! !

Blessy B. Campos Michelangelo Lemon