2 Numbers and Strings n Most data processing performed using Numbers and Strings n Examples of Numbers: Amount of sales, Student’s grades, Regular commission, Sales’ commission n Examples of Strings: Employee name, Student name, "Success", "Unsuccess". n A Number or a String could be a Variable or a Constant. – Numeric Variable or Numeric Constant – String Variable or String Constant
3 Variables n A storage location in main memory whose value can change during program execution. n These storage locations can be referred to by their names. Ex: EmployeeName n Every variable has three properties: a Name, a Value, and a Data Type. n Types of variables: Numeric and String
4 Rules for Creating Variable Names n Must begin with a letter. n Can contain letters, numeric digits. n Can have up to 255 characters. n Can Not be restricted keyword. Examples of valid Variable names EmployeeName RegCom Speed n Celsius Examples of invalid Variable names 1stChoice Name Picture Max/Min
5 Valid / Invalid Numeric Constants Integer Real number Invalid: 6.8% 33- $ & Valid
6 String Constants n A group of alphanumeric data consisting of any type of symbols. n Must be surrounded by quotation marks n Valid String Constants: "Down By the Sea Shore" "She said, ‘stop, thief!’" "134.23" n Invalid String Constants: ‘Down by the Seashore’ " "She said, "Stop, thief!""
7 Arithmetic Operations & Hierarchy of Operations Hierarchy Operator Operator Level 1 ^ Exponentiation * Multiplication / Division + Addition - Subtraction Note: If any, expressions in parentheses ( ) evaluated first Level 2 Level 3
8 Examples Evaluate the following expressions: x = 3 * / 3 x = 4 ^ (8 / 4) y = / (3 * (10 - 9)) z = ^ 2 m = 6 / 3 + 3
9 Assignment Statements for assigning values to variables Examples for Numeric & String variables EmployeeName = "Bill O’Brien" BasePay = 300 ExtraPay = BasePay *.03 WeeklyPay = BasePay + ExtraPay General syntax VariableName = Value Where:VariableName is the variable’s name Value is a constant, a variable or an expression Rules 1.Only a single variable name at the left of the = sign 2.Only a single variable name, constant or expression to the right of the = 3.Everything to the right of the = must be known to the computer
10 Visual Basic Print Statement n Print is a method used to display data on the screen or a printer. n Usually used to display numbers and strings on objects General syntax ObjectName.Print Outputlist Where:ObjectName is the name of the object/device on witch to display Outputlist represent a list of constants, a variables or an expressions Examples frmIdentity.Print " Bill O’Brien " Printer.Print " Bill O’Brien " picOutput.Print 2 * (3 + 4) picOutput.Print " The class average is "; Total / 23 PicOutput.Print 32 & Chr(176) & " Fahrenheit " & is used for concatenation Gives ° which is the 176th ANSI character (See Appendix A)
12 Data Types Each variable should be assigned a data type. n Numeric variables’ data types – Single (for storing real numbers) – Integer (for storing integers) n String variables’ data type – String
13 Declaring Variable Types n Dim statements are used to declare data type Example: Dim BasePay As Single Dim NomberOfDays As Integer Dim EmployeeName As String
14 Internal Documentation n An apostrophe ( ' ) can be used to indicate comments; comments are ignored by Visual Basic. n The keyword Rem can also be used instead of an apostrophe for comments. n Remarks can also be placed after program statement too.
15 Formatting Output with Print zones n Using commas (,) allows to print data items in different print zones n Using semicolons (;) allows to print data items one after another n Using Tab allows to print data items at target positions – E.g. Tab (25) allows to move the cursor at position 25 in the line LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLI…. Zone 1 Zone 2 Zone 3 Zone 4 Print Zones
16 Input from Input box General syntax InputBox(Prompt, [Title]) Where:Prompt is a string that tells the user what information to type Title is the caption that appear in the title bar Example EmployeeName = InputBox("Enter Client name", "Name") Will show the input box below to the user. The data typed by the user in the Text box will be assigned to the variable EmployeeName.
17 Input from a file Use Notepad to create a sequential file 1) Start/Programs/Accessories/Notepad to open Notepad 2) Type the following content in Notepad: 3) Save the file under the name "Account.txt" Read data from a sequential file: Syntax Open " FileName" For Input As #n Input #n, Var1, Var2, Var3…Varn Close #n Where:. FileName is the name of the file that contains data to be read. n is the reference number of the file (could be 1, 2, 3, …). Var1, Var2, Var3…Varn, are the names of the variable that will assigned data items read from the file. "CL001","Bill O’Brien",234,7,24 "CL002","John Diaz",321,10,15 "CL003","Nathalie Cole",543,12,16 "CL004","George Pataki",231,6,20