290 likes | 393 Views
Outline. Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA). ENGR 112. Programming Languages. Types of Programming. Procedural Strict sequence of processing steps Distinct start/stop points in program.
E N D
Outline • Software and Programming • Program Structure • Tools for Designing Software • Programming Languages • Introduction to Visual Basic (VBA)
ENGR 112 Programming Languages
Types of Programming • Procedural • Strict sequence of processing steps • Distinct start/stop points in program #include <stdio.h> main() { printf("Content-type: text/html\n\n"); printf("<html>"); printf("Hello World!"); printf("</html>"); }
Types of Programming • Event-Driven • Waits for an “event” to trigger program execution • Modular programming • No start/stop point in program
Types of Programming • Event-Driven
Event-Driven Procedure #1 • Private Sub cmbDates_Click() • Me.txtSSNScanned.SetFocus • Call scanSSN • End Sub • Private Sub cmdEndProgram_Click() • If endProgram Then • End • Else • endProgram = False • End If • End Sub • Private Sub txtSSNScanned_GotFocus() • If Me.cmbDates.Text = "" Then • MsgBox ("You need to enter a date!") • Me.cmbDates.SetFocus • Exit Sub • End If • End Sub Procedure #2 Procedure #3
Programming Languages • Assembly Language • Fast and most efficient • Very difficult to program in • Basic • Beginner’s All-purpose Symbolic Instruction Code • Easy to use • Lacks total control
Programming Languages • Fortran • FORmula TRANslator • Great for mathematical programming • Not good for other uses, e.g. graphics • C and C++ • Powerful tools • Harder to use/learn than BASIC • Can do anything
Programming Languages • Visual Basic • Develop windows programs easily • Powerful tool • Not as much control as Visual C • VBA (Visual Basic for Applications) • Visual C and C++ • Develop windows applications • Not easy to learn
VBA Macro Programming • VBA is a general purpose programming language that comes standard with Excel or Office. • Using VBA with Excel, powerful engineering analysis tools can be developed quickly and with minimum cost. • VBA can be used for many engineering tasks • communicating with engineering databases • analyzing engineering data • automating worksheet construction • engineering modeling and simulation • creating charts and engineering wizards (i.e. dialog boxes) • creating GUI's
Advantages of VBA • Programmer does not need to be an expert Windows programmer • Programmer creates GUI and defines what happens when user interacts with it • Events are generated • Push a button, move mouse, etc. • This is called event-driven programming
Visual Basic Overview • Objects • Events • Numbers • Strings • Variables • Operators
Objects & Events Label Picture box (image) Text box Picture box (text) Command button Elements of a Visual Basic
The most important properties (at least for the purpose of this course) are: Name (object’s name – important!!) Caption (text to be displayed by the object) Font (what the text looks like) Visible (how it links to other applications) Object Properties
Objects With Names lblEngr112 picCOE txtInputA, txtInputB picResults cmdAddButton
Visual Basic Events • Change initiated by user • Load a Form • Click on a button • Mouse down/up, Mouse drag • Key down, key press, etc. • Causes an event procedure (subprogram) to execute
Data Constants (data that does not change) Numbers (e.g., 1234.56) Strings (e.g., “this is a string”) Variables (names of data holders) Numbers Strings Operators Arithmetic (+, -, *, /, etc.) String (&) Elements of Visual Basic
Visual Basic Integers • Sequence of digits with no • Commas • Decimal points • Negative numbers preceded by ‘-’ • Positive numbers optionally preceded by ‘+’
Single Precision Real Numbers • Standard Representation • Integer (whole number) portion • Decimal point • Unsigned integer (fractional) portion
Visual Basic Strings • String: • Sequence of characters (a, b, c, ..., 0, 1, 2, ..., ~, !, ...) treated as a unit • Enclosed in double quotes in VB statement • “this is a string” • “John Smith” • “737-2357” • “a + b = “
Visual Basic Variables • Variable • Symbolic name for data value • Name of data “holder” • Name of a location in random access memory • Variable names rules • Must begin with a letter • May contain only letters, digits, and underscores (_) • Up to 255 characters long
Variable Names • a, b, c, ..., x, y, z • distance, speed, time, average • rateOfIncrease, startingTime, hoursPerWeek • rate_of_increase, starting_time, hours_per_week
Declaring Variable Types • Two options • Implicit declaration • Explicit declaration • Explicit declaration Dim name As String Dim count As Integer Dim average As Single Dim nextItem As Variant • Explicit declaration is always preferable!!
VB Arithmetic Operators • Addition (+) a + b • Subtraction (-) a - b • Multiplication (*) a * b • Division (/) a / b • Exponentiation (^) a ^ b
Arithmetic Order of Evaluation 1. Exponentiation (R - L) 2. Multiplication & division (L-R) 3. Addition & subtraction (L-R)
4 + 3 + 7 * 2 + 3 * 3 + 1 14 9 7 21 30 31 Examples of Evaluation (1)
2 1 a + b 1 c 1 d e f g h + - Converting Formulas to VB (1.0 / (a + b)) ^ 2 / ((1.0 / c + 1.0 / d) * (e / f - g / h))
Concatenation (&) String Operator “good” & “bye” “goodbye”