Variables, Constants &Computation T o i n s e r t y o u r c o m p a n y l o g o o n t h i s s l i d e F r o m t h e I n s e r t M n u e l e c t P i c.

Slides:



Advertisements
Similar presentations
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Advertisements

CSci 1130 Intro to Programming in Java
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
Introduction to Programming G51PRG University of Nottingham Revision 1
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 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Chapter 3 Variables, Constants, and Calculations
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Data Types 1.
Clearly Visual Basic: Programming with Visual Basic 2008
Variables and Constants
CS0004: Introduction to Programming Input and Output.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Input, Output, and Processing
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Chapter 2: Using Data.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Applications Development
Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.
Chapter 3 Variables, Constants and Calculations Programming In Visual Basic.NET.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
# 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?
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Programming with Microsoft Visual Basic th Edition
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
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.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
VB.NET 2008 Introduction to Variables Part 1. Overview.NET Languages –Source Code –Compiler –MSIL –CLR & Windows Variables –Data Types –Converting.
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic 6 Programming Decide how many variables you need by looking at this form. There is one textbox for input and there are 3 labels for output,
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.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
1.
A variable is a name for a value stored in memory.
Topics Designing a Program Input, Processing, and Output
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Data Types, Arithmetic Operations
Variables and Arithmetic Operations
Variables, Expressions, and IO
Visual Basic 6 Programming.
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Visual Basic Variables
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

Variables, Constants &Computation T o i n s e r t y o u r c o m p a n y l o g o o n t h i s s l i d e F r o m t h e I n s e r t M n u e l e c t P i c t u r e L o c a t e y o u r l o g o f i l e C l i c k O K T o r e s i z e t h e l o g o C l i c k a n y w h e r e i n s i d e t h e l o g o. T h e b o x e s t h a t a p p e a r o u t s i d e t h e l o g o a r e k n o w n a s r e s i z e h a n d l e s. U s e t h e s e t o r e s i z e t h e o b j e c t. I f y o u h o l d d o w n t h e s h i f t k e y b e f o r e u s i n g t h e r e s i z e h a n d l e s, y o u w i l l m a i n t a i n t h e p r o p o r t i o n s o f t h e o b j e c t y o u w i s h t o r e s i z e. ©Copyright by Ronald P. Kessler, Ph.D. Introduction to VB.Net 2003

Why Use Variables? They allow us to use math computations They make our programs generic to many solutions

What Is a Variable? Just as in algebra, variables in VB are used to represent values of things that can change. I can use a variable to compute sales tax: TaxRate=.0775 SalesTax= Price * TaxRate SalesTax, Price, and TaxRate are variables

How Does VB Know What Each Variable Is? When we have been entering items & prices in our data entry screens without specifically telling VB what is text and what should be a number. Items that we type in (text) are Strings Prices are numbers and should be treated like Decimal.

PROBLEM: I want to compute the total cost of items purchased. Lets say I buy 3 dozen mouse traps at per dozen. I can tell VB to compute the total: My Total will be ( $16.95 * 3) or $50.85 In code it is: Total = * 3 I have defined the variable Total simply by typing it in my formula.

Data Types for.Net (See Page 99) Short (Integer)+ 32,768 2 bytes(i or int) Integer + 2,147,483,648 4 bytes(i or int) Long Integer+ 9,223,372,036,854,775,808 8 bytes(lng) Single Precision 6 digits of accuracy 4 bytes(sng) Double Precision 14 digits of accuracy 8 bytes(dbl) Decimal More $$ than we can count 16 bytes(dec) StringSize of Str.(s or str) BooleanTrue/False 2 bytes (bln) In VB 3,4,5,& 6 we used Currency instead of decimal & it was 8 bytes

Math Operators and Computations Price = Tax =.0775 Total = Price * Tax lblTotal.Text=Total

