250 likes | 516 Views
Introduction to Programming and VBScript. Data and Information. Data are raw facts Examples of data include transactions, dates, amounts, etc. Information are data that have been processed into a usable form Information includes tables, documents, charts, etc.
E N D
Data and Information • Data are raw facts • Examples of data include transactions, dates, amounts, etc. • Information are data that have been processed into a usable form • Information includes tables, documents, charts, etc. • Goal of computer applications is to process data into information
Six Basic Computer Operations • 1. A computer can receive (input) data • 2. A computer store data in memory • 3. A computer can perform arithmetic and manipulate text strings • 4. A computer can compare the contents of two memory locations and select one of two alternatives • 5. A computer can repeat a group of operations • 6. A computer can output information (processed data)
Programs and Programming A program is a very specific set of rules that tell the computer which switches should be "ON" or "OFF". The process of creating a program is called programming. The computer only knows what it is told through programs, so they must be accurate and very specific.
What is 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 • Testing the program to ensure it accomplishes task or solves problem defined earlier • Implementing the program to accomplish the task or solve the problem
Types of Computer Languages • Procedural: Monolithic programs that run from start to finish with no intervention from user other than input • Basic, QBasic, QuickBasic • COBOL, FORTRAN, C • Object Oriented/Event Driven (OOED): Programs that use objects which respond to events; use small segments ot code for each object • Visual Basic • Visual C++ • Scripting languages: Simplified languages that must be interpreted by other software (eg, browser, server software) >>VBScript, Javascipt, PerlScript
Object-Oriented Programming (OOP) OOP uses objects, or self contained modules that combine data and program code which 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 almost a OOP language Users can combine the objects with relative ease to create new systems or extend existing ones. Properties of objects are attributes associated with an object Methods of objects are those activities that the object can carry out Objects respond to events
Web Development with Scripting • Web development: creating applications that run on either or both the server and browser software • Web development process 1. Define problem (functional definition) 2. Determine form of input from browser (html) 3. Determine form of output to browser (html) 4. Write and debug client-side script to pre-process browser input 5. Write and debug server-side script to process input to generate output to browser in form of HTML 6. Test entire project (client and server side) • Final project must solve problem defined in 1st step
Declaring Variables • Declare ALL variables with the DIM statement • General form: • Dim Variable1, variable2 • For example, • Dim MyName, MyValue • Dim Taxes, Price • Two or more variables can be declared with same Dim statement • Begin ALL pages with the <%Option Explicit%> statement even before the <HTML> statement. This forces all variables to be declared--a good thing to do • If you fail to declare a variable after the Option Explicit statement has been entered, an error occurs at Run time
Keeping Tags and Code Apart • In an .asp page, tags and code are separated by delimiters • Tags appear and work in an asp document just like they do in a normal HTML page. • Code is interspersed in page surrounded by <% and %> code delimiters, eg, <% Dim ftsize, ftcolor %> • Multiple lines of code can be surrounded by one set of code delimiters,eg, <% Dim ftsize, ftcolor For ftsize = 1 to 7 %>
Using comments • You can add internal documentation in the form of VBScript comments by using apostrophes • Anything inside code delimiters beginning with an apostrophe is ignored by the server when interpreting an asp page, eg <% ‘ This code loops 7 times changing the <ignored ‘ font size each time through the loop <ignored for ftsize = 1 to 7 <executed • You can delete code without removing it by preceding it with an apostrophe
Line Continuation • VbScript code statements in code delimiters can be continued by ending a statement with a space and an underline, eg, <% if weekday(date) > 3 and _ weekday(date) < 6 then %> is the same as <% if weekday(date) > 3 and weekday(date) < 6 then %>
Assignment Statements • Used to assign a value to a variable, eg <% Taxes = VideoPrice * TaxRate %> • A variable or expression will be on right of assignment statement • An expression is a combination of one or more variables and/or constants with operators • Operators are symbols used for carrying out processing
Arithmetic Operators • () for grouping + for addition • ^ for exponentiation - for subtraction • - for negation • * for multiplication • / for division • \ for integer division • mod for modulus
Hierarchy of Operations • Operations within parentheses ( ) • Exponentiation (^) • Negation (-)' • Multiplication and division (*,/) • Integer division (\) • Modulo arithmetic (Mod) • Addition and subtraction (+,-) • String concatenation (&)
Arithmetic Example <%Gross = 3 * (Salary - Taxes)^2 + Bonus/Months %> 3 1 2 5 4 (order) • Order 1 Subtract Taxes from Salary 2 Square the result 3 Multiply this result by 3 4 Divide Bonus by Months 5 Add result to first expression
Displaying Variable Values • To output the result of an assignment statement, use the following statement in the asp page: <% =variable%> • For example, to compute taxes and total cost: <% Option Explicit %> <HTML> <% Dim TotalCost, Videoprice, TaxRate, Taxes VideoPrice = 2.99 TaxRate = 0.07 Taxes = TaxRate * VideoPrice TotalCost = VideoPrice + Taxes %> <% =TotalCost%> </HTML> • Enter this code in Notepad and save as Compute.asp and add this line to default.asp: <a href = “compute.asp”>Compute Example </a>
More on Delimiters • If is possible (and often useful) to include code delimiters inside an HTML tag, eg, <font size = <%=ftsize%>> • In this case, instead of displaying the value of the ftsize variable, the HTML <font size > tag takes on the value of ftsize • If the ftsize variable is equal to, say, 5 then the above tag is the same as <font size = 5> • But since ftsize is a variable, it can take on many different values changing the size of the font
Working with strings • It is possible to work with strings, that is, sequences of characters • Strings can be concatenated, that is, combined with the ampersand operator, eg, <% Option Explicit %> <HTML> <% Dim FirstName, LastName, Name FirstName = “Bill” LastName = “Gates” Name = FirstName & LastName %> <% =Name%> </HTML> • What’s wrong with the assignment statement? • Correct it and save the file as String.asp and add this line to default.asp: <a href = “String.asp”>String Example </a>
Symbolic Constants • We can assign a name to a constant with the Const statement <% const constant name = value %> • Example <% Const TaxRate = 0.07%> • This assigns the constant the same value throughout the program
Using Functions • Sometimes we carry out a specific operation for which aVBScript function already exists to compute a single value • To do this, we use a function that is nothing more than an operation that takes a multiple arguments and generates a single value • variable = functionName(arg1, arg2, …) • Example functions: • Date to return the current date • Int to return the integer part of a number • Len to return the length of a string
Other Functions • Other useful functions include • Abs for absolute value Sqr for square root • Ucase/Lcase to convert to upper/lower case • DateValue for the date corresponding to string argument • FormatCurrency to display a number as currency • Instr to return the location of a substring in a string • Example: change the <%=Totalcost%> statement in compute.asp to <% =formatcurrency(Totalcost)%> and run it again. Note the difference in the output. • For more information on VBScript functions, go to http://msdn.microsoft.com/scripting/VBScript/doc/vbstoc.htm and select Functions