* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download Numbers and Arithmetic Operations
Survey
Document related concepts
Transcript
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu Numbers and Arithmetic Operations Standard Arithmetic Operations: Addition (+) Subtraction (-) Division (/) Multiplication (*) Exponentiation (^) Level of precedence for arithmetic operations: Numbers and Arithmetic Operations Standard Arithmetic Operations -Level of precedence for arithmetic operations: Addition (+) Subtraction (-) Division (/) Multiplication (*) Exponentiation (^) Numbers and Arithmetic Operations Level of precedence for arithmetic operations: Addition (+) Subtraction (-) -LEVEL 3 Division (/) Multiplication (*) -LEVEL 2 Exponentiation (^) –LEVEL 1 NOTE: 1/0 = Infinity Math.Sqrt(-4) =NaN (Not a Number) Built-In Functions Modulo : x Mod y =the reminder when m is divided my n Ex: 20 Mod 2 =0 15 Mod 4 =3 Square root: Math.Sqrt( #number) Ex: Math.Sqrt(9) =3 Built-In Functions (cont.) The greatest integer less than or equal to a number: Int(#number) Ex: Int(2.7) =2 Int(-2.7) =-3 Rounding a number to r decimal places: Math.Round(#number, #r) or Math.Round(#number) Math.Round(2.14 , 1) =2.1 Math.Round(2.67) =3 Assignment Statement Var = expression (1) Dim myNumber as Double myNumber =5.21 (2) Dim myNumber as Double =5.21 NOTE: Dim x as Integer =3, y as Double =5.3 String Properties and Methods Dim str as String =“ My own String ” Length() =returns the number of characters in the string str.Length ->19 ToUpper() =returns the string with letters capitalized str.ToUpper() ->” MY OWN STRING “ ToLower() =returns the string with all letters in lowercase format Str.ToLower() -> “ my own string “ Trim() =returns the string with all spaces removed from the front and back of the string str.Trim() -> “My own String” String Properties and Methods Dim str as String =“ My own String ” Dim str2 as String = “own” Substring (m,n) =the substring that starts at position m and its n characters long IndexOf(newString) str.IndexOf(str2) =str.IndexOf(“own”) str.Substring(0,5) returns: “ My” Clear() =creates an empty string str.Clear() str.Txt=“” returns: 6 String Properties and Methods CStr(number) or number.ToString converts a number into a string CInt(string) converts a String into an Integer value CDbl(string) converts a String into a Double value & used to concatenate two strings str =“One” ; str1 =“ and two” str & str1 returns: “One and two” _ the continuation character Input Data from a Text Box stringVar =textBox.Text Ex: Dim str as String str =txtName.Text If the input data will be used in a MATH expression you will need first to convert it to Double or Integer and then use it Input Data from Dialog Box stringVar = InputBox(prompt, title) Ex: Dim name as String Dim prompt, title as String prompt =“Please give a name” title =“Input box for a name” name =InputBox(prompt, title) Output Data Using a Message Box MsgBox(prompt, button, title) Ex: MsgBox (“Congratulation !”, 0, “Result Window”) OR MsgBox (“Congratulation !”) -> in this case the title is the same string as the name of the main window Note: check *Dailly Schedule* on www.cs.pitt.edu/~roxana/cs4/ for a complete list of possible values for variable button Application (1) Write a VB program that asks a name as input and it will copy that name in a list. (2) Write a VB program that asks a name as input and it will copy the first name in one list and the last name in another list (3) Continue exercise (2) by adding a new button Del, that will delete an entry selected from any list.