CS4 – lecture 6
description
Transcript of CS4 – lecture 6
CS4 –lecture 6Wednesday, Jan 19, 2011
Roxana Gheorghiu
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)
Numbers and Arithmetic Operations
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
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.1Math.Round(2.67) =3
Built-In Functions (cont.)
Var = expression
(1)Dim myNumber as DoublemyNumber =5.21
(2)Dim myNumber as Double =5.21
NOTE: Dim x as Integer =3, y as Double =5.3
Assignment Statement
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”) returns: 6str.Substring(0,5) returns: “ My”
Clear() =creates an empty stringstr.Clear() str.Txt=“”
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
String Properties and Methods
stringVar =textBox.Text
Ex: Dim str as Stringstr =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 a Text 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)
Input Data from Dialog Box
MsgBox(prompt, button, title)
Ex: MsgBox (“Congratulation !”, 0, “Result Window”)ORMsgBox (“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
Output Data Using a Message Box
(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.
Application