cs0004: introduction to programming variables – numbers

13
CS0004: Introduction to Programming Variables – Numbers

Upload: archibald-doyle

Post on 26-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS0004: Introduction to Programming Variables – Numbers

CS0004: Introduction to Programming

Variables – Numbers

Page 2: CS0004: Introduction to Programming Variables – Numbers

Review The basic form of setting a property of a control:controlName.property = setting

txtBox.ForeColor = Color.RedPrivate 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 procedure2. 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

Page 3: CS0004: Introduction to Programming Variables – Numbers

Numeric Literals and Arithmetic Operations Data appearing as numbers are called numeric literals.

3 4 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

Page 4: CS0004: Introduction to Programming Variables – Numbers

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)

Page 5: CS0004: Introduction to Programming Variables – Numbers

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 traveleddistance = speed x time elapsed This can be solved by a computer program.

Page 6: CS0004: Introduction to Programming Variables – Numbers

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

Declaration 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

Page 7: CS0004: Introduction to Programming Variables – Numbers

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

Page 8: CS0004: Introduction to Programming Variables – Numbers

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

Page 9: CS0004: Introduction to Programming Variables – Numbers

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

Page 10: CS0004: Introduction to Programming Variables – Numbers

Notes You can declare multiple variables in one line.Dim a As Double

Dim b As Double

Becomes…Dim a, b As Double

OrDim 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

Page 11: CS0004: Introduction to Programming Variables – Numbers

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

Page 12: CS0004: Introduction to Programming Variables – Numbers

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

Page 13: CS0004: Introduction to Programming Variables – Numbers

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