programs and programming

48
Programs and programming A program is a very specific set of A program is a very specific set of rules that tell the computer what to rules that tell the computer what to do. It is 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 The process of creating a program is called programming. called programming. The computer only knows what it is The computer only knows what it is told through programs, so they must told through programs, so they must be accurate and very specific. be accurate and very specific.

Upload: yana

Post on 14-Jan-2016

26 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Programs and programming

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.

Page 2: Programs and programming

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

Page 3: Programs and programming

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

Page 4: Programs and programming

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

Page 5: Programs and programming

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

Page 6: Programs and programming

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++

Page 7: Programs and programming

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

Page 8: Programs and programming

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

Page 9: Programs and programming

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

Page 10: Programs and programming

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.

Page 11: Programs and programming

Variables

Named memory locations where you can store data values

Can imagine them as buckets with a label on them

5

Age

Page 12: Programs and programming

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

Page 13: Programs and programming

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

Page 14: Programs and programming

Variable Name Example(Yes or No?) Radius 8_student Number_1 Number2 Hours_worked Gross Pay Net/Pay

Page 15: Programs and programming

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

Page 16: Programs and programming

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

Page 17: Programs and programming

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

Page 18: Programs and programming

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

Page 19: Programs and programming

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

Page 20: Programs and programming

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

Page 21: Programs and programming

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

Page 22: Programs and programming

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

Page 23: Programs and programming

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

Page 24: Programs and programming

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.

Page 25: Programs and programming

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

Page 26: Programs and programming

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

Page 27: Programs and programming

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

Page 28: Programs and programming

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.

Page 29: Programs and programming

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.

Page 30: Programs and programming

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

Page 31: Programs and programming

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.

Page 32: Programs and programming

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

Page 33: Programs and programming

Sketch of Vintage Video Interface

Vintage Videos

Price

Amount Due

Calculate Exit

Output

Action Objects

Taxes

Input

Page 34: Programs and programming

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

Page 35: Programs and programming

Vintage Video Interface

Page 36: Programs and programming

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

Page 37: Programs and programming

IPO Table for Calculate Button

Input Processing Output

Video price

Taxes = 0.7 x price

Amount due = price + taxes

Taxes

Amount due

Page 38: Programs and programming

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

Page 39: Programs and programming

Pseudocode for Calculate Button

Begin procedureInput Video PriceTaxes = 0.07 x Video PriceAmount Due = Video Price + TaxesOutput Taxes and Amount Due

End procedure

Page 40: Programs and programming

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

Page 41: Programs and programming

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

Page 42: Programs and programming

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

Page 43: Programs and programming

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.

Page 44: Programs and programming

Programming constructs

Linear construct Branching construct Looping (cyclic) construct

Page 45: Programs and programming

Processing blocks

Begin/End

Stand alone statement

YES NO Decision Block

Cycle block

Page 46: Programs and programming

Linear construct

Page 47: Programs and programming

Branching construct

YES NO

Page 48: Programs and programming

Looping construct