1 / 48

Programs and programming

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.

yana
Download Presentation

Programs and programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. • The computer only knows what it is told through programs, so they must be accurate and very specific.

  2. 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

  3. 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

  4. 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

  5. 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

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

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

  8. Language translation process Total = 0 Current = 10 Do while current <> 0 Total = Total + Current Current = Current – 1 Loop 10111000 11011000 10110001 00000100 11100010 10101000 11100011 00010001 10111000 11011000 10110001 00000100 11100010 10101000 11100011 00010001 11100010 10101000 Translation Program High level language program Machine language program

  9. 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

  10. 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.

  11. 5 Age Variables • Named memory locations where you can store data values • Can imagine them as buckets with a label on them

  12. 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 8 5 Age

  13. 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

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

  15. 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

  16. Data types

  17. Data types

  18. Data types

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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.

  25. 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

  26. 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

  27. 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

  28. 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.

  29. 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.

  30. 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 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 – mouse click, key press

  31. 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.

  32. 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

  33. Sketch of Vintage Video Interface

  34. 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

  35. Vintage Video Interface

  36. 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

  37. IPO Table for Calculate Button

  38. 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

  39. Pseudocode for Calculate Button Begin procedure Input Video Price Taxes = 0.07 x Video Price Amount Due = Video Price + Taxes Output Taxes and Amount Due End procedure

  40. 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 asdebugging, since it involves removing "bugs". • Use test data for which you know the correct answer to test the object code

  41. 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 Currency Dim AmountDue as Currency Price = val(txtVideoPrice) Taxes = 0.07*Price AmountDue = Price + Taxes txtTaxes = str(Taxes) txtAmountDue = str(AmountDue) End sub

  42. 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

  43. 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.

  44. Programming constructs • Linear construct • Branching construct • Looping (cyclic) construct

  45. Processing blocks Begin/End Stand alone statement Decision Block NO YES Cycle block

  46. Linear construct

  47. Branching construct NO YES

  48. Looping construct

More Related