Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT.

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
VBA Modules, Functions, Variables, and Constants
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
Visual Basic: An Object Oriented Approach 4: Simple Programming in VB.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
1 Pertemuan 03 Representing Data Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
CHAPTER THREE Representing Data: Constants and Variables.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Lecture 8 Visual Basic (2).
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Chapter 2: Using Data.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
CPS120: Introduction to Computer Science
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright Curt Hill Variables What are they? Why do we need them?
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CPS120: Introduction to Computer Science Variables and Constants.
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., Representing Data Constants and Variables chapter THREE.
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Representing Data: Constants and Variables
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Data Types, Arithmetic Operations
2. Understanding VB Variables
Variables and Arithmetic Operations
IDENTIFIERS CSC 111.
Chapter 6 Variables What is VBScript?
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
C++ Data Types Data Type
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
VB Variables and Data
Chapter 2: Introduction to C++.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT

Copyright © Don Kussee 1120-Ch3 #482 Data Types Strings –“Visual Basic is easy to learn and use.” –“Don” “X” “286 Main Street” “3” –“” ‘ The empty string Numbers (can be used for math functions) –286 – –

Copyright © Don Kussee 1120-Ch3 #483 Constants Fixed at run time Cannot be changed Literal constant - the value itself Symbolic constant - pi =

Copyright © Don Kussee 1120-Ch3 #484 Numeric values ‘0 to left of the decimal not required ‘leading 0’s ignored - trailing 0’s contain information, carried if possible 8.9E-6 = E+3 = Note: no commas or $ in the number

Copyright © Don Kussee 1120-Ch3 #485 Symbolic Constant Use a symbol - SALESTAXRATE - defined once, used many times Name characters letters & numbers 1st character must be a letter Spaces not allowed, SALES_TAX_RATE Reserved words not allowed - problem All Caps is the convention

Copyright © Don Kussee 1120-Ch3 #486 Define & Declare Constants Const is a reserved word used to indicate this symbol name and it’s value Const SALES_TAX_RATE = Const COMPANY_ADDRESS = “347 Main Street, Orem UT, 84058” Const DISK_ERR= “There is no disk in A:”

Copyright © Don Kussee 1120-Ch3 #487 No Magic Numbers Only 0, 1, -1, “” allowed as literal constants Named constant easier to understand Constant reduces a the chance for typos Makes a change throughout the program easy and correct, especially when the same value used for two different purposes. TAX_RATE goes from to Const SHIPPLING_PER_TON =.081

Copyright © Don Kussee 1120-Ch3 #488 “Special” numbers to remember Bit - 1 or 0 - All the computer can use. Byte - 8 bits –256 different codes - 0 through 255 K is 1,000 Bytes (really 1024 Bytes) Meg is 1,000,000 Bytes (really 1,024,000) Gig is 1,000 Meg Bytes Tara is 1000 Gig Bytes

Copyright © Don Kussee 1120-Ch3 #489 Visual Basic Constants Shipped as a part of Visual Basic A large number related to various objects BackColor, ForeColor, … –vbRed vbYellow, vbBlue, vbGreen … Alignment –vbLeftJustify, vbRightJustify, vbCenter

Copyright © Don Kussee 1120-Ch3 #4810 Variables A named memory, used as temporary holder Current value is replaced forever by the new Define & Declare DIM Name as String –name –data type Initialization Name = “Don” –VB makes it “” (empty string ) or 0

Copyright © Don Kussee 1120-Ch3 #4811 Variable Names Name must be unique & should have meaning No Keywords Case Insensitive - but matches the Dim Dim is the keyword used to tell VB to –Give it a name - to be used later in the program –Set aside an amount of memory Name 255 characters, 1st letter Alpha – no spaces, #, !, $

Copyright © Don Kussee 1120-Ch3 #4812 Variable data types What kind of container Quart of water 4 books and 3 folders of papers 8 ski poles Piano Load of logs Family of 6 Quart of milk

Copyright © Don Kussee 1120-Ch3 #4813 Data types defined in VB String –Variable length –Fixed length Numbers –Integer, Long, Single, Double, Currency Others - Variant User defined

Copyright © Don Kussee 1120-Ch3 #4814 Number data types Whole numbers –Integer - small –Long - big Fractional numbers –Single - Big –Double - Bigger –Currency Special Biggest –Date Special

