CS4 Lecture 4: Intro to Programming with Visual Basic - Variables & Arithmetic, Study notes of Computer Science

A part of the university of pittsburgh's cs4 summer 2009 course lecture notes. It covers the topics of variables, numbers and strings, arithmetic operations, and literals. The concepts of methods, declaration statements, assignment statements, and variable types. It also introduces the use of control properties as variables and built-in math functions.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-pym
koofers-user-pym 🇺🇸

5

(1)

10 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS#0004#–#Introduc/on#to#
Programming#
Using#Visual#Basic#
Summer#2009#
University#of#Pi@sburgh#
Lecture#4#
5/20/09# CS4#‐#Summer#2009#‐#Lecture#4# 1#
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download CS4 Lecture 4: Intro to Programming with Visual Basic - Variables & Arithmetic and more Study notes Computer Science in PDF only on Docsity!

CS 0004 – Introduc/on to

Programming

Using Visual Basic Summer 2009 University of Pi@sburgh Lecture 4

Chapter 3.1‐3.

Variables

Numbers and Strings

Arithme/c Opera/ons

Vocabulary words

  • Numeric expression
    • A combina/on of literals, variables, and arithme/c opera/ons that can be evaluated to yield a number
  • Increment
    • Add to a value
  • Decrement
    • Subtract from a value
      • Syntax error
        • Gramma/cal error such as misspellings, omissions, or incorrect punctua/on
      • Excep/ons
        • Also called run‐/me errors, happen while a program is running
      • Keywords
        • Reserved words

Variables vs. Literals

  • Variables are symbols that hold a value
    • In math, you use
      • x
      • y
    • In programming you can use
      • total
      • numCupcakes
      • name
  • Literals are values
    • 5
    • “When does class start?”

Declaring Variables

  • Every variable should be declared before it is

used

  • Declare variables at the top of a method
  • Declara/on statement
  • Dim variableName As VariableType
  • Such as:
  • Dim numCupcakes As Integer
  • Dim address As String
  • Dim totalBill As Double

Variable Types

  • Double
    • Numbers that may have decimal component, between about nega/ve and posi/ve 1.8* 308
  • Integer
    • Whole numbers, between about nega/ve and posi/ve 2 billion
  • String
    • Contains zero or more characters
    • “210 S. Bouquet Street” is a string

Control Proper/es as Variables

  • Proper/es of controls are also variables
  • We can change them, even as the program is

running

  • Remember changing the text of a bu@on?
  • Remember turning the text different colors?

Math!

Let’s try some math

  • Open Visual Studio 2008
  • Start a new project
  • While that’s loading, let’s talk about…

Methods

  • Process that performs a task for a par/cular

object

  • Examples of methods:
    • lstBox.Items.Clear()
      • Clear() is a method that acts on the Items property of lstBox
    • lstBox.Items.Add(“Alpha”)
      • Add() is a method that acts on the Items property of lstBox

Built‐in Math Func/ons

  • Math.Sqrt
    • Math.Sqrt(9) = 3
    • Math.Sqrt(2) = 1.414214…
  • Int
    • Nearest whole number less than the one given
    • Int(3.8) = 3
    • Int(‐5.3) = ‐
  • Math.Round
    • Rounds to nearest whole number
    • In case of halfway between two numbers, round to the even number
    • Math.Round(2.5) = 2
    • Math.Round(3.5) = 4

Let’s try some math func/ons

  • Inside the event handler for btnCompute.Click
    • lstResults.Clear()
    • lstResults.Add(Math.Sqrt(25))
    • lstResults.Add(Int(5.2)*Math.Round(4.5) )
    • Others?

Concatena/ng Strings

  • Let’s say you have 2 strings that you want to

join…

  • Dim firstName, lastName, fullName As String
  • firstName = “Ada”
  • lastName = “Lovelace”
  • Join them together with &
  • fullName = firstName & lastName
  • But this gives “AdaLovelace”
  • fullName = firstName & “ ” & lastName
  • Now fullName = “Ada Lovelace”

String Length ‐ Property

  • String property: Length
    • Dim str As String
      • Remember to declare variables before using
    • str = “Good Morning”
      • str.Length is 12
    • Str = “”
      • Str.Length is 0