Classes

 

 

 

 

 

 

Qbasic is a programming language.  Programming languages are used to create operating systems, applications programs such a Word, Excel, Access, PowerPoint, FrontPage, Publisher, and Telecommunications programs.  Everything that we do with a computer must first be written in a programming language.

The first programming language developed is called machine language which is written with 1 and 0.  This language is often referred to a binary or digital language.  This language required the programmer to give step by step instructions for each action in binary code. Computers only understand machine language.

The second level programming language is called assembler language.  This language allowed the programmer to use alphabet characters such as MV (which means move) that will be translated by the program into machine language. Two methods are used to translated a programming language into machine language.  
    1. A compiler takes all of the source code and translates the code into another file called the object file which is in binary format and then executes the object file.  The source file still remains. Compiled programs execute independent of the programming language and execute much faster than interpreted languages. Object files generally have a .exe extent.
    2. An interpreter take one line of the source code translates it into machine code, executes the line, returns to the source code and gets the next line, interprets it into machine code.  This continues until all of the source code has been translated and executed line by line from the top of the program to the bottom.  This is referred to as sequencing through the program. An interpreter functions inside of the programming language. The version of Qbasic that we will be using uses an interpreter. 

The third level programming languages (Higher Level) consisted of languages that were closer to natural languages.  Cobol, RPGII, Qbasic, Pascal, C, C+++, Fortran are examples of third level languages.   These languages allow you to use English words such as print, input, and read to write  program code.  

The fourth level languages are the visual languages such as Visual Basic and Visual C+++.  These are also called Object Oriented Programming languages because they attached code to objects (icons/visual pictures).  Windows 98 was written using OOP. 

The fifth level languages are the voice actuated languages.  Windows Office XP comes bundled with voice actuation and writing pad actuation. 

All computer languages operate within the Structured Theorem parameters which allow the solving of programming problems with:

    1. Sequencing in which code is executed line by line from top to bottom.  All computers execute code starting at the top of the program and continue to the bottom (end) of the program code.

    2. Selection in which a branch of code is executed based on a true or false condition. The control structures used with selection are if, then, endif for single selection, if, then, else, endif for two way selection statement, and a if, then, elseif, then, else enidif for multiple selection. 

IF condition1 THEN
[statementblock-1]
[ELSEIF condition2 THEN
[statementblock-2]]...
[ELSE
[statementblock-n]]
END IF
If age < 30 then
Print "You are less than 30 yrs old"
ElseIf age <40 then
print "You are less than 40 yrs old"
Else
Print "You are older than 40"
End If

 

    3. Iteration (looping, repetition) in which a branch of code is executed over and over while a condition is true.  The control structures are Do While-Loop,- Do Loop Until, and For, Next  for iteration statements.

DO [{WHILE | UNTIL} condition]
[statementblock]
LOOP
or
DO
[statementblock]
LOOP [{WHILE | UNTIL} condition]

FOR counter = start TO end [STEP increment]
[statementblock]
NEXT [counter [,counter]...]

    4. Select Case allows selection of a branch of code based on a pre-selected condition being true. My friend Brian Marshall considers that the Select Case is a selection statement and that Structured Theorem only has three elements. The control structure for select case is

SELECT CASE testexpression
CASE expressionlist1
[statementblock-1]
[CASE expressionlist2
[statementblock-2]]...
[CASE ELSE
[statementblock-n]]
END SELECT

In summary all computer programming languages  solve problems using sequencing, selection, iteration and case select control structures. 

An important part of programming is the use of RAM (Random Access Memory).  In machine and assembler languages use of RAM was accomplished by stating the hexadecimal address in which information was to be stored.  Every time a byte of information was used, or data was derived from stored data the hexadecimal address had to be referenced to find the data and to store the derived data.  In Qbasic we only have to declare a variable or constant name, specify the data type and Qbasic like all higher level languages keeps track of the hexadecimal address in memory of the data for us.  A variable name must begin with a alpha character and can have up to 40 characters in the name.  The period or alpha characters or numbers can be used in a variable names. When you identify variables always make the name logical. For example: FirstName$ instead of fn$.

Variable - a variable name represents a location in memory where data will be stored that may or may not change during program execution. 

Constant - a constant name represents a location in memory where data will be stored that will not change during program execution.

