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.

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.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
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)
Data types and variables
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
Chapter 2 Data Types, Declarations, and Displays
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
VB .NET Programming Fundamentals
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
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.
Section 1.1 Numbers and Their Properties.
Objectives You should be able to describe: Data Types
Variables and Constants
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Variables, Constants, and Calculations
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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)
1-2 Order of Operations and Evaluating Expressions.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
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.
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?
C++ for Engineers and Scientists Second Edition
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.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic Variables
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Variables and Arithmetic Operations
Chapter 6 Variables What is VBScript?
INPUT & OUTPUT scanf & printf.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Introduction to C++.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

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 not change during program execution. An identifier is a name of a variable or constant. Variables are declared (named) in the declarations section of the program. Example Dim stName As String ‘Declare a string variable Dim iCounter as Integer ‘Declare an integer Const cDISCOUNT as Currency =.15 Variables, Constants, and Computations

Chapter 3 P. 2 Internal actions when declaring variables - memory location is reserved for the variable (symbol table is created) - value is placed into the location Example Dim iCounter As Integer Symbol table NAMEADDRESSTYPE iCounter integer stName string When the program is compiled, each identifier is replaced by the corresponding address. Note: Memory can be thought of as a collection of cells iCounter stName

Chapter 3 P. 3 Data types Definition A data type is a set of values. Most important data types NAME OF TYPEVALUES BooleanTrue or False IntegerWhole numbers in a given range (the range is machine dependent) LongLarger whole numbers CurrencyNumbers containing a decimal point SingleNumbers containing a decimal point (6 digits of accuracy) DoubleNumbers containing a decimal point (14 digits of accuracy)

Chapter 3 P. 4 NAME OF TYPEVALUES StringAlphanumeric data (any visible symbol, such as letters, digits, punctuation marks, etc.) VariantAny type Note Long is allocated more space than integer. Double is allocated more space than single. Note2 is of type Integer. 2.0 is of type Single (possibly Double), not integer. ExampleCONTENTTYPEREASON SSNoStringNo calculation Rate of payCurrencyDecimal point PopulationIntegerWhole number; used in calculation

Chapter 3 P. 5 Naming rules - 1 to 40 characters - allowable symbols are letters, digits, and underline - no spaces are allowed - reserved words are not allowed (Print, Name, Caption, etc.) Naming conventions - identifiers must be meaningful (avoid 1-letter identifiers) - precede the identifier with a letter giving the type - capitalize variable names after the type - use all capitals for constants Example st = (inc - ft) * 0.2 is not as good as cState_Tax = (iIncome - cFederal_Tax)*0.2

Chapter 3 P. 6 Constants- named and intrinsic - named constants (created by the programmer) - constant statement - general formConst Identifier As Type = value - Example Const TAXRATE As Single = intrinsic constants (built-in) Declaring variables - dim statement - general formDim Identifier As Type - ExampleDim sTax As Single

Chapter 3 P. 7 Scope of variables Definition The scope of a variable is the code over which the variable is defined. Local declarations Definition A variable is local if its scope is one procedure. Example Sub cmd_Click() ‘Compute the tax Dim mcTax as Currency mcTax = iIncome * TAXRATE End Sub (Here iIncome and TAXRATE are declared at the module level.) Definition A variable is module-level if its scope is all the objects on the form. Example See the previous example.

Chapter 3 P. 8 - including the scope in identifiers (precede the name with m) - coding module-level declarations Calculations Arithmetic operators +-*/^ (exponent) - parentheses are also used - hierarchy is necessary because arithmetic expressions are written all on one line 1 Example is written as 1/2 2 - hierarchy - exponent - multiply and divide - add and subtract Example * 5 is computed as 2 + (3 * 5), which is 17

Chapter 3 P. 9 In general - expressions in parentheses are evaluated first, left to right - exponentiations are evaluated, left to right - multiplications and divisions are evaluated, left to right - additions and subtractions are evaluated, left to right Example B. P. 82 Using calculations in code General computations - general form is variable name = computation - Example lblTax.Caption = (iIncome -iDeductions * 3000) * 0.2 sDeterminant = sBcoeff^2 - 4 * sAcoeff *s Ccoeff Screen computations (center and resize form, if needed; B. P. 83) - frmMain.Top = (Screen.Height - frm.Height)/2 - frmMain.Left = (Screen.Width - frm.Width)/2 Note: This code goes in the Form_Load procedure

Chapter 3 P. 10 Counting and accumulating sums Example B. P. 83miTotal = miTotal + txtScore.text - get the value of miTotal - add the value of txtScore.text - place the result into miTotal Example ‘Increase the value of iCounter by 1. iCounter = iCounter + 1 Val function - changes a non-numeric value to a numeric value (for example, changes a blank to 0) - Example B.P. 84 iTotal = iTotal + val(txtScore.text) - the expression in parentheses is the argument

Chapter 3 P. 11 Formatting data Defining string formats - general formFormat$(expression, “FormatString”) - Example Format$(sName, “<“) ‘Write Name in lowercase letters - general format operators for strings B. P space placeholder - null placeholder - left fill - Example B. P. 87 Defining numeric formats B. P null placeholder - decimal point - thousand separator - percentage - literal character list

Chapter 3 P. 12 Programming hint Require variable declaration - use the Option Explicit feature (see the handout on Most frequently used Visual Basic commands) - select Tools - select Options - select Editor - check on Require variable declarations