An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.

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.
Types and Arithmetic Operators
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.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
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.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 3 Input, Variables, Constants, And Calculations.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 2: Chapter 3: Slide 1 Unit 2 Variables and Calculations Chapter 3 Input,
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
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.
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
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 3: Chapter 3: Slide 1 Unit 3 Formatting Chapter 3 Input, Variables, Constants,
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
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.
Introduction to Computer CC111 Week 09 Visual Basic 2 Visual Basic Variables 1.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
1.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic Variables
Data Types, Arithmetic Operations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Revision Lecture
Variables, Expressions, and IO
2. Understanding VB Variables
Java Programming: From Problem Analysis to Program Design, 4e
Lecture Set 4 Data Types and Variables
Variables and Arithmetic Operations
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Visual Basic Variables
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Arithmetic Expressions & Data Conversions
Presentation transcript:

An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use

Why Have Variables? A variable is a storage location in the computer’s memory, used for holding information while the program is running The information that is stored in a variable may change, hence the name “variable”

What Can You Do With Variables? Copy and store values entered by the user, so they may be manipulated Perform arithmetic on values Test values to determine that they meet some criterion Temporarily hold and manipulate the value of a control property Remember information for later use in the program

Setting the Value of a Variable An assignment statement is used to set the (new) value of a variable, as in: length = 112 greeting = "Good Morning " & txtName.Text

Variable Declarations A variable declaration is a statement that causes Visual Basic .NET to create a variable in memory As in Dim length As Integer

Declaration Syntax The official syntax is Dim VariableName As DataType where Dim (stands for Dimension) is a keyword VariableName is the name to be used As is a keyword DataType is the type of the variable and will be one of many possible keywords

Visual Basic .NET Data Types Boolean Byte Char Date Decimal Double Integer Long Object Short Single String

Variable Naming Rules The first character of a variable name must be a character or an underscore Subsequent characters may be either of those plus the numeric digits Thus variable names cannot contain spaces or periods (or many other kinds of characters) Variable names must not be keywords

Variable Naming Conventions Each variable name should describe its use, e.g., itemsOrdered When multiple words are used in a name, capitalize the initials, except for the first one (again, itemsOrdered) As noted earlier, certain names should have a specific prefix, e.g., btn

Auto List Feature As you are entering your Visual Basic .NET program, VB will often aid you by offering a list of choices for that could be entered next Right after you type "As" in a variable declaration, Visual Basic .NET will offer you a list of all of the established data types Either choose one or keep typing

Variable Default Values When a variable is first created in memory, Visual Basic .NET assigns it a default value numeric types are given a value of zero strings are given a value of Nothing dates default to 12:00:00 AM January 1,1

Initialization of Variables via the Declaration It is preferable to establish your program's own initial value for variables that will not otherwise be given values before they are used In the declaration, simply append " = value" Dim length As Integer = 112

Scope of a Variable, I A variable’s scope is the part of the program where the variable is visible and may be accessed by programming statements

Scope of a Variable, II The scope of a variable begins where it is declared And extends to the end of the procedure in which it appears This variable is called local The variable is not visible outside of the procedure and its name cannot be declared again within the same procedure

Lifetime of a Variable The storage for a variable is created upon each use of the procedure The storage for a variable is destroyed as soon as the procedure finishes executing

The Val Function, I Suppose you wish to use text input as a number: number = txtInput.Text This will work without a run time error as long as txtInput.Text is the text equivalent of a numerical value (like "45") If it is not, there will be a run time error

The Val Function, II The Val function is more lenient on conversions from text to numeric values If the initial characters form a numeric value, it will return that Otherwise, it will return a value of zero

The Val Function, III Argument Val(Argument) "34.90" 34.9 "86abc" 86 "34.90" 34.9 "86abc" 86 "$24.95" 0 "3,789" 3 "" 0 "x29" 0 "47%" 47 "Geraldine" 0

ToString Method This is a Method that will convert any variable to a string, as in Dim number As Integer = 123 lblNumber.Text = number.ToString

Option Strict On Placed at the very top of the code window this will prevent Visual Basic .NET from performing implicit data type conversion The code must perform all conversions using Val or ToString

Performing Calculations and Working With Numbers Visual Basic .NET Provides Several Operators for Performing Mathematical Operations You May Also Use Parentheses to Group Operations and Build More Complex Mathematical Statements

The Arithmetic Operators, I Visual Basic .NET provides operators for the common arithmetic operations: Addition + Subtraction - Multiplication * Division / Exponentiation ^

The Arithmetic Operators, II Examples of use: total = price + tax area = length * width average = total / items salePrice = retail / 2 cube = side ^ 3

Special Integer Division Operator The backslash (\) is used as an integer division operator The result is always an integer, created by doing the division and then discarding any remainder Any floating-point operand is first rounded to the nearest integer

Special Modulo (MOD) Operator This operator follows the same basic rules as the backslash operator, but yields the remainder after the division \ operator yields an integer result of division MOD operator yields the integer remainder (after division using the \ operator)

Arithmetic Operator Precedence, I Which operations are done first -- precedence tells us -- highest to lowest: Exponentiation (^) Multiplicative (* and /) Integer Division (\) Modulus (MOD) Additive (+ and -)

Arithmetic Operator Precedence, II When two operators with the same precedence share an operand, the operator on the left works first, then the operator on the right

Arithmetic Operator Precedence, III Grouping with parentheses () forces the expression within those parentheses to be evaluated before others Roughly speaking, the order of evaluation in Visual Basic .NET is similar to that used in algebra classes (parenthesized expressions first, then exponentiation, then multiplicative operators, then the additive operators)

Combined Assignment Operators, I Frequently program assignment statements are similar to: number = number - 5 That is, modify a variable with one arithmetic operator and store the result back into the same variable

Combined Assignment Operators, II There are special assignment operators to enhance this usage: += add a value to the variable -= subtract a value from the variable *= multiple the variable by some value /= divide the variable by some value \= integer divide the variable by some value &= concatenate the variable with some value

More C (Convert) Functions Cbool Cbyte Cchar Cdate CDbl CDec Cint CLng Cobj Cshort CSng CStr

Named Constants, I Whenever a program needs to use a constant (e.g., the local sales tax percentage) it is a good idea to give it a variable name However, a variable does not necessarily have the same value throughout the program as an assignment statement can change the value

Named Constants, II Visual Basic .NET provides for a variable whose value, once established in the declaration, cannot be modified afterwards: Const salesTax As Single = 0.06

Formatting Numbers for Output Numbers May Be Formatted in Various Ways for Output

FormatNumber Function FormatNumber(expression [, DecimalPoints]) The expression is evaluated and output as a number The optional second argument gives the number of requested decimal places

FormatCurrency Function FormatCurrency(expression [, DecimalPoints]) The expression is evaluated and output as a currency value based on your PCs local options The optional second argument gives the number of requested decimal places

FormatPercent Function FormatPercent(expression [, DecimalPoints]) The expression is evaluated and output as a percentage value The optional second argument gives the number of requested decimal places