Qbasic supports two  variable and constant data types:

 1. Alpha Numeric (also called a string) is represented by a $ sign at the end of the variable or constant name.  This data type can store Alpha characters and numbers in memory.  If you have numbers such as SSN, PartNo., InvoiceNo. ,etc. that will not be used in calculations (added, subtracted, multiplied, or divided) then make theses numbers an Alpha Numeric variables or constants.

    For example: Firstname$, City$, State$ and SSN$  are all alpha numeric variable names. When a variable or constant is declared the data type must also be declared.  By declaring the data type Qbasic sets up the address in memory  to receive the specific data type. 

2. Numbers are represented by !,%,# and & at the end of the variable or constant name.

    ! - single precision numbers are  4 bytes 
        positive - 2.80E-45 to 3.40E+38
        negative -3.40E+38 to -2.80E-45
    # - double precision number  8 bytes
        positive - 4.94D-324 ro 1.80D+308
        negative -1.80D+308 to -4.94D-324
    % -  integer-2 byte- -32,768 to +32,767
    &-  long integer- 4 byte- 0 to 4 billion
        -2,147,483,648 to 2,147,483,647

 For example: if you wanted to initialize a variable for a persons age using an integer would be appropriate since all human fall between the age of 0 and 200 years.  

let Age% = 0  or 
Age% = 0

These two statement are identical in the operation performed which is to identify a hexadecimal address in memory to store data that is an integer and to enter a 0 into that memory address. When you initialize variables,  numeric variables are initialize to 0 and alphanumeric variables are initialized to " ". 

let FirstName$= " " or
FirstName$ = " "

Constants are initialize to the value of the constant.

FedTaxRate!=.037 or
CompanyName$ = "Cheap Student Labor"

Note: Let statements use the = sign to assign to the memory address the data to the right of the =. It is not an equality statement.  For example:

profit! = retailprice!-cost! 
This statement assigns to the memory address called profit! the results of the evaluation of the expression/formula.

REM - the REM or ' are used to provide internal documentation of the program.  The rem is used by the programmer to tell how the program is written.  Any data following the REM or ' is not executed by Qbasic. 

PRINT - outputs to the screen each print line.  LPRINT outputs to the printer each lprint line.  For example:

Print "Hello"  outputs to the screen Hello.  To place the text at a specific column on the 80 column screen the Tab() function can be used.  For example:
Print tab(25) "Hello"   will move the cursor to column 25 and display Hello.  The 25 inside of the parentheses is called the argument.  The argument of the tab function is the column number. Functions are built in programs in Qbasic that have already been written to make often used code available for easy use. Most programming languages have a library of built in functions.

If you use a for next loop to print out hello five times by changing from , or ; or : following the word hello will provide different output.

For example: 

for x = 1 to 5
print "hello":( or nothing)
next x

will output
hello
hello
hello
hello
hello

The : or nothing at the end of a print statement forces the next print statement encountered down one line.

for x = 1 to 5
print "hello ";
next x

will output
hello hello hello hello hello

The ; at the end of the print statement forces the next print statement encountered to print on the same line immediately after the last print statement.  

for x = 1 to 5
print "hello",
next x

will output:
hello      hello      hello       hello       hello
The , at the end of the print statement forces the next print statement encountered to print 11 spaces to the right.  The 11 spaces is  called a print zone. 

If we want to format the output of the print statement we can use print using.  For example: If pay is 4567.89

Print using "your net pay is $$###,###.##"; pay    
would result in:
your net pay is $4,567.89

The # signs are place markers for the digits. The two $$ signs force the $ to float up against the first occupied digit. If only a single $ is used then the $ will be placed at the first place marker. For example:

Print using "your net pay is $###,###.##"; pay    
would result in:
your net pay is $  4,567.89

Data can be entered into a computer program three different ways.

    1. Data can be stored in the program code itself by using the Read and Data statements in Qbasic.

READ firstname$, age%, income!
DATA "Harry Jones", 23, 4444.44

When you use the read statement there must be the same number of data elements in the data statement, in the same order, and the same data type: often referred to as the NOT rule.

    2. Data can be input from the keyboard  and other input devices using the Input statement directly into a variable.

Input "Enter your first name"; firstname$

    3. Data can be input from data files stored on floppy drives, hard drives and CD's by using the open files statements.

Open “a:\test.txt for

            Append  - Add records to the bottom of the file.
           
Output – Add records to the file where the file pointer is             located
            Input – read data from the disk file into memory
            As #1 – pseudo name for file while open – up to 12

 Open “a:\test.txt” for append as #1
 Write #1, studentname$, test1,test2,test3
 Close #1

Or

