programs and programming
DESCRIPTION
Programs and programming. A program is a very specific set of rules that tell the computer what to do. It is a sequence of primitives to be executed by a computer in order to automate a certain business, industrial or scientific task. The process of creating a program is called programming. - PowerPoint PPT PresentationTRANSCRIPT
Programs and programming A program is a very specific set of rules A program is a very specific set of rules
that tell the computer what to do. It is that tell the computer what to do. It is a sequence of primitives to be executed by a computer in order to automate a certain business, industrial or scientific task.
The process of creating a program is called The process of creating a program is called programming.programming.
The computer only knows what it is told The computer only knows what it is told through programs, so they must be through programs, so they must be accurate and very specific.accurate and very specific.
Common computer primitives:
Calculating an arithmetical or a logical expression and generating a value
Storing/retrieving a value into/from the memory Comparing the contents of two memory locations and
selecting one of two alternatives Accepting a value from an input device Sending a value to an output device Sending and receiving of audio and video streams repeating a group of primitives
Steps in programming Deciding if there is a task to be accomplished or problem
to be solved using a computer, eg, is there a need for a program?
Determining the nature of the task or problem, eg, what must the program do?
Developing a plan that will accomplish the task or solve the problem, eg, generating the step-by-step process that the program will follow (algorithm)
Converting the plan into a computer language program Implementing the program to accomplish the task or
solve the problem Testing the program to ensure it accomplishes task or
solves problem defined earlier
Types of programming and languages
Procedural: Monolithic programs that run from start to finish with no intervention from user other than input. Uses variables, structures, and functions. There is a root function. Other functions are called inside.
Languages:Basic, COBOL, FORTRAN, C, Pascal
Types of programming and languages Object-oriented: Apart from variables,
structures, and functions used in procedural programs, it also uses classes. A class contains a collection of variables (properties) and functions (methods).
Languages:C++, Object Pascal, Java, Eiffel
Types of programming and languages Object Oriented/Event Driven (OOED) or
Visual: Programs that use objects which respond to events. Special classes are used to represent visual objects. Small segments of code are associated with each object.
Languages:Visual Basic, Delphi, Visual C++
Levels of computer languages Low Level: at the level of the computer, that is, in binary
(0-1) format Intermediate level: close to the computer but uses
English words or mnemonics, eg, Assembler, that is converted directly into binary
High Level: at the level of the programmer using English words and clearly defined syntax; must be converted or translated into binary for computer to implement it,eg, Visual Basic
Computer can execute only a binary form of a program Need a software program to handle the conversion of
high-level into binary
Language translation process
Total = 0
Current = 10
Do while current <> 0
Total = Total + Current
Current = Current – 1
Loop
Translation Program
High level language program
10111000 11011000
10110001 00000100
11100010 10101000
11100011 00010001
10111000 11011000
10110001 00000100
11100010 10101000
11100011 00010001
11100010 10101000
Machine language program
Translating from High-level Language to Binary
Interpreted: each statement translated as it is executed--slow but easy to use while developing programs
Compiled: entire program is converted to binary--executes faster, but more difficult to use (.exe files are compiled programs)
VB is interpreted during creation and testing but can then be compiled into an .exe file
Languages used in this course
Visual Basic. For the demonstration purposes. Some examples demonstrated.
Visual Basic for Applications (VBA). Subset of full-fledged Visual Basic adapted to the applications of Microsoft Office. Used to solve many business problems.
Variables
Named memory locations where you can store data values
Can imagine them as buckets with a label on them
5
Age
Variables (Cont.)
Contain values that are expected to change
When a variable changes, it is similar to taking the current item out of the bucket and replacing it with a new item
Age
58
Variables Variables are named locations in memory (memory cells) in which we store
data that will change at run time Variable names in Visual Basic VB:
Must begin with letter can’t include period can’t be over 255 characters are not case-sensitive Contain only letters, numbers, and underscore character ( _ ), No
spaces, punctuation marks Be any word except a VB reserved word, E.g. End or Sub
Example variable names: VideoPrice for Video Rental Price Taxes for taxes on this price AmountDue for sum of price and taxes YTDEarnings for year to date earnings EmpLastName for employee’s last name
Variable Name Example(Yes or No?) Radius 8_student Number_1 Number2 Hours_worked Gross Pay Net/Pay
Data Types A data type specifies the type of data that is
assigned to a variable or constant Two primary types of data in VB
numeric and string Numeric data can be used in arithmetic
operations String data should not be used in arithmetic Numeric data types can be subdivided into
specific types:- currency $55,567.78 - integer 255- single 567.78 - long (integer) 35,455- double 567.78129086 - Boolean True or False
Numeric Data Type
Range Precision Number of Bytes
Single 1.4E-45 to 3.4E38 and
-3.4E38 to -1.4e-45
Seven significant digits
4
Double 4.9E-324 to 1.8E38 and
-1.8E308 to -4.9E-324
Fifteen significant digits
8
Cur-rency
-922,337,203,685,477.5808 to 922,37,203,685,477.5807
4 places to right of decimal
8
Data types
Data types
Numeric Data Type
Range Precision Number of Bytes
Integer -32,768 to 32,767 Whole numbers
2
Long -2,147,483,648 to 2,147,483,647
Whole numbers
4
Date January 1, 100 to December 31, 9999
Not applicable
8
Boolean True or False Not applicable
2
Data types
Numeric Data Type
Range Precision Number of Bytes
String (variable length)
N/A N/A 10 bytes+length of string
String (fixed length)
N/A N/A Length of string
Variant adjusted adjusted 8
Integer Variables
Store integers between -32,768 and 32,767
Require no decimal point If the number is outside of the range
Use the Long data type
Long Integer Variables
Used to store integers between -2,147,483,648 and 2,147,483,647
Must be used whenever an integer number exceeds the range for the Integer type
Single Precision Variables
Represents numbers which have both a whole and a fractional part
Used to store values ranging in value from -3.402823E38 to -1.401298E-45 for negative values and from 1.401298E-45 to 3.402823E38 for positive values
Can store numbers with greater precision than integers
Double-Precision Variables
Store large floating point numbers, but require twice as many bytes of storage compared to type Single
Stored as floating-point numbers ranging in value from - 1.79769313486232E308 to - 4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Single vs. Double
Use Single unless the calculation requires greater precision
Not only does Double take more memory to store, but it also requires more time to run than type Single
Boolean Variables
Can be used whenever a variable may have one of only two possible values - True or False, Off or On, etc.
Frequently used in statements which require a test of whether something is true or false.
String Variables
Can hold any character, word, or phrase that the keyboard can produce.
In VB there are two kinds of string variables: Variable-length: Stings in which the number of
characters in the string is not specified in advance. Fixed-length: Strings in which the number of
characters in the string is specified in advance
Declaring Variables
Declare ALL variables with the DIM statement General form:
Dim Variable1 as type1, variable2 as type2, etc. For example,
Dim strMyName as String, sngMyValue as Single Dim curTaxes as Currency, curPrice as Currency Dim curAmountDue as Currency
Two or more variables can be declared with same Dim statement but you must include the variable type
If you fail to declare a variable after the Option Explicit statement has been entered, an error occurs at Run time
Use variables rather than objects in processing since all textboxes are strings
Structures
A structure contains a collection of variables (properties) to represent an entity. For example:Struct Date {int Day, char Month, int year}
This example is from language C
Functions
A set of instructions that are executed as one whole.
In the language of VBA a function that does not return a values is called Subroutine. A function that returns a value is called Function.
Objects
Apart from properties, like in structures, also contain methods represented by functions. An object represents a physical entity. Class is the description of an object in programming language.
Example: An object of class Car has the following properties: Wheel, engine, hulk, seats. It also has the following methods: it can drive, it can be parked, it can undergo reparation.
Object-Oriented Event-driven Programming (OOED)
OOED uses objects, or self contained modules that combine data and program code that pass strictly defined messages to one another.
OOED is easier to work with, because it is more intuitive than traditional programming methods.
Visual Basic is an OOED languageUsers can combine the objects with relative ease to
create new systems or extend existing ones.Properties of objects are attributes associated with an
objectMethods of objects are those activities that the object
can carry outObjects respond to events – mouse click, key press
OOED Programming Process
A six step process for writing an OOED computer program:
1. Define problem.
2. Create interface
3. Develop logic for action objects
4. Write and test code for action objects
5. Test overall project
6. Document project in writing
No matter how well a program is written, the objective is not achieved if the program solves the wrong problem.
Step One: Define Problem
Before you can create a computer application to solve a problem, you must first clearly define it
Must identify the data to be input to the program and the results to be output from it.
Sketching an interface is a good way to understand the problem and to communicate your understanding to other people
Denote input and output objects as well as action objects--those for which code (instructions) are needed
Sketch of Vintage Video Interface
Vintage Videos
Price
Amount Due
Calculate Exit
Output
Action Objects
Taxes
Input
Step Two: Create Interface
Once you have defined problem and sketched interface, you are ready to create interface
Doing this with VB is quite easy--select objects and place them on VB form following sketch
For Vintage Video, only need three objects: buttons for action textboxes for input and output labels for descriptors
Vintage Video Interface
Step Three: Develop Logic for Action Objects
Need to think about what each action object should do in response to an event
This is the logic for each object
Use Input/Processing/Output (IPO) Tables and Pseudocode to develop the logic
IPO Tables show the inputs, outputs, and the processing to convert inputs into outputs
IPO Table for Calculate Button
Input Processing Output
Video price
Taxes = 0.7 x price
Amount due = price + taxes
Taxes
Amount due
Using Pseudocode
An important part of the developing the logic for action objects is generating corresponding pseudocode.
Pseudocode involves actually writing a program in English rather than in a computer language.
When the actual computer program is written, the pseudocode is translated into computer language.
A pseudocode program is useful for two reasons: The programmer may use it to structure the
algorithm's logic in writing. It provides a relatively direct link between the
algorithm and the computer program
Pseudocode for Calculate Button
Begin procedureInput Video PriceTaxes = 0.07 x Video PriceAmount Due = Video Price + TaxesOutput Taxes and Amount Due
End procedure
Step Four: Write and Test Code of Action Objects
Once you learn the vocabulary and syntax of a language, you should be able to convert the logic embodied in the pseudocode into a computer language. In our case, we use VB
You need to test each code for each action object as they are created
Once the object code is written, the programmer must test and correct it. This stage is referred to as debugging, since it involves removing "bugs".
Use test data for which you know the correct answer to test the object code
VB Code for Calculate Button
Private Sub cmdCalc_Click()‘ This object calculates the Taxes and Amount Due ‘ given the Video Price
Dim Price as Currency, Taxes as CurrencyDim AmountDue as Currency
Price = val(txtVideoPrice)Taxes = 0.07*PriceAmountDue = Price + TaxestxtTaxes = str(Taxes)txtAmountDue = str(AmountDue)
End sub
Step Five: Test Overall Project
Even with extensive testing, some bugs can often be found in most commercial software.
With computer software, each program instruction must be absolutely correct. Otherwise, the whole program might fail.
BE SURE to test your program in the actual environment and on the actual data on which it will be used
It this course:
We are going to use variables a lot in VBA. Variables will be used a lot with the Subroutines
and Functions We shall use the objects associated with the
elements of an Excel workbook, and a Word document.
We shall not create any custom objects. In order to do this, we should be qualified programmers.
Programming constructs
Linear construct Branching construct Looping (cyclic) construct
Processing blocks
Begin/End
Stand alone statement
YES NO Decision Block
Cycle block
Linear construct
Branching construct
YES NO
Looping construct