Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Visual Basic Chapter 1 Mr. Wangler.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 1B Introduction (Tutorial)
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Review for Mid-term! October 26, Review Homework Worksheet True or False Operators are symbols that perform specific operations in Visual Basic.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to Programming with RAPTOR
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 2 Variables.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
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
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)
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.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Data and variables in Visual Basic. Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: 
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Instructors: Sidra Ehsan Budoor Bawazeer CpCS 206.
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.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Visual Basic Variables
Lecture 3: Operators, Expressions and Type Conversion
Variables and Arithmetic Operations
Chapter 3 Assignment Statement
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Introduction to Programming
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 4 Variables and constants

4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand -Declaration Statement (variables must be declared before you can use them) -Format: Dim variable_name As type -Examples: Dim sngLength As Single Dim sngWidth As Single -Single (Data type) -any numeric value that can contain a decimal -variable name begins with sng -Examples:sngLength, sngWidth, sngHeight -Initializing a value (not necessary but helpful) - Example:Dim sngLength As Single = Multiple Declarations in a single statement (not necessary but helpful) - Example:Dim sngRadius, sngDiameter As Single

4.2 Variable Assignments -A value stored in a variable can be changed at run-time through assignment -Format: variable_name = value -Example:Dim sngRadius As Single = 10‘declaration and initialization sngRadius = 12.3‘change value -Using variables to write formulas -Example:sngCircleArea = 3.14 * sngRadius ^ 2 ‘sngCircleArea = Common Error -Example12.3 = sngRadius(What’s wrong???) -Think About it -Example:Dim sngX As Single sngX = 5.5 sngX = 8 Me.lblResult.text = sngX(What will be displayed???)

Ch 3 – Review 1 -New Project -New Form -Add 2 labels (lblQuestion, lblAnswer) -Add 1 button (btnAnswer) -Add appropriate comments to your code -Add btnanswer click event -Add following statements to event procedure Dim sngRadius As Single = 6 Dim sngCircleArea As Single sngCircleArea = 3.14 * sngRadius ^ 2‘area of circle formula Me.lblAnswer.text = sngCircleArea‘Displays answer in label -Run program (Answer should be )

4.3 Obtaining a Value from the User -TextBox Object (Allows users to enter text at run-time) -Properties: -Name: txt + description -Text: What is displayed (usually left blank) -TextAlign:Sets alignment of textbox -Prompt (A label that is placed near a text box to describe its contents/purpose) -TextChanged Event -Used to make changes when a user changes the text in a textbox -Anything typed in the textbox is stored in the text property for the textbox -Example:sngRadius = Me.txtRadius.text -If the textbox contains a number (Code as Follows!!!) -sngRadius = Val(me.txtRadius.text)‘converts text to number -What will sngHeight be assigned in the following examples??? 1. sngHeight = Val(“62.5 inches”) 2. sngHeight = Val(“Twenty Inches”) 3. sngHeight = Val(“six feet 2 inches”)

Ch 3 – Review 2 -New Project -New Form -Add 2 labels (lblPrompt, lblAnswer) -Add 1 text box (txtRadius) -Add 1 button (btnAnswer) -Add btnAnswer click event -Add following statements to event procedure Dim sngRadius As Single Dim sngCircleArea As Single sngRadius = Val(Me.txtRadius.text)‘gets value from textbox sngCircleArea = 3.14 * sngRadius ^ 2‘area of circle formula Me.lblAnswer.text = sngCircleArea‘Displays answer in label -Run program (Enter 3 into text box. Answer should be 12.56)

4.4 Using Named Constants -A constant is a named memory location which stores a value that can’t be changed. -Constant Declarations: -Format:Const variable_name As Single = Value -Example:Const sngPI As Single = Good Programming Style -Description part of name is all capitals -Place before variable declarations -Examples: sngPI, sngSPEEDOFLIGHT -Common Error (trying to change value of constant) Const sngPI As Single = sngPI = 22/7

4.5 Choosing Identifiers -Legal Names for variables and constants -must begin with a letter -can only contain letters, digits, and the underscore character -periods, spaces, and other special characters not allowed -can’t be a keyword (any word used by VB) -Show up in a different font than normal code -Examples: Single, End, Sub, Private, etc.

4.6 Built – In Data Types -Refer to (Visual Aide p. 17) -Special Considerations: -Char % String assignments must be in quotation marks -Initialization Value -Short, Integer, Long, Single, Double, Decimal are set to 0 -Date set to 12:00:00 AM -Boolean set to FALSE -Char, String set NOTHING -NOTHING is the same as “ “ -Example: me.lblMember5.text = NOTHING -Most common types to be used in class -Single = and decimal value -Integer = any whole number -String = and thing containing non-numerical characters

4.7 Automatic Type Conversion -VB will convert decimals to Integers following normal rounding rules -Example: Dim intX, intY As Integer intX = 6.7 intY = 12.3 What value is stored in intX and intY???

4.8 Scope -Scope explains which procedures can use a variable -Local Scope = variable declared inside of event procedure -only that procedure can use variable -Global Scope = variable declared outside of event procedure -all procedures can use variable -Visual Aide (p. 18) -Good Programming Style = only use global variables if completely necessary

Ch. 4 – Review 5 -New Project -New Form -Add 2 labels (lblPrompt, lblAnswer) -Add 2 buttons (btnProc1, btnProc2) -Add Comment Code -Add Global Variable Dim intX As Integer = 10 -Add btnProc1 Click Event Dim intX As Integer = 10 Me.lblAnswer.Text = intX -Add btnProc2 Click Event Me.lblAnswer.Text = intX -Run Program (Why does the Answer change???)

4.9 Special Division Operators -Integer Division ( \ ) -divides 2 numbers, but gives answer as a whole number -Example: How many basketball teams can we make with 23 people? 23 \ 5 = 4 teams -Modulus Division ( mod ) -divides 2 numbers, but gives answer as the remainder -Example: How many leftover people after we make full teams with 23 people? 23 mod 5 = 3 people -Order of Operations - Parentheses, Exponents, Multiply, Divide, Integer division, Modulus, Add, Subtract

4.10 Programming Errors -Syntax Error (violates rules of VB.NET) -not syntactically correct -usually underlined with wavy blue line -Logic / Semantic Error (syntactically correct but produces undesired results) -Hard to detect -Examples: wrong formulas, wrong data types, etc. -Computer will not detect, programmer must find through good testing -Run-time Error (halts program during testing) -sometimes called an exception -will display a message box with what caused the error

4.11 Debugging an Application -Be familiar with vocabulary from this section -Debugging -Breakpoint -Watch Window

Ch. 4 – Case Study -Sketch Form -Use Arrows to name all objects -Fill out Initial Properties table -Brief description of whole project -Write PseudoCode for each event procedure -Should include needed variables, input, operation, and output