Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Constants and Variables.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
CHAPTER THREE Representing Data: Constants and Variables.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
# 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?
Programming with Microsoft Visual Basic th Edition
Mark Dixon Page 1 04 – Conditional Execution. Mark Dixon Page 2 Questions: Expressions a)What is the result of: 10 * Int( ) b)How many functions.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
B065: PROGRAMMING Variables 2. Starter  What is wrong with the following piece of code: Option Explicit Off Dim num1 as string Dim num2 as integer num1.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Representing Data: Constants and Variables
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Basic Computation
Data Types, Arithmetic Operations
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
B065: PROGRAMMING Variables 2.
Presentation transcript:

Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables

Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –Introduce you to data storage concepts, i.e. constants, data types and variables Objectives, by end of this week’s sessions, you should be able to: –declare and use constants –declare a variable, selecting appropriate data type –assign a value to a variable, using combination of literal values, operators, functions, and identifiers

Mark Dixon, SoCCE SOFT 136Page 3 Types of Information Numbers (numeric)29 (integer/whole) (decimal/real) Text“Hello there!”“BOO” Pictures Sound

Mark Dixon, SoCCE SOFT 136Page 4 Constants similar to variable value given in declaration value can’t be changed useful for removing 'magic numbers' declaration syntax: name used to represent literal value [Global] Const constantname = expression

Mark Dixon, SoCCE SOFT 136Page 5 Example: CirCalc v1 Private Sub Form_Load() picCircle.ScaleMode = vbMillimeters End Sub Private Sub btnOK_Click() Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * * radius area = * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub btnOK txtRadius picCircle picInfo

Mark Dixon, SoCCE SOFT 136Page 6 Example: CirCalc v2 Private Sub Form_Load() … Private Sub btnOK_Click() Const Pi = Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * Pi * radius area = Pi * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub CirCalc btnOK txtRadius picCircle picInfo

Mark Dixon, SoCCE SOFT 136Page 7 Data Types Integer – whole numbers Long – whole numbers (large) Single – decimal numbers Double – decimal numbers (more precise) Currency – money String – text Boolean – True or False

Mark Dixon, SoCCE SOFT 136Page 8 Data Type Selection

Mark Dixon, SoCCE SOFT 136Page 9 Data Storage Data can be stored in –Controls visible to user (although can use visible property to hide) take lots of memory slow to access –Variables Not visible to user take up very little memory fast to access

Mark Dixon, SoCCE SOFT 136Page 10 Example: GuessNum - Analysis SPECIFICATION User Requirements –need to keep children occupied/entertained, while learning about maths Software Requirements –Functional: –computer picks a number between 0 and 100 –user enters a number –compare numbers and display appropriate message –Non-functional should be easy and fun to use

Mark Dixon, SoCCE SOFT 136Page 11 Variables (why?) Variables useful for: –reducing memory use –speed up execution –storing information you don't want user to see –storing intermediate results of calculations temporarily (makes code easier to understand) –making code easier to read (short variable name instead of long object.property names)

Mark Dixon, SoCCE SOFT 136Page 12 Variables (what) Variables have –Identifier (name) – you choose this, used to refer to (reference) variable –Type – you choose this (to suit purpose) –Value – you set/change this 23xInteger Name/Identifier Value Type Memory

Mark Dixon, SoCCE SOFT 136Page 13 Numeric Variables

Mark Dixon, SoCCE SOFT 136Page 14 String (Text) Variables

Mark Dixon, SoCCE SOFT 136Page 15 Variable declaration (how 1) Variables must be declared, using the following syntax (grammar): Dim As e.g. Dim weight As double Dim x As long Dim s As string Dim year As long

Mark Dixon, SoCCE SOFT 136Page 16 Questions: Variable declaration Write a line of code that: –Declares a variable called x of type double –Declares a variable called y of type integer –Declares a variable called surname of type string –Declares a variable called age of type integer Dim x As double Dim y As integer Dim surname As string Dim age As integer

Mark Dixon, SoCCE SOFT 136Page 17 Variable assignment (how 2) Variables are assigned values, using the following syntax: = e.g. x = 5 weight = name = "Bob" s = "Hello " Note: the data flows backwards (from right to left)

Mark Dixon, SoCCE SOFT 136Page 18 Questions: Variable assignment Write a line of code that: –Assigns the value of 23 to the variable y –Assigns the value of 14.6 to the variable x –Assigns the value of ‘John’ to the variable surname –Assigns the value of 21 to the variable age y = 23 x = 14.6 surname = "John" age = 21

