Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

1.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 2: Introduction to C++.
Introduction to C Programming
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
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.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
CS0004: Introduction to Programming Variables – Numbers.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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?
Input, Output, and Processing
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter 2: Using Data.
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. Chapter 2: Introduction to C++
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 5 Completing the Inventory Application Introducing Programming.
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”
Introduction to Programming with RAPTOR
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 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
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.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
# 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?
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.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Making Interactive Programs with Visual Basic .NET
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 4 Assignment Statement
Visual Basic Variables
Variables and Arithmetic Operations
Chapter 3 Assignment Statement
Variables and Arithmetic Operations
Introduction to C++ Programming
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Review of Java Fundamentals
Presentation transcript:

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 Type of the variable – indicates what kind of value it will store.  All variables SHOULD be declared with a Dim statement that includes an identifier and data type. ◦ Example: Dim dblRadius As Double  Variable Declarations – reserves a space in memory for a value.

 Dim - is a keyword that stands for dimension. A declared variable has “dimension” because it has been assigned space in memory.  Identifier – is the name of the variable.  Data type – indicates what kind of value it will store.

 Assignment operator – equal symbol (=).  Variable is given a value through assignment.  Variables assignment must be written so the variable is on the left side of the equal sign and the value on the right. ◦ Example: dblRadius = 12.3  If there is an expression on the right side of the equal symbol, the expression is evaluated first and the answer is stored in the memory location

 Expression must be used on the right side of the assignment. ◦ Example: dblCircleArea = 3.14 * dblRadius ^2

 Constant is a named memory location which stores a value that cannot be changed at run-time from its initial assignment.  Const statement is used to declare a variable. ◦ Example: Const dblPI As Double = 3.14  Const should be declare before variable declarations.

 Rules for creating variable names ◦ Must begin with a letter ◦ Must contain only letters, digits, and the underscore (_) character. Periods, spaces, and other special characters are not allowed ◦ Cannot exceed 255 characters.

 Keywords- has special meaning to the compiler. Cannot be used as user declared variables. ◦ Example: Double, Const  Case Sensitivity in Identifier ◦ Visual Basic is not Case Sensitive.

 Variables and constants should be named so that they are quickly and clearly understandable to the reader.

 Built-In Data Types ◦ Single – numbers possibly containing a decimal ◦ Double – numbers possibly containing a decimal ◦ Integer integers only (no decimals) ◦ Long – integers (no decimals) ◦ Currency – numbers representing dollar amounts ◦ String – set of characters ◦ Boolean – True or False

 Each data type has their own memory requirements for storing values.  Single uses 4 bytes  Double uses 8 bytes  Integer uses 2 bytes  Long uses 4 bytes  Currency uses 8 bytes  Boolean uses 2 bytes  String uses 1 byte for each character in the string

 Represents positive and negative real numbers (floating point) decimals  Single – can store numbers up to 3.4e 38  Double can store numbers up to 1.8e 308

 Represents positive and negative integers.  Integer can go up to 32,767 (a number)  Long is used for numbers greater than 32,767.  Trying to store a decimal in an integer location will round up the decimal if it is greater than 5. ◦ 4.7 = 5.0

 Represents real numbers that are money values.  Currency can store values with up to four digits to the right of the decimal place and 15 digits to the left of the decimal place.

 Represents a set of characters called string. Can include just about everything, numbers, letters, symbols, spaces etc…, just along as it is in double quotes. ◦ Example: Dim strLastName as String strLastName = “Grassman”

 Represents True or False  Yes/No or on/off or open/closed

 Identifiers should be descriptive and begin with an appropriate prefix.  sgl – Single  dbl – Double  int – Integer  lng – Long  cur –Currency  str – String  bln – Boolean

 A single Dim statement can be used to declare multiple variables using commas. ◦ Example: Dim strName As String, intAge As Integer, dblHeight As Double ◦ Good to put all integers together, strings, etc.  Visual Basic initializes variables when they are declared by default. Double, Single, Integer, Long, & Currency are initialized to 0. String is set to an empty string (“”) and Boolean is set to False. If you don’t set a value they will be set to the default values

 Visual Basic does not require variables to be declared before they are used.  This may lead to an error in your output. ◦ Example: Dim dblRadius As Double ◦ dblRadis = 10 ‘dblRadius is misspelled ◦ dblCircleArea = dblPi * dblRadius^2 ◦ Answer will be 0 ◦ dblRadius is 0 by default and never assigned to 10 Even though the program works your end result may be incorrect

 Prevents error as in the previous slide  2 ways to use Option Explicit Statement ◦ In Existing code, select General from the object list in the Code Editor window and then type the statement typed. ◦ To automatically have the Option Explicit Statement appear all the time, go to Tools, Options and select Require Variable Declaration option from the Editor tab

 Syntax Error – statements that violates the rules of Visual Basic ◦ Example: Dim intX As Integer = 12 ‘Syntax Error – Can’t assign variables in the declaration. ◦ Syntax Error are displayed in Red in the Code Editor.

 Errors that occur during the application run.  Dialog box displays the error  Clicking the button highlights the statement causing the runtime error.

