Introduction to Computer CC111 Week 09 Visual Basic 2 Visual Basic Variables 1.

Slides:



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

Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
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.
1.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
CIS 115 Lecture 5.  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can be referred.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Variables and Constants
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 2: Chapter 3: Slide 1 Unit 2 Variables and Calculations Chapter 3 Input,
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
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.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Week 1-Visual Basic: Introduction HNDIT Rapid Application Development.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
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)
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Basic Computation
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic Variables
Data Types, Arithmetic Operations
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Lecture Set 4 Data Types and Variables
Introduction to C++ Programming
Numbers.
CIS16 Application Development Programming with Visual Basic
Unit 3: Variables in Java
Visual Basic Variables
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Introduction to Computer CC111 Week 09 Visual Basic 2 Visual Basic Variables 1

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Intended Learning Objectives Familiar with the Programming Variables Declaration and Initialisation. Effective Choice of Data Types Appreciate the benefits of using Constants. The Assignment Operator and Type Casting Understanding of Operators and Operators Precedence.

Visual Basic Variables

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Variables A storage location in memory (RAM) – Holds data/information while the program is running – These storage locations can be referred to by their names Every variable has three properties: – Name - reference to the location - cannot be changed – Value - the information that is stored - can be changed during program execution, hence the name “variable” – Data Type - the type of information that can be stored - can be transferred

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. How to Think About Variables You the programmer make up a name for the variable Visual Basic associates that name with a location in the computer's RAM The value currently associated with the variable is stored in that memory location You simply use the name you chose when you need to access the value

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Choosing Data Type Data type - Specifies type of data variable can store Integer variables: Long, Integer, Short, Byte Floating-point variables: Single, Double Fixed decimal point variable: Decimal Boolean variables: True, False Character variable: Char Text variable: String The Object variable – Default data type assigned by Visual Basic – Can store many different types of data – Less efficient than other data types

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Visual Basic Data Types Data typePrefixSizeValues Bytebyt1 bytepositive integer value from 0 to 255 Shortshr2 byteinteger from –32,768 to +32,767 Integerint4 byteinteger from +/- 2,147,483,647 Longlng8 byteinteger from +/- 9,223,372,036,854,775,807 Singlesng4 byte single-precision, floating-point number Doubledbl8 byte double-precision, floating-point number Decimaldec16 bytenumber with up to 28 significant digits Charchr2 byte Any single character Booleanbln2 byte True or False Stringstr(4 byte)Text - Any number/combination of characters Datedtm8 byte 8 character date: #dd/mm/yyyy# Objectobj(4 byte)An address that refers to an object

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Variable Names  First character must be a letter or underscore  Must contain only letters, numbers, and underscores (no spaces, periods, etc.)  Can have up to 255 characters  Cannot be a VB language keyword  Naming Conventions  Should be meaningful  Follow 3 char prefix style - 1st 3 letters in lowercase to indicate the data type  After that, capitalize the first letter of each word  Example: intTestScore

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Declaring a Variable A variable declaration is a statement that creates a variable in memory Syntax: Dim VariableName As DataType – Dim (short for Declaration in Memory) - keyword – VariableName - name used to refer to variable – As - keyword – DataType - one of many possible keywords to indicate the type of value the variable will contain Example: Dim intLength as Integer

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Declaring and Initializing a Variable A starting or initialization value may be specified with the Dim statement Good practice to set an initial value unless assigning a value prior to using the variable Syntax: Dim VariableName As DataType = Value  Just append " = value” to the Dim statement  = 5  assigning a beginning value to the variable Example: Dim intLength as Integer = 5

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Variable Declaration Rules Variables MUST be declared prior to the code where they are used Variable should be declared first in the procedure (style convention) Declaring an initial value of the variable in the declaration statement is optional – Refer to default values (next slide)

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Default Values for Data Types Data typeDefault (Initial) value All numeric typesZero (0) BooleanFalse CharBinary 0 String or ObjectEmpty Date12:00 a.m. on January 1, 0001

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Named Constants Programs often need to use given values – For example: decTotal *= 1.06 – Adds 6% sales tax to an order total Two problems with using statements like this – The reason for multiplying decTotal by 1.06 isn’t always obvious – If sales tax rate changes, must find and change every occurrence of.06 or 1.06

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Named Constants (cont.) Use of named constants resolves both these issues Can declare a variable whose value is set at declaration and cannot be changed later: Syntax: Const CONST_NAME As DataType = Value Looks like a normal declaration except: – Const used instead of Dim – An initialization value is required – By convention, entire name capitalized with underscore characters to separate words

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Named Constants (cont.) The objective of our code is now clearer – Const sngSALES_TAX_RATE As Single = 0.06 – decTotal *= sngSALES_TAX_RATE Can change all occurrences in the code simply by changing the initial value set in the declaration – If tax rate changes from 6% to 7% – Const sngSALES_TAX_RATE As Single = 0.07

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Scope of Variables What – Indicates the part of the program where the variable can be used When – From the variable declaration until the end of the code block (procedure, method, etc.) where it is declared – Variable cannot be used before it is declared – Variable declared within a code block is only visible to statements within that code block Called Local Variable – Can be declared at the beginning of the class code window (General Declarations section) and be available to all blocks Called Form Level Variable – Variables that share the same scope cannot have the same name (same name ok if different scope)

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Lifetime of Variables What – Indicates the part of the program where the variable exists in memory When – From the beginning of the code block (procedure, method, etc.) where it is declared until the end of that code block – When the code block begins the space is created to hold the local variables Memory is allocated from the operating system – When the code block ends the local variables are destroyed Memory is given back to the operating system

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Assignment Statement Syntax: variablename = expression Assigns the value of the expression to the variable. (The variable must be on the left and the expression on the right.) Example: – intNumber1 = 4 – intNumber2 = 3 * (2 + 2) – intNumber3 = intNumber1 – IntNumber1 = intNumber1 + 6

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Implicit Type Conversions A value of one data type can be assigned to a variable of a different type – An implicit type conversion is an attempt to automatically convert to the receiving variable’s data type A widening conversion suffers no loss of data – Converting an integer to a single – Dim sngNumber as Single = 5 A narrowing conversion may lose data – Converting a decimal to an integer – Dim intCount as Integer = 12.2‘intCount becomes 12

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Explicit Type Conversions VB provides a set of functions that perform data type conversions These functions will accept a literal, variable name, or arithmetic expression The following narrowing conversions require an explicit type conversion – Double to Single – Single to Integer – Long to Integer Boolean, Date, Object, String, and numeric types represent different sorts of values and require conversion functions as well

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. The Val Function The Val function is a more forgiving means of performing string to numeric conversions Uses the form Val(string) If the initial characters form a numeric value, the Val function will return that Otherwise, it will return a value of zero

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. The Val Function Val FunctionValue Returned – Val("34.90") 34.9 – Val("86abc")86 – Val("$24.95")0 – Val("3,789")3 – Val("")0 – Val("x29")0 – Val("47%")47 – Val("Geraldine") 0

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. The ToString Method Returns a string representation of the value in the variable calling the method Every VB data type has a ToString method Uses the form VariableName.ToString For example Dim number as Integer = 123 lblNumber.text = number.ToString – Assigns the string “123” to the text property of the lblNumber control

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Performing Calculations with Variables Arithmetic Operators ^ Exponential * Multiplication / Floating Point Division \ Integer Division MODModulus (remainder from division) +Addition – Subtraction &String Concatenation (putting them together)

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Common Arithmetic Operators Examples of use: – decTotal = decPrice + decTax – decNetPrice = decPrice - decDiscount – dblArea = dblLength * dblWidth – sngAverage = sngTotal / intItems – dblCube = dblSide ^ 3

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Special Integer Division Operator The backslash (\) is used as an integer division operator The result is always an integer, created by discarding any remainder from the division Example – intResult = 7 \ 2‘result is 3 – shrHundreds = 157 \ 100‘result is 1 – shrTens = ( \ 100 * 100) \ 10 ‘result is ?

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Special Modulus Operator This operator can be used in place of the backslash operator to give the remainder of a division operation intRemainder = 17 MOD 3 ‘result is 2 dblRemainder = 17.5 MOD 3 ‘result is 2.5 Any attempt to use of the \ or MOD operator to perform integer division by zero causes a DivideByZeroException runtime error