Copyright © Don Kussee 1120-Ch3 #4815 Additional Data Types Byte 0 to 255 Boolean True or False Date Jan,1, 100; Dec.31,9999 Variant Date, Time, Universal Object (pointer) User defined - structures

Copyright © Don Kussee 1120-Ch3 #4816 Data type String Variable length - 2 Billion max –Dim FirstName as String –FirstName = “Don” FirstName= “Victoria” –Size changes automatically –Size 10 bytes + 1 byte per letter Fixed length - 65,400 max –Dim ZipCode as String * 5 –ZipCode = “48058”

Copyright © Don Kussee 1120-Ch3 #4817 Data Type Integer Whole number 89, -37, , Memory set aside 2 Bytes Maximum Size +-32K ( to ) Dim Age as Integer Age = 100 Fractional values are dropped

Copyright © Don Kussee 1120-Ch3 #4818 Data Type Long (Integer) Whole number 89, -37, , Memory Set aside 4Bytes Maximum Size very big –-2,147,843,648 to 2,147,843,648 Dim BankBallance as Long ‘4 bytes BankBallance = ‘ No comma’s

Copyright © Don Kussee 1120-Ch3 #4819 Data Type Single Fractional number Maximum - Big number (-3.4E -45 to 3.4E 38 ) Memory Set aside 4 Bytes 7 Significant Digits – Dim Wage as Single Wage = 5.90

Copyright © Don Kussee 1120-Ch3 #4820 Data Type Double Fractional number – Maximum - Very big (-1.9E 324 to 4.9E 308 ) Memory Set aside 8 Bytes 15 Significant Digits – Math takes six time

Copyright © Don Kussee 1120-Ch3 #4821 Data Type Currency Fractional number with 4 decimal places Maximum Very big (15.4 digits) Memory Set aside 8 Bytes Dim HouseLoan as Currency HouseLoan = Math takes six times

Copyright © Don Kussee 1120-Ch3 #4822 Decision rules for Standard Data Types Math to be done Size change? F String ! V String ! Currency? Currency ! No Yes No Yes

Copyright © Don Kussee 1120-Ch3 #4823 Decision rules for Math Data Types Decimals? > 7 Digits ? > 32 K ? Single Double Long Integer No Yes

Copyright © Don Kussee 1120-Ch3 #4824 Review of Standard Data Types Type Bytes Limit String 10 + character 2,000,000,000 Integer K Long 4 Big Single 4 7 digits Double 8 15 digits Currency digits

Copyright © Don Kussee 1120-Ch3 #4825 Define, Declare and Initializing Variables Dim name as Data type Dim Name as String ‘ Empty string Name = “Don” Dim Age as Integer ‘ 0 value entered Age = 84 Dim WageRate as Single ‘ 0.0 value entered WageRate = 5.76

Copyright © Don Kussee 1120-Ch3 #4826 Variable Scope Depends on where and how declared –Scope How declared –Local - Dim, Const, Static, ReDim –Module - Dim, Private –Global - Global, Public Right mouse click will show properties of variable

Copyright © Don Kussee 1120-Ch3 #4827 Variable Scope & Life Procedure Declaration –Scope - local to procedure –Life - length of procedure Form Declaration –Scope - global to all procedures in form –Life - of application Global Declaration –Scope - global to application

Copyright © Don Kussee 1120-Ch3 #4828 Static Vs Dim Initializes Variable the first time only, then the value is maintained for the remainder of the life of the program. Reuse of the variable (by entering the procedure), does not reinitialize the variable. Scope is unchanged. Static Joe As Integer Dim Joe As Integer

Copyright © Don Kussee 1120-Ch3 #4829 Assignment operator = Target = Source ‘ required direction –3 = A if A = 7 and B = 2 then after A = B what is contained in both A and B ??? Target must be a variable Right side of equation is completely evaluated, then transferred over = Auto-conversion will occur unless forced by programmer – But only in this language

Copyright © Don Kussee 1120-Ch3 #4830 String Concatenation & Adds a string to the end of another string String1 = “The big bad” String2 = “wolf.” String3 = String1 & String2 String3 now contains The big badwolf. String4 = String3

