150 likes | 221 Views
Chapter 3 (ctd) Sections 3.3 - 3.5 (partial). Input/Output NUMBERS & STRINGS. The basic arithmetic operations (for now…). + , -, *, /, ^ order of precedence: ( ) ^ (or **) *, / +, -. Using PICTURE BOXES to display numeric values (Output).
E N D
Chapter 3 (ctd)Sections 3.3 - 3.5 (partial) Input/Output NUMBERS & STRINGS
The basic arithmetic operations (for now…) • + , -, *, /, ^ • order of precedence: • ( ) • ^ (or **) • *, / • +, -
Using PICTURE BOXES to display numeric values (Output) • First, we clear out the picture box, using a method provided for us by Visual Basic: • Cls- used to clear a picture box PictureBox.Cls • Next, we use another method called Print to display the desired number: • Print - used to display a value PictureBox.Print nwhere n is a numeric value
The basic plan... • Create a command button & a picture box; when the user clicks the command button, a value will be displayed in the picture box. • Write a CommandButton_Click() event procedure containing PictureBox.Cls & PictureBox.Print n statements
Variables - use to receive input & intermediate values within a program • Variables are symbols that represent values that have been stored in the computer’s memory. • Ex: x = 5 y = 3 Picture1.Print x; y; x+y; x*y • Vertical & horizontal spacing
Some Examples: • Page 80: • # 4,5,6,18,19,22 • # 29, 30, 33, 34, 35, 36, 37, 38
The basics about strings • String constants • “abcdefg”, “3”, “?;#&*!” • String variables • c = “abcdefg” • numbers = “24812”
Using PICTURE BOXES to display character data (Output) • Use the method Print Picture1.Print “xyz” alpha = “abcdef” Picture1.Print alpha; “ghij”; 7 • Horizontal spacing leaves no blank spaces • Can mix numeric & character data
Variable types • A variable of typestringis capable of storing only character values. • A variable of typesingleis capable of storing only numeric values. • It is good programming practice to declare all variables at the top of the event procedure: Dim x As Single Dim y As String
Using TEXT BOXES for String Input and Output • Input: • You can read character string values into your program from a user’s response in a text box: answer = TextBox.Text • Output: • You can use text boxes to display character string values from your program: TextBox.Text = “15”
Characters, digits & numbers • Text boxes can containonly string data, so we must make use of special Visual Basic functions if we want to use this data in a numeric sense. • Val (“123”) is equal to the number 123 • Str (123) is equal to the string “123”
Concatenation • Two strings can be combined to form a new string. This process is called Concatenation and is represented by an ampersand (&) • E.g. • str1 = “Hi” • str2=“There” • PicBox.Print str1 & str2 • The result will be • HIThere (no space)
If we want to put a space between the two strings, we must do it manually • E.g. • PicBox.Print str1 & “ “ & str2
Using TEXT BOXES to receive or display numeric results • Input: • You can read numeric values into your program from a user’s response in a text box: answer = val(TextBox.Text) • Output: • You can use text boxes to display numeric values from your program: TextBox.Text = str(number)
Lab. Due Today • Page 96 • #27, 30, 32