In 1964 Dartsmouth college of USA introduced a low level programming language called BASIC. It became quite popular but it was disliked be many programmers as being low level language.
In 1985 Microsoft released its own version of BASIC which is know as QBASIC. It stands for Quick Beginner's All-purpose Symbolic Instruction Code. Q Basic is simple and easy to learn. It used English like keywords and mathematical symbols to write program.-
Elements of QBasic
Character Set: A set characters which are valid in Qbaisc called character set . It consists of
- Alphabets: A-Z, a-z
- Numbers: 0-9
- special characters: ~!@#$%^&*()_+=-|\[]{};;'"/?.<>
Qbasic keywords , variables, operators are formed using the characters set
Keywords:
Keyword is a special words which have special purpose and meaning in QBasic. It is also called reserved words.
Constant:
Constant are the values that do not change during execution of program. It may be letter, word, number, or special character that does not change in the time of execution. Based on the type of value constants are categorized into two types they are discussed below:
- Numeric constant is positive or negative number. Different mathematical operation can be performed to numeric constant. Numeric constants must not contain letter, string, commas, and blank space.54,3,22,-4.3 etc are examples of some valid numeric constants.
- String Constant are the sequence of characters such as letters,numbers and certain special characters. String constant should be included inside the quotation mark. It may include blank spaces in a string. "Jidesh Baidya","kathmandu, Nepal", "232","Phoenix School" etc are some example of valid string constants.
- Symbolic constant is a symbol represented by a name such as variable,which store one and only one value. It may be number or string . A symbolic constant can be declared using keyword CONST. Pie(PI) and Gravitational constant(g) are symbolic constant. eg CONST PIE= 22/7, CONST G=9.8
Data type is the characteristics or set of possible value that determines the kind of data that can be stored in a variable. there are two major data types used in Q basic they are string and numeric data type.
Variable is a named memory location that contains a value, which may change during execution of program. Variable hold only hold one value at a time. Name of variable should always being with a letter and can have letters, numbers, decimal point, but with no spaces. The location of variable is memory is called the 'address'.
Rules for naming the variables are given below
- variable name can be up to 40 characters long.
- variable name cannot be reserved words i.e. keyword
- variable name must begin with letter (A-Z or a -z)
- variable name cannot contain characters other than letters, numbers, period and type of declaration characters ($,%,&,! and #)
variable are classified into two types they are described below
String variables: used to store a character string, such as a
name, an address, or social security number.
String variable name begins with a letter followed by letter
or digits and must end with a dollar sign.
Nme$="jidesh baidya"
SSnumb$
Dte$
Numeric variable is variable name or reference which stores positive or negative numbers. It can have
age = 24
Integer
long integer
operator
it helps to do different mathematics and logical oration
variable declaration is important in programming.It allows user to declare variable name,its type and size.In Qbasic variable can ve declared in 2 different ways they are discussed below
implicit declaration is assigning a value to variable by using type declaration symbol is known as implicit declaration of variable. It allows user to declare a variable whenever the variable is needed to use in program. Implicit declaration of the bariable can be done using suffix symbols(% ! $ # &)
eg LET n$= "Jidesh" LET age%23 INPUT "enter any number";NUM%
here $ symbol is used for variable name and % symbol is used to declare the variable type integer
or
if else statement
less 40 poor
COMPUTER 40-55 average
55-75 good
75-80 very good
80 above excellent
if (false)
true
else
false
computer=55
IF(computer>=80) (55>=80)
PRINT "excellent";
elseif(computer>=55 and computer <80)
print "very good"
elseif(computer>=40 and computer <55)
print "average"
elseif(computer<40)
print "poor"
END IF
END
cls
input "enter any number";n
select case n
case n mod 2=0:
print "even number"
case n mod 2 < > 0:
print "odd number"
end select
end
goto
a=7
rabindra:
print a
a=a+7
if a<50 then goto rabindra
end
7
14
21
28
35
42
49
operator
There are 4 types of operators
- mathematical/Arithmetic operators (+,-,*,/) ===> answer number
- logical(AND, OR, NOT)==> answer True or false
- string(+) ===> answer string
- Relational Operator(<,>,=,<=,>=,<>(not equals to)) =====> true or false
1>2 false
2>1 ture
1=1 True
1<=1 true
0<>1 ture
print "jidesh"+"baidya"
==>jideshbaidya
logical operator: AND, OR NOT
AND: In any two condition if anyone condition is false that condition will be false. it is true only when both the condition are true.
1 IS TRUE
0 IS FALSE
A=take iphone 11S
B=take note 10+
CONDITION: TAKE iphone 11s AND take note 10+
marks
eng=50
nepp=39
sci=70
ss=55
com=60
if (eng>=40 AND nep>=40 AND sci>=40 AND ss>=40 AND com>=40)
print "contracts you are pass"
A
|
B
|
A AND B =A * B
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
1
|
OR: In any two condition if anyone condition is TRUE that condition will be TRUE. it is FALSE only when both the condition are FALSE.
1 IS TRUE
0 IS FALSE
A=take iphone 11S
B=take note 10+
CONDITION: TAKE iphone 11s OR take note 10+
marketing qualification MBA or practically 5 year experience in marking
A | B | A OR B =A + B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Qbasic statement
instruction with in a program are called statement. Each Qbasic instruction is like sentence. As sentence much have a verb in it each qbasic instruction must have a Qbasic Keyword.
TYPES OF STATEMENT:
1) DECLARATION STATEMENT
2) ASSIGNEMENT STATEMENT
3) CONTROL STATEMENT
1)DECLERATION STATEMENT ARE THOSE STATEMENT WHICH ARE USED TO DECLARE A CONSTANT AND VARIABLE.
FOR EG.
CONST PI = 3.14
DIM A AS STRING
2)ASSIGNMENT STATEMENT is used to assign a value to a variable
for eg let num = 11
let a4="qbasic"
CONTROL STATEMENT control flow of execution of program
for eg
CLS
IF N<10 THEN
PRINT N
END
3) CONTROL STATEMENT control
CLS:It is used to clear the screen
syntax: CLS
REM: It is used to write remarks or explanation. sentence written inside REM is not not executable in program
syntax: REM remarks
Print: it is used to display message in the screen.
Input Statement: it is used for accepting a value from the user in a variable through the keyboard during program execution.
Input command will print and wait for the user to enter the value and then assign this value to variable.
syntax: Input "message";variable
LET statement is used to assign a value to a variable with in a program. Whose value may change during execution of program.
syntax: let a =5
swap: it is used to exchange the value of two similar type of variable
swap var1, var2
END: IT Explain us that program is completed
expression
---------------------------------------------------------------------------
Algebraic Expression | Qbasic Expression
add +
subtract -
multiply *
divide /
power ^
integer division \
simple program
CLS
CONST PI =3.14
RADIUS= 4
PRINT "AREA OF CIRCLE IS" ; PI * RADIUS ^ 2
END
READ ....... DATA STATEMENT
READ ...... DATA statement is used to assign list of data to list of variable at a time. READ statement read data from the DATA statement and store then in variables. DATA statement is not executable statement used to assign the numeric and string constant value to variable listed in READ statment
syntax:
READ variable list
statement
DATA constant
variable list are separated by commas.
eg:
CLS
READ A$,B$,C$,D$,E
DATA we, are, from , grade , 9
print DATA
END
SWAP STATEMENT is used to exchange the values of two similar types of variables or array elements
syntax SWAP var1,var2
CLA
A=1
B=2
PRINT "THE VALUE OF A BEFORE SWAP IS :-";A
PRINT"THE VALUE OF B BEFORE SWAP IS :-";B
SWAP A,B
PRINT "THE VALUE OF A BEFORE SWAP IS :-";A
PRINT"THE VALUE OF B BEFORE SWAP IS :-";B
END
TAB STATEMENT is used to move the cursor to the speciifc column on line where a character is printed. It measures from the first column of the first line, not from the current position.
syntax: PRINT TAB(N)
WHERE N IS INTEGER FROM 0 TO 32767
CLS
PRINT "MY HOBBIES"
A$="PLAYING GAMES"
B$="CODING"
C$="TRAVELLING"
PRINT A$; TAB(20);B$;TAB(45);C$
END
LOCATE [ROW] [COLUMN)
CLS
A$ = "ANY TEXT"
LOCATE 10, 10: PRINT A$
END
SPC(N)
CONST SYNBOLIC CONSTANT= EXPRESSING
Control structure in Qbasic
there are 3 types of control structure used in QBASIC. they are
i) sequential structure
ii) Selection structure
iii) looping structure
in sequential statement program flow is one after other i.e program flow in top to down.
selection structure is branching structure that allows you to transfer program control from one part of program to another on the basis o specified condition or without condition. It has 2 types
a) IF STATEMENT
b) SELECT CASE STATEMENT
A program can select or execute instruction depending on the result of a condition. Program is able to decide what to do or which instruction to execute if the condition is true or what to do or which instruction to execute if the condition is false.
IF-THEN STATEMENT
IF - THEN statement is one way branching statement. If the condition of if is true the statement after then is executed. If the condition is false is false the program directly goes to END IF. There is condition between IF and THEN
syntax:
IF <CONDITION> THEN [STATEMENT]
OR
IF <CONDITION>
[BLOCK OF CODES/ STATEMENT]
END IF
where condition is logical expression that evaluate either TRUE or FALSE.
eg.
CLS
input "In which grade do you study"; class
IF (CLASS = 7 ) THEN
PRINT "HEY YOU FROM GRADE 7 YOU ARE HAVING COMPUTER CLASS RIGHT NOW :)"
END IF
END
IF-THEN-ELSE statement :
Qbasic allows the use of ELSE clause in this statement. It is two way branching statement. This statement can decide which task it has to perform when the condition is true or false. If the condition is true the statement after then is executed and if the condition is false then the statement after else is executed.
CLS
input "In which grade do you study"; class
IF (CLASS = 9 ) THEN
PRINT "HEY YOU FROM GRADE 9 YOU ARE HAVING COMPUTER CLASS RIGHT NOW :)"
ELSE
PRINT "YOU ARE NOT GRADE 9 , I DON'T KNOW WHICH PERIOD YOU ARE HAVEING RIGHT NOW"
END IF
END
END IF
END
IF.....THEN....ELSEIF ELSE statement
it is multiway branching from of IF statement that is used when there are more than two condition to be evaluated.
syntax:
IF <condtition 1> THEN
statement 1
ELSEIF <condition 2> THEN
statement 2
ELSEIF <condition 3> THEN
statement 3
-------------------------------
ELSE
statement n
END IF
SELECT CASE STATEMENT
SELECT CASE statement is the multi branching decision making statement. It executes one of the several statement block depending on the value of an expression, which may be string or numeric type
SYNTAX:
SELECT CASE test-expression
CASE expression-list1
statement1
CASE expression-list2
statement2
-------------
CASE ELSE
statement n
END SELECT
eg
CLS
INPUT "ENTER ROLL NUMBER OF STUDENT"; roll
SELECT CASE roll
CASE 1
PRINT "a"
CASE 2
PRINT "b"
CASE 3
PRINT "c"
CASE ELSE
PRINT "sorry not such roll number exist"
END SELECT
END
UNCONDITIONAL BRANCHING STATEMENT
this statement transfers program from one part of program to another part of program without testing any condition. GOTO statement is commonly used in unconditional statement
syntax:
GOTO linelabel
here line level is ended with colon
eg
cls
let a=5
top:
print a
a= a+5
if a<25 GOTO top
END
LOOPING STATEMENT
QBASIC statement are normally and sequentially executed. In real time projects or practice it is often required to change the order of execution of statements or repeat group of statement until some condition are met or satisfied. In such case the repeating statements are usually called looping in program.
FOR....NEXT STATEMENT
FOR....NEXT is a type of looping statement that repeats a block of statement a specified number of times.
syntax:
FOR counter = start TO end [STEP increment]
[statement]
NEXT [counter]
variable start and end declaration can be number or variable that will be enter by programmer. There are some rules to be followed while specifying start and end value. The rules are listed below:
- both negative and posituve value are use for start and end
- the start and end values can be integer and floating point
- value of start must be lower than the value of end other wise the increment value should be negative
while statement
while statement executes block of statement repeatedly until the specified condition is true. When the value of the condition switches to false the loop stops. This statement ends with WEND statements
syntax:
WHILE <condition>
statements to be repeatedly executed
WEND
eg: program to print sum of first 10 natural numbers
CLS
n=1
while n<=10
s=s+n
n=n+1
wend
print "sum of first 10 natural number is -";s
END
DO WHILE statement
repeats a part of program while a condition is true or a condition becomes true.
DO WHILE <condition>
statement
LOOP
eg: program to print odd number less than 15
CLS
NUM =1
DO WHILE NUM <=15
PRINT NUM
NUM = NUM +2
LOOP
END
DO ..... LOOP WHILE STATEMENT
executes while condition remains true. It is post test loop structure while executes statements block at lest one in this structure condition check at the bottom
syntax:
DO
statement to be repeatedly executed
LOOP while <condition>
eg: program to print school name 5 times
cls
count =1
input "enter your school's name";n$
do
print "School name is ";n$
count = count +1
loop while count <=5
end
NESTED LOOP
loop inside a loop is known as nested loop. Nested loop contains an outer loop and inner loops.
there is very important role of nested role in programming.
In qbasic this is usually used to generate pattern
for printing above pattern the code is
CLS
a$ = "*"
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT a$;
NEXT j
PRINT
NEXT i
FOR i = 1 TO 8
FOR j = 1 TO i
PRINT a$;
NEXT j
PRINT
NEXT i
END
Comments
Post a Comment