CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
-
Upload
mervin-dixon -
Category
Documents
-
view
216 -
download
0
description
Transcript of CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
![Page 1: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/1.jpg)
CS4 –lecture 6Wednesday, Jan 19, 2011
Roxana Gheorghiu
![Page 2: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/2.jpg)
Standard Arithmetic Operations:Addition (+)Subtraction (-)Division (/)Multiplication (*)Exponentiation (^)
Level of precedence for arithmetic operations:
Numbers and Arithmetic Operations
![Page 3: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/3.jpg)
Standard Arithmetic Operations -Level of precedence for arithmetic operations:
Addition (+)Subtraction (-)
Division (/)Multiplication (*)
Exponentiation (^)
Numbers and Arithmetic Operations
![Page 4: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/4.jpg)
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
![Page 5: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/5.jpg)
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
![Page 6: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/6.jpg)
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.)
![Page 7: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/7.jpg)
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
![Page 8: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/8.jpg)
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
![Page 9: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/9.jpg)
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
![Page 10: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/10.jpg)
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
![Page 11: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/11.jpg)
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
![Page 12: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/12.jpg)
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
![Page 13: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/13.jpg)
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
![Page 14: CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.](https://reader035.fdocuments.in/reader035/viewer/2022062906/5a4d1b267f8b9ab0599973da/html5/thumbnails/14.jpg)
(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