Concatenating Strings Concatenate: connect strings together Concatenation operator: the ampersand (&) Include a space before and after the & operator Numbers after & operator are converted to strings How to concatenate character strings – strFName = "Bob" – strLName = "Smith" – strName = strFName & " "  "Bob " – strName = strName & strLName  "Bob Smith" – intX = 1 intY = 2 – intResult = intX + intY – strOutput = intX & “ + “ & intY & “ = “ & intResult  " = 3 "

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Self Assignment Operators Often need to change the value in a variable and assign the result back to that variable For example: var = var – 5 Subtracts 5 from the value stored in var OperatorUsageEquivalent toEffect +=x += 2x = x + 2Add to -= x -= 5x = x – 5Subtract from *= x *= 10x = x * 10Multiply by /= x /= yx = x / yDivide by \= x \= yx = x \ yInt Divide by &= x &= “.”x = x & “.”Concatenate

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Arithmetic Operator Precedence Operator precedence tells us the order in which operations are performed From highest to lowest precedence: – Exponentiation (^) – Multiplicative (* and /) – Integer Division (\) – Modulus (MOD) – Additive (+ and -) Parentheses override the order of precedence Where precedence is the same, operations occur from left to right

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. All Operators Precedence Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (, >=, ) Logical Operators (AND, OR, NOT)

© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Precedence Examples 6 * 2 ^ / 2 = 50 7 * 4 / 2 – 6 = 8 5 * (4 + 3) – 15 Mod 2 = 34 intX = 10 intY = 5 intResultA = intX + intY * 5'iResultA is 35 iResultB = (intX + intY) * 5'iResultB is 75 dResultA = intX - intY * 5 'dResultA is -15 dResultB = (intX - intY) * 5'dResultB is 25