Open “a:\test.txt” for output as #1
Write #1,  studentname$,test1,test2,test3
Close #1

Or

Open “a:\test.txt” for input as #1
Input #1, studentname$,test1,test2,test3
Close #1

Note: when you write data to a file to input the data back into main memory you must follow the NOT rule.

Functions:

Function Argument,use
tab() column number, used with print to place text at column 
eof() file number opened, used to read file until ^z end of file
abs() number, absolute value
asc(X$) first char of string, returns ASCII code number
chr$() number, returns ASCII character for number x
date$ none, returns current date mm-dd-yyyy
time$ none, returns current time hh:mm:ss
lcase$() char, returns lower case
ucase$() char, returns upper case
len() char, returns number of char in string
   


EOF()  use this function to read data from disk file.

Problem: We have formed a company called cheap student labor and have to write a payroll program that will allow us to enter from the keyboard employee name, Social Security Number, address, city, state, zip, number of hours worked, date hire, payrate, and pay dates.  Additionally we need to produce a payroll stub that includes current and year to date hours worked, regular, overtime, and double time and pay for each, federal income tax, state income tax, Social Security tax, state disability insurance, gross pay, net pay.  Additional the pay stub will have check no., Company name, address, city, state, pay period from and to dates, date check was written,  employee name, ssn, address, city, state, zip.  Furthermore the program must be menu driven with one entry point into the program and one exit point and the data is to be stored on magnetic disk. Last we are to produce both detail internal documentation of the program and a users Manuel. 

The first thing that we do to solve this problem is to define what the output is going to be. Once we know what the output is going to be we can then determine what we have to input into the program, what processing of the raw data must be done, and last formatting the output  in a form the user can understand.  Input - Processing - Output is the sequencing the program must use to be successful. 

Fortunately, we have a Qbasic program template to use for this purpose. 

REM
REM Date
REM Instructor Name
REM Assignment Chapter      Number      Page  
REM
REM Statement of problem :
REM Analysis  IPO.
REM
REM Data Requirements
REM
REM Constants
REM
REM Inputs
REM
REM Outputs    
REM
REM Variables
REM
REM Formulas
REM
REM Design
REM
REM     Algorithm
REM
REM     1.
REM     2.
REM     3.
REM     4. Etc.
REM
REM Implementation - here you begin to code.
CLS
Rem  Initialization Section This is where we state our variables. 
REM Input Section
REM Processing Section
REM Output Section

We also have a template that we can use to set up a menu with that will have one entry point into the program and one exit point.

CLS
MenuOption = 0
DO WHILE MenuOption <> 7
   PRINT
   PRINT
   PRINT "   M  E  N  U"
   PRINT "----------------"
   PRINT "1. OPTION 1"
   PRINT "2. OPTION 2"
   PRINT "3. OPTION 3"
   PRINT "4. OPTION 4"
   PRINT "5. OPTION 5"
   PRINT "6. OPTION 6"
   PRINT "7. EXIT PROGRAM"
   PRINT
   INPUT "YOUR CHOICE =>"; MenuOption
   SELECT CASE MenuOption
      CASE IS = 1
         PRINT
         PRINT "You selected option 1."
      CASE IS = 2
           PRINT
           PRINT "you have selected option 2:"
      CASE IS = 3
         PRINT
         PRINT "You selected option 3."
      CASE IS = 4
         PRINT
         PRINT "You selected option 4."
      CASE IS = 5
        PRINT
        PRINT "you have selected option 5"
      CASE IS = 6
        PRINT
        PRINT "You have selected option 6"
      CASE IS = 7
         PRINT
         PRINT "Thank you for using this program."
      CASE ELSE
         PRINT
         PRINT "Invalid Menu Option"
   END SELECT
LOOP
END

Since we will be interacting with the monitor we have format part of our program to deal with a monitor screen that is 80 columns wide by 21 rows (the same as the original keypunch card) and our printer output is is 80 columns by 66 rows. When ever you send data to the screen you want to clear the screen for your output which is done using the cls statement. 

Mathematical Order of precedence for evaluating expressions/formulas.
Mathematical Operator Use
^ - exponential 3^3 = 27
* - multiply 3*3 = 9
/ - divide 5/2=2.5
\ - integer division 5\2 = 2
mod - modular division 5 mod 2 = 1
+ - add 5 +2 = 7
- - subtract 5 - 2=3
(   ) parenthesis  do what is inside first
 

Copyright © by Earl T. Wylie 2001, 2002,2003,2004,2005,2006,2007