3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

Slides:



Advertisements
Similar presentations
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 15 Creating Database Forms and Reports Introduction Forms Reports.
Chapter 5: Database Forms and Reports
Introduction to C Programming
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
CHAPTER 4: Representing Integer Data The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CHAPTER 2: Number Systems
Chapter 2: Input, Processing, and Output
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Introduction to C Programming
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 3 Variables, Assignment Statements, and Arithmetic.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Variables and Constants
Chapter 3: Using Variables and Constants
CS0004: Introduction to Programming Variables – Numbers.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
CHAPTER THREE Representing Data: Constants and Variables.
Lecture 8 Visual Basic (2).
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
7-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
CHAPTER 3: Number Systems
CHAPTER 3: Number Systems The Architecture of Computer Hardware and Systems Software & Networking: An Information Technology Approach 4th Edition, Irv.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
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.
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
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.
Programming with Microsoft Visual Basic th Edition
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
CHAPTER 3: Number Systems
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.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 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.
H-1 Prepared by Coby Harmon University of California, Santa Barbara Westmont College.
2-1 Chapter 2 Using VB.NET to Create a First Solution.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
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
Chapter 3: Using Variables and Constants
An Introduction to Programming and VB.NET
Variables and Arithmetic Operations
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic

3-2 Learning Objectives Understand a four-step process to writing code in VB.NET and its relationship the six operations a computer can perform Declare and use different types of variables in your project Describe the various type of data used in a program Use text boxes for event-driven input and output

3-3 Learning Objectives (continued) Write VB.NET functions to carry out commonly used operations Use buttons to clear text boxes and exit the project Describe the type of errors that commonly occur in a VB.NET project and their causes

3-4 Four step coding process Once the logic is defined through IPO tables or pseudocode, one needs to: Decide what variables and constants will be needed and declare them Input data from text boxes or other input controls Process data into information using arithmetic or string operations and store results in variables Output values of variables to text boxes or other output controls

3-5 Variables Conceptually a variable is a space in memory with a Name and a Value. The name identify the variable itself. It does not change. The value can be changed at will, i.e. it can be written to as well as read from.

3-6 Naming Variables A variable name can contain – Letters, A-Z – Digits, 0-9 – The underscore _ A variable name must start with a letter Variable names are case insensitive – MyVariable and myvariable refer to the same variable

3-7 Variable Names Cannot be any of VB.NET keywords Preferably meaningful names Often follows some convention to indicate – The type of the variable – The scope of the variable – Perhaps other characteristic

3-8 Commonly Used Data Types String : to hold text Single : a real number Double : a real number with larger coverage and precision than the single Integer : an integer Long : an integer with larger coverage Decimal : to work with currency Date : a date Boolean : True or False

3-9 Data types

3-10 Declaring Variables Purpose : informing VB.NET of the type of the variable Typical declaration: – Dim variableName As variable Type Examples – Dim dblWeight As Double – Dim strName As String – Dim x As Long, y As Long

3-11 Declare two variables of same type – Dim x, y As Long – Dim c As Integer, x, y As Long – In both cases, both x and y are Long Initialization and declaration – Dim intAge As Integer = 10 – Dim decPrice As Decimal =

3-12 Option Explicit Statement Use one of the following to enforce the requirement of declaring every variable you use (or not) – Option Explicit On – Option Explicit Off By default Option Explicit is On The Statement should be at the very top of the module

3-13 Event-Driven Input You need data to operate on You need a way to “input” the data You typically use controls such as text boxes to take the input You read the value of the input in code when a certain event occurs

3-14 TextBox Control Allows the user to enter text Can also be used to display text The Text property of the control is always equal to the text entered in the box

3-15 A simple calculator A simple application where – User enters two numbers in TextBoxes – User clicks a Sum button – The application displays the sum in another TextBox

3-16 Calculator appearance

3-17 Control properties

3-18 Assignment Statement Declare variables to hold useful values – Dim intFirst, intSecond, intSum As Integer When user clicks the sum button, read and assign the values in each TextBox to the right variable – intFirst = txtFirstNum.Text – intSecond = txtSecondNum.Text Assignment : VariableName = SomeValue