Copyright © Don Kussee 1120-Ch3 #4831 Type Mismatch error Trying to put a golf club in a gallon jug. Dim X as Integer x = “14 inches” ‘ Error x = ‘No Error auto truncation to 14 Run time error - Depends on values when the program is running so it’s hard to test.

Copyright © Don Kussee 1120-Ch3 #4832 Changes to the Property of a Controls Label1.Caption = “New Caption” Label2.Caption = Label1.Caption Most properties can be Set or Read Most properties can change Some (Name, Tag,…) can not be changed Some are “Read only at run time” and can not be changed

Copyright © Don Kussee 1120-Ch3 #4833 Variable (or Const) Scope What Variable can be accessed (read or changed) by a different domain Local - Dim within a Control_Event( ) –can only be accessed it’s domain Module -Dim in Form General Declarations –accessed by all the form’s controls domain-bad Global -Global in Form Declaration –anyone can access - generally bad

Copyright © Don Kussee 1120-Ch3 #4834 Variable Scope Local variables can not have the same name in the same procedure Local variables can have the same name in different procedures Local variables can have same name as Modules/Globals Computer looks local, then module, global All Forms are Global

Copyright © Don Kussee 1120-Ch3 #4835 Variable’s Life Local - while code runs –Except Static variables Module - while the form is in memory Global - while the Program runs Allows re-use of memory

Copyright © Don Kussee 1120-Ch3 #4836 Public Vs Private code Public changes all variables to Global Allows the code to be used by others Violates basic OOP concepts - very unusual

Copyright © Don Kussee 1120-Ch3 #4837 Methods Vs Properties Birds method = Flight,Properties =Red eyes Properties –Store a Numeric or string value –Can be used in an assignment statement Methods –Cannot be used in assignment statements –Must be on a line by itself

Copyright © Don Kussee 1120-Ch3 #4838 Code format Option Explicit Header Declarations Private Sub Object_Event ( ) –Header –Declaration –Procedure code End Sub

Copyright © Don Kussee 1120-Ch3 #4839 Data Type Fixed String Memory - Byte per space (here 26) Dim City as String * 26 Not used space is filled by NULL characters Overflow is truncated

Copyright © Don Kussee 1120-Ch3 #4840 Data Type Date Contains both date and time information Date.Time Memory set aside 8 Bytes Date Jan.1, 100 to Dec.31, 9999 Time 00:00:00 to 23:59:59 –Noon =.5000, Midnight or 00:00:00 = 0

Copyright © Don Kussee 1120-Ch3 #4841 Data Type Boolean True or False Memory set aside 2 Bytes

Copyright © Don Kussee 1120-Ch3 #4842 Data Type Byte Whole positive number 0, 89, 255 Memory set aside 1 Bytes Maximum range 0 to 255 Dim Age As Byte Age =100

Copyright © Don Kussee 1120-Ch3 #4843 Data Type Variant Default data type Memory set aside 17 Bytes Will hold any data type (Byte, Boolean, Date, Double, Integer, Long, Single, String, Objects) and Empty, Error, Nothing, Null type = VarTyp (variable name) ‘defines type of variable contained

Copyright © Don Kussee 1120-Ch3 #4844 Data Type Object A pointer to an object –Do not use Let –Use Set –Use Set = NULL to free pointer before destroying an object.

Copyright © Don Kussee 1120-Ch3 #4845 Multiple Forms All the code is associated with the form and its controls and is stored in the.frm file Load event occurs when ever a form is displayed Form.Unload-code dumped from RAM Form.Load-code brought from disk to RAM Form.Hide-makes form disappear- in RAM Form.Show-Loads & Displays the form

Copyright © Don Kussee 1120-Ch3 #4846 More than one form in a project Add form to project Each form can change it’s or the other form’s properties Form1.Label1.BackColor = Form2.BackColor Move from one form to another with Hide & Show. Each Form will generate a new file

Copyright © Don Kussee 1120-Ch3 #4847 Adding & Deleting a form Adding a new form to a project –Use New Form Icon or Project New Form –Project Explorer shows the additional form Deleting a form from a project –Highlight in Project Explorer window –Project Remove Form New forms generate a new file when saved

Copyright © Don Kussee 1120-Ch3 #4848 Computer Variable’s Terms Constant Variable Declare Initialize Integer Long Single Double String Concatenation Scope Variable lifetime Global scope Module scope Precision