Mark Dixon, SoCCE SOFT 136Page 19 Example: AddNum v3 Private Sub btnAdd_Click() Dim num1 As Double Dim num2 As Double Dim res As Double num1 = Val(txtNum1.Text) num2 = Val(txtNum2.Text) res = num1 + num2 lblResult.Caption = res End Sub AddNum Variables used to: –spread code over several lines –makes code easier to understand

Mark Dixon, SoCCE SOFT 136Page 20 Example: GuessNum - Code Option Explicit Dim GuessNum As Long Private Sub Form_Load() Randomize GuessNum = Rnd() * 100 End Sub Private Sub btnGuess_Click() If txtGuessNum.Text = GuessNum Then lblResult.Caption = "Correct" Else lblResult.Caption = "Wrong, please try again" End If End Sub txtGuessNum btnGuess lblResult

Mark Dixon, SoCCE SOFT 136Page 21 Variables: Errors Option Explicit Dim z as integer Sub Form_Click () Dim s As String Dim x As Integer Print y Print z x = x = "21" s = 21 x = 3.2 End Sub OK, forces explicit variable declaration OK Duplicate definition error. Variable not defined error. OK, as z was declared at the form level. Overflow error. Type mismatch error. OK (however x will be 3).

Mark Dixon, SoCCE SOFT 136Page 22 Questions: Variable assignment 2 Write a line of code that: –Increases the value of x by 2.89 –Decreases the value of z by y –Divides Km by 1.6 and puts the result in Miles –Joins two strings Surname and Forenames together, putting the result in LongName x = x z = z - y Miles = Km / 1.6 LongName = Surname & Forenames

Mark Dixon, SoCCE SOFT 136Page 23 Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Form/module/unit (general declarations)

Mark Dixon, SoCCE SOFT 136Page 24 Variable Scope (How) Module variables –general declarations (top) Local variables: –in procedures Option Explicit Dim mv as long Private Sub btnCalc_Click() Dim lv1 as long... End Sub Private Sub btnAdd_Click() Dim lv2 As Long... End Sub

Mark Dixon, SoCCE SOFT 136Page 25 Variable Scope

Mark Dixon, SoCCE SOFT 136Page 26 Scope (why) In short – Robustness of code/software –Protection from accidental outside interference One of many responses to code that is –Difficult to maintain, and –Unreliable –House of cards phenomenon Prevent: –Uncontrolled and ad hoc interactions between code Always define things at lowest level needed

Mark Dixon, SoCCE SOFT 136Page 27 Variable Scope: Errors In the following: Option Explicit Private Sub btnCalc_Click() Dim x As Integer x = 0 lblTotal.Caption = "£" & x End Sub Private Sub btnQuit_Click() x = 0 lblTotal.Caption = "£" & x End Sub Variable not defined error

Mark Dixon, SoCCE SOFT 136Page 28 Questions: Variable Scope Will this compile? Option Explicit … Dim x As integer … Private Sub thing() Dim z As Integer x = 23 y = "there" z = 12 end Private Sub btnTest_Click() Dim y As String y = "hello" x = 67 z = 53 End Sub Is x in scope? Is y in scope? Is z in scope? Is y in scope? Is x in scope? Is z in scope? Yes No Yes No

Mark Dixon, SoCCE SOFT 136Page 29 Example: Ball Char (v3) Option Explicit Private Sub tmrLeft_Timer() Me.picBallChar.Left = Me.picBallChar.Left If Me.picBallChar.Left < 0 Then tmrLeft.Enabled = False tmrRight.Enabled = True End If End Sub Private Sub tmrRight_Timer() Me.picBallChar.Left = Me.picBallChar.Left If Me.picBallChar.Left >= Me.ScaleWidth Then tmrRight.Enabled = False tmrLeft.Enabled = True End If End Sub

Mark Dixon, SoCCE SOFT 136Page 30 Example: Ball Char (v4) Option Explicit Dim xInc As Single Private Sub Form_Load() xInc = 100 End Sub Private Sub tmrMain_Timer() picBallChar.Left = picBallChar.Left + xInc If picBallChar.Left = frmBounce.ScaleWidth Then xInc = -xInc End If End Sub Variable (xInc) –reduces amount of code

Mark Dixon, SoCCE SOFT 136Page 31 Tutorial Exercise: GuessNum Task 1: Get GuessNum example working. Task 2: Modify GuessNum to tell the user whether their incorrect guess was higher or lower than the correct number. Task 3: Modify GuessNum to only allow 5 attempts before picking a new number.

Mark Dixon, SoCCE SOFT 136Page 32 Tutorial Exercise: Ball Char Task 1: Get Ball Char (v3) example working. Task 2: Add sound to Ball Char (v3) example Task 3: Get Ball Char moving diagonally, bouncing off all four sides of the window. Task 4: Modify your program so that it allows the user to control how fast the ball character moves.