Can’t be divided by zero

Debugging – Is a process of correcting errors. Grace Murray Hopper was the first person to apply the term debug.

 2 ways to get the Immediate Window. ◦ CTRL + G ◦ View menu – Select Immediate window  The Immediate window displays the output of the Debug.Print statement.  Debug.Print item ‘you want printed out  Item is the variable, a string in double quotes, or an expression  Multiple items can be printed out by using the semicolon (;)  When the code is run the Immediate window displays the output of the Debug.Print

Private Sub cmdCalculate_Click() Const dblPI As Double = 3.14 Dim dblRadius As Double Dim dblCircleArea As Double Debug.Print "dblRadius ="; dblRadius dblRadius = 10 Debug.Print "dblRadius ="; dblRadius dblCircleArea = dblPI * dblRadius ^ 2 lblAnswer.Caption = dblCircleArea End Sub

 Object that is created using the TextBox control in the Tool box.

 Name – identifies the object and is used by the programmer. Prefix for naming is txt  Text – changes what is display in the text box. Can be change at run time through assignment. Also changes when user types in the text box.  Alignment – sets the alignment of text relative to the text box.

 Does not have a Caption  Must provide a label box next to it to act as it Caption.

 Code for the text box  Is executed when the user begins to type in a text box  At run time, the Text property contains whatever characters the user types into the text box.  The value of the Text box then can be used for assignments, expressions etc.

 When a decimal number ends with.5. Visual Basic will round up if the decimal number is an odd number and round down if the decimal number is even.  Example: Dim intX as Integer intX = 5.5 ‘intX is assigned 6 because intX is an Integer and 5 is odd intX = 6.5 ‘intX is assigned 6 because intX is an Integer and 6 is even

 Visual Basic automatically converts data to match the type of the variable it is being assigned to.  (Implicit-computer changes it) ◦ Example: Dim intSum As Integer intSum = 6.7 ‘intSum will get 7  Data typed in a text box is a String  The String will be converted by Visual Basic to the appropriate data type when assigning it.

 Trying to assign a value into an inappropriate data type causes an error. ◦ Example: intSum = “abc” ‘Trying to assign a string to an Integer.

 \ - Integer Division – truncates the decimal portion of the quotient which results in an Integer. ◦ Example: intSum = 20\7 ‘intSum is assigned 2  Mod – Modulus Division – returns the remainder resulting from division. ◦ Example: intSum = 20 Mod 7 ‘intSum is assigned 6

 ^  *, /  \  Mod  +, -

 Also called radio buttons  Another way to get input from user.

 Name – identifies the object and is used by the programmer. The prefix for Option Button names is “opt”  Caption – Changes the text displayed as the option button label. Can be change at run time.  Value – Can be set to True and False. Only one option button can be true at any given time.  Alignment – Moves option button left, right, or center.

 A click event procedure is usually coded for each option button.  Is executed when user clicks on an option button

 Is a container object to group option buttons relative to a single choice.

 Name – identifies the object and is used by programmer. Prefix for frames is “fra”  Caption – changes the text displayed as the frame label.

 Create frame first and then select OptionButton control and drag over to frame.  When dragging the frame, the option buttons will move with the frame.

 Object names that begin with 3 letters descriptive of the object.  Comments that explain and clarify code  Use parentheses in Expressions if needed to better understand what is intended.  Variables and constants with meaningful identifiers to represent values. Prefix identifies data type  Option Explicit statement  Variables of the appropriate data type 5 * * 3 Better written as (5 * 3) + (2* 3)

 Variables declared at the beginning of the procedure that they are used in  Constants to represent unchanging values  Constants declared at the beginning before variable declarations  Statements in the procedures are indented with a tab