Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.

Slides:



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

Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
Python November 14, Unit 7. Python Hello world, in class.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & 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 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 04 – Data Types. Mark Dixon Page 2 Admin: On-line Quiz.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CS0004: Introduction to Programming Variables – Numbers.
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.
2440: 211 Interactive Web Programming Expressions & Operators.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
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.
Mathematical Calculations in Java Mrs. G. Chapman.
Numerical Functions & Tricks In today’s lesson we will look at: why we might want to use mathematical techniques some useful functions other mathematical.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
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.
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
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)
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
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
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.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
GCSE Computing: Programming GCSE Programming Remembering Python.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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)
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Making Interactive Programs with Visual Basic .NET
© 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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Representing Data: Constants and Variables
Chapter 2 Basic Computation
04 – Information Processing: Expressions, Operators & Functions
Visual Basic Variables
Topic: Python’s building blocks -> Statements
Variables, Expressions, and IO
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CS150 Introduction to Computer Science 1
Data Types and Expressions
Chapter 2 Programming Basics.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions

Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Introduce you to data storage concepts, i.e. data types and variables –Introduce you to processing concepts, i.e. operators and functions Objectives, by end of this week’s sessions, you should be able to: –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 131Page 3 Information Processing All computing problems: –involve processing information/data information has meaning (e.g. 5lb 3.3kg 18 years) data has no meaning (e.g ) –following this pattern: For example: –to multiply two numbers: 7 * 9 = 63 Input DataProcessOutput Data *

Mark Dixon, SoCCE SOFT 131Page 4 Information Processing (cont.) Hence, to solve any computing problem ask: –what information goes in –what processing is done to it –what information comes out

Mark Dixon, SoCCE SOFT 131Page 5 Example 1: Multiply Option Explicit Private Sub btnMultiply_Click() lblResult.Caption = txtNum1.Text * txtNum2.Text End Sub Multiply

Mark Dixon, SoCCE SOFT 131Page 6 Expressions: Evaluation, & Substitution The following assignment statement: lblResult.Caption = txtNum1.Text * txtNum2.Text contains an expression Given values for txtNum1.Text and txtNum2.Text txtNum1.Text = "7", txtNum2.Text = "9" can evaluate expression: lblResult.Caption = txtNum1.Text * txtNum2.Text (from above) lblResult.Caption = "7" * "9" (substitute) lblResult.Caption = 63 (calculate)

Mark Dixon, SoCCE SOFT 131Page 7 Example 2: AddNum v1 Option Explicit Private Sub btnAdd_Click() lblResult.Caption = txtNum1.Text + txtNum2.Text End Sub AddNum

Mark Dixon, SoCCE SOFT 131Page 8 Functions & Operators Used to: –process (manipulate) data Both Functions & Operators: –take input data/parameters (1 or more item) –process it –return a result which replaces the expression (substitution) Function Parameter(s) Result

Mark Dixon, SoCCE SOFT 131Page 9 Functions & Operators (cont.) Functions: come before the data (which is in brackets) Sqr(16) square root function result is 4 Abs(-23) absolute value function result is 23 Int(2.543) integer function result is 2 Val("63") value function result is 63 Left$("123",2) left string function result is "12" Operators: sit between the data addition operator result is subtraction operator result is 3 5 * 2 multiplication operator result is 10 5 / 2 division operator result is 2.5 "5" & "2" string concatenation result is "52"

Mark Dixon, SoCCE SOFT 131Page 10 Exercise 1: Expressions What is the result of: 1 + Val("23") + Int( ) + Sqr(Int(9.4523)) What is the result of: "23" & "18" + Left$("bob",1) + Right$("sal",2) Write an expression to: give integer value of " " Write an expression to: give the first two letters of "Mr John Smith" = 29 "23" & "18" & "b" & "al" = "2318bal" Int(Val(" ")) Left$("Mr John Smith", 2)

Mark Dixon, SoCCE SOFT 131Page 11 Example 3: AddNum v2 Option Explicit Private Sub btnAdd_Click() lblResult.Caption = Val(txtNum1.Text) + Val(txtNum2.Text) End Sub AddNum

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

Mark Dixon, SoCCE SOFT 131Page 13 Data Types Integer – whole numbers Long – whole numbers (large) Single – decimal numbers Double – decimal numbers (more precise) Currency – money String – text

Mark Dixon, SoCCE SOFT 131Page 14 Data Type Selection

Mark Dixon, SoCCE SOFT 131Page 15 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 131Page 16 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)

Mark Dixon, SoCCE SOFT 131Page 17 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 131Page 18 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 131Page 19 Exercise 2: 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 131Page 20 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 131Page 21 Exercise 3: 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 131Page 22 Example 4: 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 131Page 23 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 131Page 24 Exercise 5: 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