3-19 Tip: Tab Order A power user can use the Tab key to navigate controls in an application The order of the controls depends on the order in which they are laced on the form To change the default order – Change the TabIndex property of controls – Use the View | Tab Order menu

3-20 Functions A function is a black box taking some input (arguments) and returning a value

3-21 Example of Functions CInt() : converts the argument to integer Today() : returns the system date CStr() : converts a number to a string – Dim MyString as String, MyInt as Integer = 12 – MyString = CStr( MyInt) – MyString holds the value “12”, not 12

3-22 Code for btnSum Private Sub btnSum_Click(ByVal sender as System.Object, _ ByVal e as System.EventArgs) _ Handles btnSum.Click Dim intFirst, intSecond, intSum As Integer intFirst = txtFirstNum.Text intSecond = txtSecondNum.Text intSum = intFirst + intSecond txtSum.text = str(intSum) End Sub

3-23 Properties and Methods Access properties and methods through the dot notation – object.propertyName – object.methodName Ex: The Focus method shifts the cursor to the object calling it – txtFirstNum.Focus()

3-24 Step-By-Step 3-1 Creating calculator Demo

3-25 Arithmetic Operators

3-26 Hierarchy of operations The order in which operations are executed is the following 1. Parentheses 2. Raising to a power 3. Change of sign (negation) 4. Multiplication or division 5. Integer division 6. Modulus 7. Addition or subtraction

3-27 String Operators The only string operator is the concatenation operator, & It combines two strings into one – Dim strFName as String = “Jesse” – Dim strLName as String = “James” – Dim strName as String – strName = strFName & “ “ & strLName

3-28 Symbolic Constants Rather than have “magic” numbers in your programs, use symbolic constants Ex: you want to find the total sale when adding sales tax (now 7%) Declare a Constant and use it in code – Const TaxRate as Double = 0.07 If you maintain the program later and the tax rate has changed you need to reflect the change in a single place

3-29 Vintage DVDs Extend application. Allow to input renter’s name, the DVD rented, and price for DVD. Allow for printing rental price with tax, clearing the textboxes, and exiting application.

3-30 Step-by-Step 3-2 Modifying a project Demo

3-31 Develop logic for action objects

3-32 Write and test code Code for Calculate button Private Sub btnCalc_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCalc.Click Const sngTaxRate As Single = 0.07 'Use local tax rate Dim decPrice, decAmountDue, decTaxes As Decimal decPrice = CDec(txtDVDPrice.Text) decTaxes = decPrice * sngTaxRate 'Compute taxes decAmountDue = decPrice + decTaxes 'Compute amount due txtTaxes.Text = CStr(decTaxes) txtAmountDue.Text = CStr(decAmountDue) End Sub

3-33 Conversion functions

3-34 Formatting Output Formatting functions – Format( expression, format ) – FormatCurrency( expression )

3-35 Format Expressions

3-36 Clearing Entries on the Form Private Sub btnClear_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) _ Handles btnClear.Click txtCustName.Text = "" txtDVDName.Text = "" txtDVDPrice.Text = "" txtTaxes.Text = "" txtAmountDue.Text = "" txtCustName.Focus() End Sub

3-37 Step-by-Step 3-3 Adding controls to Vintage DVDs Demo

3-38 More built-in functions

3-39 Monthly payment Calculator

3-40 Code for btnCompute_Click Private Sub btnCompute_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) _ Handles btnCompute.Click Dim decAmount, decPayment As Decimal Dim intMonths As Integer Dim sngRate As Single decAmount = CDec(txtAmount.Text) intMonths = CInt(txtMonths.Text) sngRate = (CSng(txtRate.Text) / 100) / 12 decPayment = Pmt(sngRate, intMonths, -decAmount) txtPayment.Text = Format(decPayment, "currency") txtAmount.Text = Format(decAmount, "currency") End Sub

3-41 Ste-by-Step 3-4 Creating Monthly Payment Calculator Demo

3-42 Errors in VB.Net Syntax Errors – Incorrect grammar – Incorrect Usage Runtime Errors – Occurs while program is running – Caused by un planned conditions division by 0 Logic Errors – Incorrect program design

3-43 Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein