Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Variables in C Amir Haider Lecturer.
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
VARIABLES: Your Own Piece of the ram. Why Use Variables? To save a piece of data in memory To save a piece of data in memory To store results of a calculation.
Data Types and Expressions
CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Basic Elements of C++ Chapter 2.
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 and Constants
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.
Lecture 8 Visual Basic (2).
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
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.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CPS120: Introduction to Computer Science
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
1 homework Due today: hw #1 (mailing list printout) readings to date: chapter 1 and chapter read appendix B (3 pages on DOS) and and.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fundamentals 2.
1.
A variable is a name for a value stored in memory.
Java Variables and Types
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic Variables
Data Types, Arithmetic Operations
Data Types, Identifiers, and Expressions
2. Understanding VB Variables
Visual Basic 6 Programming.
IDENTIFIERS CSC 111.
Data Types, Identifiers, and Expressions
Numbers.
Chapter 2: Java Fundamentals
Prepred By: Deborah Becker
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Prepred By: Deborah Becker
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Java Fundamentals
VB Variables and Data
An Introduction to Programming with C++ Fifth Edition
Visual Basic Variables
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Variables and Constants
Presentation transcript:

Variable, Constants and Calculations Dr Mohammad Nabil Almunawar

Learning Objectives Distinguish variables & constants Identify and specify data type for variables and constants Declare variable Make simple programs using variables

Variables & Constants A variable is a symbolic name (memory location) that hold a value. The value can be changed anytime. A constant is a value or a symbolic name (memory location) that has a value, which cannot be changed once it is declared.

Data Types A data type (type) of a variable or constant is a set of values valid for that variable or constant. A default data type for any variable or constant is variant (If you do not specify a type for a variable or constant then the type is variant). It is a good habit to specify a type for every variable or constant.

Data types Example deciding data types: ContentsData typesReason IC number or Student ID# StringNot used in calculation PriceCurrencyUsed in a calculation, contains decimal points Hours workedCurrencyUsed in a calculation QuantityIntegerUsed in calculation, contains a whole number Success statusBooleanContain logical value true or false

Naming Rules Each variable or constant name: 1.Must be one character and maximum 255 characters 2.Contains letters, digit, and underscore. They cannot contain any space or period. 3.Must be different from VB reserved names such as Sub, print, function, value.

Naming Convention A name (identifier) must be meaningful. Do not abbreviate, unless its meaning is obvious. Do not use very short identifier such as X and Y Precede each identifier with lowercase prefix that specifies the data type. (bln for Boolean, int for Integer, cur for Currency, lng for Long, str for string, dtm for date/time, vnt for variant, sng for single) Capitalize each word of the name (following prefix. Always use mixed case, never all uppercase.

Sample identifiers DataPossible name IC numberstrICNumber Pay ratecurPayRate PricecurPrice QuantityintQuantity KampongstrKampong Product namestrProductName FlagblnFlag PopulationlngPopulation

Exercise: What do you think about the following identifiers OmittedstrSub int#soldMinimumRate Int.Number.SoldstrCompanyName SubconMaximum sng$AmountblnSwitch CaptionlngPopulation

Declaring Constant Constant statement: Const Identifier [As Datatype] = Value Examples: Const strCompanyName As String = “Bakar Ali Sdn. Bhd.” Const intHourWork As Integer = 40 Const curSalesTax As Currency = 0.1

Declaring Variables Dim statement Dim identifier [As datatype] Example: Dim strCustName As String Dim dtmDateOfBirth As Date Dim intAmount As Integer Dim curPrice As Currency Dim sngWidth As Single Dim strGrade As String

Example of Programs A program to compute an area of a triangle Simple currency conversion Calculate final mark and grade the mark

Variables Scope Variable life time is the period of time that the variable exist. –Local variable: a variable is called a local variable if it is defined inside a subroutine (Sub or function). The scope of this variable is within the corresponding Sub or function. In other words, it cannot be seen from outside the Sub or function. –Module variable: a variable which is defined in a module (form). The scope of this variable is within a form (can be seen anywhere within the form)

Option Explicit Dim mModuleLevelVariable As Datatype Const mModuleLevelConstant Ad Datatype __________________________________ Private Sub cmdCalculate_Click Dim LocalVariable As Datatype Const LocalVariable As Datatype … End Sub ___________________________________ Private Function Length As Datatype Dim LocalVariable … mModuleLevelVariable = …. End Function ___________________________________

Calculations Arithmetic Operations Operations within parentheses “(…)” will be performed first. ^Exponentiation *, /Multiplication/division +,-Addition/substraction Order of operation

VB Calculation Notation Mathematical NotationEquivalent VB Statement 2A2*A 3(X + Y)3 * (X + Y) (X + Y)(X – Y)(X + Y) * (X – Y) X + Y 2 X + Y^2

Formatting Data FormatCurrency FormatNumber VariableValueFunctionOutput curBalance FormatCurrency(curBalance)$1, curBalance FormatCurrency(curBalance,3)$1, curBalance FormatCurrency(curBalance,0)$1,276 VariableValueFunctionOutput curBalance FormatNumber(curBalance,0)1,276 curBalance FormatNumber(curBalance,3)1, curBalance FormatNumber(curBalance,2)$1,276.68

FormatPercent FormatDateTime VariableValueFunctionOutput curRate0.75FormatPercent(curRate)75% curRate0.734FormatPercent(curRate,1)73.4% curRate0.734FormatPercent(curRate,2)73.40% VariableValueFunctionOutput dtmToday9/11/00FormatDateTime(dtmToday,vbShortDate)9/11/00 dtmToday9/11/00FormatDateTime(dtmToday,vbLongDate)Monday, September 11,2000 dtmTime FormatDateTime(dtmTime,vbLongTime)3:16:24 PM dtmTime FormatDateTime(dtmTime,vbShortTime)3:16

Exercise