Math Operators Use the +, -, *, and / for the basic math operations. Integer Division uses a \ (backslash) so that: 5\2 = 2 Use variables in your computations: Dim Sales as Decimal Dim SalesTax as Decimal Dim TotalSale as Decimal Sales= SalesTax=.0775 TotalSale= Sales * SalesTax lblTotal.Text = TotalSale

Operation Hierarchy How to solve/setup formulas: 64 / 8 * 3 / ( ) - 2 = 64 /8 * 3 / = 8 * 3 / = 24 / = = 0 1.Always do stuff in () first 2.Exponentiation (raise things to powers) 3.* and / 4.+ and - Always go left to right and handle * and / or + and - as they come

Difference Between Assignment and Math In the formula: Income= HoursWorked * PayRate we are computing a math operation. In the formula: btnPrint.Text = Print this junk we are assigning data from the right side to the left. When we say : NumCustomers = NumCustomers + 1 we are assigning, not doing math. In mathematics, this statement cannot exist. In VB, the equal sign (=) doubles as an operator and an assignment character Assignments take the stuff on the right and assign it to the variable on the left and stores that value in memory.

Working with Data Entry assign data from data entry screen and use conversion functions Procedure= txtProc.Text Charge= CDec(txtChrg.Text) Convert text to decimal type Paid= CDec(txtPaid.Text) Adjustment= CDec(txtAdj.Text) CurrentBal= Charge - Paid – Discount lblResult.Text = FormatNumber(CurrentBal, 2) 2 decimals OR…. lblResult.Text = FormatCurrency(CurrentBal, 2) shows $ FormatNumber & Val are built-in functions in VB

Formatting Your Numbers... To format Currency (Decimal data type) we can say: lblTax.Text = CDec(txtPrice.Text) * 0.08 'Make VB treat text as a number using the CDec (Convert to Decimal) function and we decide how to format it. lblTax.Text =FormatNumber(CDec(txtPrice.Text * 0.08), 2) lblBalDue.Text = Format(CDec(txtPrice.Text) + CDec(lblTax.Text), "###.00") If you want a $ to appear before the amount, use: lblBalDue.Text = FormatCurrency(CDec(txtPrice.Text) + CDec(lblTax.Text), 2") VB also has FormatPercent & FormatdateTime

Defining Variables the Correct Way...

Why Define Variables Ourselves? This makes much better use of memory (RAM) It makes our programs run faster It will catch our spelling errors

Where Do I Tell VB to Force Me to Define Variables? In the GENERAL section of the main form of your program use the term: Option Explicit On You can force VB to make you declare variables from every project automatically if you want. Or you can type in Option Explicit On each time. I want you to start using this from now on! Use Tools|Options|Projects to set it in Visual Studio.

How to Define Variables Ourselves We use the command Dim (dimension) to define the type of data we are wanting to use. If I want the total cost of an item, then I tell VB this before I start using the variable: You usually Dimension variables in the subroutines (event procedures) where you are going to use them...

Define Variables In Subs... Private Sub btnPrint_Click () ---Declare variables here Dim strItem as String Dim decPrice as Decimal Dim decTotal as Decimal Dim intQuantity as Integer strItem= txtItem.Text decPrice= CDec(txtPrice.Text) intQuantity= CInt(txtQuantity.Text) use CInt or CShort decTotal = decPrice * intQuantity lblReceipt.Text = "Total===>" & Format(CDec(txtPrice.Text) + CDec(lblTax.Text), "###.00") End Sub

Using Constants...

In the section of the form, just after Windows Form designer generated code, use this to define a constant : Const SALESTAX As Decimal = 0.08D The D makes it a decimal…I know it looks redundant! Get in the habit of typing constants all in capitals. Now we can use the value assigned to SALESTAXRATE any where in our program. This value never changes as long as the program is running.

Using Your Own Constants Inside the form code Const SALESTAX As Decimal = 0.08D _________________________________________ Sub btnPrint_Click () Dim Tax as Decimal Dim Price as Decimal Price= CDec(txtPrice.Text) Tax= Price * SALESTAXRATE End Sub

All Done….for now at least.