Download presentation
Presentation is loading. Please wait.
Published byArchibald Doyle Modified over 9 years ago
1
CS0004: Introduction to Programming Variables – Numbers
2
Review The basic form of setting a property of a control: controlName.property = setting txtBox.ForeColor = Color.Red Private Sub btnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton.Click ‘Code goes here End Sub There’s only two parts you need to worry about: 1. btnButton_Click is the name of the event procedure 2. btnButton.Click tells the procedure to listen for the click event on the button btnButton. To make an event procedure you can: 1. Double click on the control (creates the default event procedure for the control) 2. Click on the control then click the “Events” button at the top of the Properties Window
3
Numeric Literals and Arithmetic Operations Data appearing as numbers are called numeric literals. 33 44 9.9 With these we can perform various arithmetic operations with arithmetic operators: Addition + Subtraction - Multiplication * Division / Exponentiation ^ Grouping Symbols () The arithmetic operators follow normal order of operations
4
Arithmetic Operations Example See Example on Course Webpage New Things: Numeric Literals Arithmetic Operations List Box lstExample.Items Accesses the list boxes items lstExample.Items.Clear() Removes all items from the list box lstExample.Items.Add(parameter) Adds the supplied parameter to the list as a list item Add and Clear are known as methods A method is a process that performs a task for a particular object. What is in the “ () ” in the Add method is called a parameter. A parameter is information passed to the method to be processed Not all methods require parameters ( Clear, for example, doesn’t)
5
Variables In math problems quantities are referred to by name. For example, if we wanted to see how far a car traveled if the car was going 50 miles per hour for 14 hours? How would we solve this? Take the speed (50 miles per hour) Multiply it by the time elapsed (14 hours) To get the distance traveled distance = speed x time elapsed This can be solved by a computer program.
6
Variables Example See Example on Course Webpage New Things: Variables – speed, timeElapsed, and distance are variables Variables can be thought of boxes where we store values. You access variables by using their names. Camel Casing When you want something to be named a series of words DO NOT put spaced between the words, use camel casing instead. Camel casing is capitalizing the next work instead of using a space. timeElapsed Assignment Statement An assignment statement puts a value into a variable. Uses the assignment operator ( = )’ speed = 50 D eclaration Statement It is good practice to declare a variable before using it. This allows the program to free up space for the variable in memory. Dim speed As Double
7
Declaration Statement The form of a declaration statement in VB is: Dim variableName As type variableName is whatever you decide to name the variable Naming rules: 1. Variable names must start with an alphabetic character or an underscore (_) 2. After the first character they can have any alphabetic, numeric, or underscore character 3. Keywords cannot be used as variable names type is what type of information the variable can hold. Two numeric types: Double – can hold positive or negative floating-point (decimal) values Example: 3.4, 90.222, 4.97635 Integer – can hold positive or negative whole numbers Examples: 1, 3, 573
8
Initialization If a variable is assigned a value right after it is declared, you can combine the two into one statement: Dim speed As Double speed = 50 Becomes… Dim speed As Double = 50 This is called an initialization statement. Again, this makes the variable speed have to value of 50
9
Some Built-In Functions You can do some more complex math operations such as square root and round. To do this, we use some of VBs built-in functions. Functions are methods that return a value. Returning a value means doing some computation and supplying some value where the function is used (or called). Square Root and Round are functions of the Math object. So, to access them, you must do: Math.Sqrt(parameter) Math.Sqrt(9) is 2 Math.Round(paramer) Math.Round(3.6) is 4 Sometimes you want to convert a double into an integer by cutting off the decimal portion Int(parameter) Int(3.6) is 3
10
Notes You can declare multiple variables in one line. Dim a As Double Dim b As Double Becomes… Dim a, b As Double Or Dim a As Double = 2 Dim b As Double = 5 Becomes… Dim a As Double = 2, Dim b As Double = 5 In addition to the mathematical operators mentioned before, there are two specific for integers: Integer Division ( \ ) 14 \ 3 is 4 Modulus – ( Mod ) 14 Mod 3 is 2
11
Notes You can mix numeric literals, variables, and arithmetic operators can be mixed answer = x + (4 - y) * 6 Often times you want to perform an operation to a variable then store it back into the original variable Example: x = x + 1 You can simplify this by using the += operator Example: x += 1 There are two special numeric values in VB NaN – Not a Number Example: Math.Sqrt(-1) Infinity Example: 1/0
12
Numbers Notes Example See Example on Course Webpage New Things: Initialization Multiple declarations in one line Integers -= operator Square Root Round Int Integer Division Modulus
13
Errors Two Types Introduced Before: Syntax Error – Gramatical Errors to the VB language Misspellings, omissions, incorrect punctuations, etc. Logical Error – When the program does not perform the way it is intended Typing a = b + c when you mean a = b – c, for example Another Type: Runtime Error – Some error the compiler could not check before making the executable. Often due to influence outside of the program Error List Window
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.