Qbasic Programming
Qbasic Programming
1. END STATEMENT
Ends a program, procedure, block, or user-defined data type. If no argument is supplied, END
ends the program and closes all files.
2. OUTPUT STATEMENT
PRINT “Welcome to QBASIC programming”
PRINT “I am Ashfa”
END
Symbol Meaning Usage Output
; to append several PRINT “Welcome”; “Home” WelcomeHome
items without space
, to append several PRINT “Welcome”, “Home” Welcome Home
items with space
3. COMMENTS
Comments are portions of the code which do not compile or execute. In the following example,
an underlined statement is a comment.
Example
REM My first qbasic program
CLS ‘clears the screen
PRINT “Welcome to QBASIC programming”
PRINT “I am Ashfa” ‘prints your name
END ‘ends the program
Program Execution
OUTPUT
Welcome to QBASIC programming
I am Ashfa
1 © [email protected] 9110191
4. ARITHMETIC OPERATORS
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division (Real)
\ Division (Integer)
^ Exponent
5. HIERARCHY OF OPERATIONS
1. Parentheses
2. Exponentations
3. Multiplication & Division
4. Mod & Integer Division
5. Addition & Subtraction
6. RELATIONAL OPERATORS
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
= Equal to
<> Not equal to
String (str) can contain characters like “A” or “ƒ”. Strings are always notated with
double quotation marks around them (“).
Integer (int) can store only non-decimal numbers from -32768 up to 32767
Long (lng) can store only non-decimal numbers from -2147483648 to 2147483647
2 © [email protected] 9110191
8. IDENTIFIERS
Identifiers are names given to various program elements, such as, variable, constant, arrays,
functions, sub-program etc
END
Program Execution
OUTPUT
CONSULTANCY SERVICE 21
CONSULTANCY SERVICE21
3 © [email protected] 9110191
10. VARIABLE DECLARATION USING DIM STATEMENT
DIM a AS INTEGER, name AS STRING, age AS INTEGER
Example
REM a program to demonstrate use of dim statement
DIM cmpname AS STRING
DIM rank AS INTEGER
CLS
cmpname = “COUNSALTANCY SERVICE”
rank = 21
PRINT cmpname, rank
PRINT cmpname; rank
END
Program Execution
OUTPUT
CONSULTANCY SERVICE 21
CONSULTANCY SERVICE21
11. CONSTANTS
name = “Tao Yue”
firstletter = “a”
year = 1997
pi = 3.1415926535897932
married = “TRUE”
12. ASSIGNMENTS
Once you have declared a variable, you can store values in it. This is called assignment.
Example
DIM some_int AS INTEGER
some_int = 375
PRINT some_int
END
Program Execution
OUTPUT
375
4 © [email protected] 9110191
13. SOME MATHEMATICAL FUNCTIONS
Function Meaning Example Output
ABS() returns the absolute value of a number PRINT ABS(45.5 - 100.0) 54.5
INT() returns the integer value of a number PRINT INT(10/3) 3
MOD returns the remainder of an
PRINT 10 MOD 3 1
expression
returns the square root of an
SQR() PRINT SQR(25) 5
expression
converts a string representation of a
VAL() PRINT VAL("12") 12
number to a number.
exchanges the values of two variables
SWAP() SWAP VARIABLE1, VARIABLE2
of the same type.
Example
REM a program to demonstrate use of goto
DIM i AS INTEGER
i = 10
start:
PRINT "I = "; i
i = i + 10
GOTO start
END
5 © [email protected] 9110191
15. INPUT STATEMENT
Input is what comes into the program. It can be from the keyboard or file. Input can be done to
one or more variables, separated by commas. For keyboard input, a semicolon immediately
after INPUT keeps the cursor on the same line after the user presses the Enter key. INPUT can
be accepted into STRING, NUMERIC, ARRAY, and RECORD ELEMENTS.
INPUT : reads input from the keyboard, uses a comma as a separator between entries.
LINE INPUT: reads a line of up to 255 characters from the keyboard, up to an enter key.
Example 1:
CLS
INPUT " NAME : ", name$
INPUT " AGE : ", age$
PRINT
PRINT " NAME : "; name$
PRINT " AGE : "; age$
END
Program Execution
PROMPT INPUT OUTPUT
NAME : Ashfa
AGE : 21
Name : Ashfa
AGE : 21
Example 2
REM calculation of simple interest using PRINT to prompt
input
CLS
DIM p, r, t, si AS SINGLE
PRINT “Enter the values of P, R, T”
INPUT p, r, t
si = p * r * t / 100
PRINT si
END
Program Execution
PROMPT INPUT OUTPUT
Enter the values of P, R, T 1000, 5, 0.2 10
6 © [email protected] 9110191
Example 3
REM calculation of simple interest using INPUT to prompt
input
CLS
DIM p, r, t, si AS SINGLE
INPUT “Enter the values of P, R, T ”, p, r, t
si = p * r * t / 100
PRINT si
END
Program Execution
PROMPT INPUT OUTPUT
Enter the values of P, R, T 1000, 5, 0.2 10
Example 4
REM calculation of average age
CLS
DIM a1,a2,a3,a4,tot AS INTEGER, average as SINGLE
PRINT “Enter the values of A1,A2,A3,A4”
PRINT ‘prints a blank line
INPUT a1,a2,a3,a4
tot = a1 + a2 + a3 + a4
average = tot / 4
PRINT average
END
Program Execution
PROMPT INPUT OUTPUT
Enter the values of A1,A2,A3,A4
4,5,6,7 5.5
7 © [email protected] 9110191
16. CONDITIONAL STATEMENT
A Conditional statement allows selective processing of a statement or a group of statements.
There are four forms of conditional statement:
1. if… then… else…
2. if… then… elseif… else… (linear if)
3. if… if… then… else… (nested if)
4. select case
a. IF … THEN… ELSE …
Example 1
REM program to determine an input age is major/minor
DIM age AS INTEGER
CLS
INPUT “Enter Person’s Age: ”, age
IF age < 18 THEN
PRINT “Person is MINOR”
ELSE
PRINT “Person is MAJOR”
END IF
END
Program Execution
PROMPT INPUT OUTPUT
Enter Person’s Age: 25 Person is MAJOR
Example 2
REM a program to determine if a number is odd or even
DIM num AS INTEGER
CLS
INPUT “Enter any number : ”, num
IF (num MOD 2 = 0) THEN
PRINT “Even”
ELSE
PRINT “Odd”
END IF
END
Program Execution
PROMPT INPUT OUTPUT
Enter any number : 4 Even
8 © [email protected] 9110191
Example 3
REM calculation of total expenditure
CLS
DIM qty, rate, dis AS INTEGER, totexp AS SINGLE
PRINT “Enter the values of qty, rate”
INPUT qty, rate
IF qty > 1000 THEN
dis = 10
totexp = qty*rate – dis*rate/100
PRINT totexp
END IF
END
Program Execution
PROMPT INPUT OUTPUT
Enter the values of qty, rate 2000,15 29998.5
9 © [email protected] 9110191
c. IF… IF… THEN… ELSE… (Nested if)
Example
REM discount calculation
CLS
INPUT "Enter Person : ", person$
IF person$ = "employee" OR person$ = "EMPLOYEE" THEN
INPUT "Enter years worked : ", years%
IF years% > 5 THEN
INPUT "Enter Item bought : ", item$
IF item$ = "electrical" OR item$ = "ELECTRICAL" THEN
PRINT "DISCOUNT IS 10%"
ELSE
PRINT "DISCOUNT IS 20%"
END IF
ELSE
PRINT "DISCOUNT IS 10%"
END IF
ELSE
PRINT "NO DISCOUNT"
END IF
END
Program Execution
PROMPT INPUT OUTPUT
Enter Person : employee
Enter years worked : 7
Enter Item bought : soap
DISCOUNT IS 20%
10 © [email protected] 9110191
d. SELECT CASE
The SELECT CASE statement tests the value of a given variable/expression against a list of
case values and when a match found, a block of statements associated with that case is
executed.
Example 1
REM calcultion program
DIM a, b AS INTEGER, result AS SINGLE, op AS STRING
INPUT "Enter first number : ", a
INPUT “Enter second number : ”, b
INPUT “Enter any one operator + - / * : ”, op
SELECT CASE op
CASE IS = "+"
PRINT " ADDITION "
result = a + b
PRINT a; “ + ”; b; “ = ”; result
CASE IS = "-"
PRINT " SUBTRACTION "
result = a - b
PRINT a; “ - ”; b; “ = ”; result
CASE IS = "*"
PRINT " MULTIPLICATION "
result = a * b
PRINT a; “ * ”; b; “ = ”; result
CASE IS = "/"
PRINT " REAL DIVISION "
result = a / b
PRINT a; “ / ”; b; “ = ”; result
CASE ELSE
PRINT "INVALID OPERATOR "
END SELECT
END
Program Execution
PROMPT INPUT OUTPUT
Enter first number 25
Enter second number 16
Enter any one operator + - / * : /
REAL DIVISION
25 / 16 = 1.5625
11 © [email protected] 9110191
Example 2
REM Calculating days of a month
DIM month, days, year AS INTEGER
CLS
INPUT "Enter the month number : ", month
SELECT CASE month
CASE IS = 1, 3, 5, 7, 8, 10, 12
days = 31
CASE IS = 4, 6, 9, 11
days = 30
CASE IS = 2
INPUT "Enter the Year : ", year
IF (year MOD 4 = 0) THEN
days = 29
ELSE
days = 28
END IF
CASE ELSE
PRINT "Invalid month number"
END SELECT
PRINT days
END
Program Execution
PROMPT INPUT OUTPUT
Enter the month number : 1 31
Enter the month number : 9 30
Enter the month number : 18 Invalid month number
Enter the month number : 2
29
Enter the year : 2000
12 © [email protected] 9110191
17. LOOP STRUCTURES
a. FOR… NEXT LOOP
The FOR LOOP is a very flexible and powerful repetitive structure (PRE-TEST LOOP). If you
know the number of times a block of statement(s) that you want to execute a specific
number of times then FOR LOOP is most appropriate.
Example 1
FOR i = 1 TO 5
PRINT i
NEXT i
Program Execution
OUTPUT
1
2
3
4
5
Example 2
FOR i = 7 TO -6 STEP –3
PRINT i
NEXT i
Program Execution
OUTPUT
7
4
1
-2
-5
Example 3
REM print 1st N number. Also calculate sum & average of N
numbers
CLS
DIM i, n, sum AS INTEGER, avg AS SINGLE
Sum = 0
INPUT "Enter number : ", n
FOR i = 1 TO n
PRINT i
sum = sum + i
NEXT i
avg = sum / n
PRINT "Sum : "; sum
PRINT "Average : "; avg
END
13 © [email protected] 9110191
Program Execution
PROMPT INPUT OUTPUT
1
2
Enter number : 4
3
4
Sum : 10
Average : 2.5
Example 4 (Nested Loop)
REM nested loop
DIM row, column AS INTEGER
CLS
FOR row = 1 TO 5 ‘Start of Outer loop
FOR column = 1 TO 5 ‘Start of inner loop
PRINT column,
NEXT column ‘End of Inner Loop
NEXT row ‘End of Outer Loop
END
Program Execution
OUTPUT
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
b. WHILE…WEND LOOP
The Do … While loop is a post-test loop
Example 1
DIM count AS INTEGER
count = 0
WHILE count < 5
PRINT "Count = "; count
count = count + 1
WEND
END
Program Execution
OUTPUT
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
14 © [email protected] 9110191
Example 2
DIM count AS INTEGER
count = 7
WHILE count > -6
PRINT "Count = "; count
count = count - 3
WEND
END
Program Execution
OUTPUT
Count = 7
Count = 4
Count = 1
Count = -2
Count = -5
c. DO…LOOP UNTIL… LOOP
The Do … Loop Until… loop is a post-test loop
Example 1
DIM count AS INTEGER
count = 0
DO
PRINT "Count = "; count
count = count + 1
LOOP UNTIL count = 5
END
Program Execution
OUTPUT
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
15 © [email protected] 9110191
Example 2
DIM count AS INTEGER
count = 7
DO
PRINT "Count = "; count
count = count - 3
LOOP UNTIL count < -6
END
Program Execution
OUTPUT
Count = 7
Count = 4
Count = 1
Count = -2
Count = -5
16 © [email protected] 9110191