Variables "There are 10 kinds of people in the world today – those who understand binary and those who don't!” Unknown.

Slides:



Advertisements
Similar presentations
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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.
VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
CS0004: Introduction to Programming Variables – Numbers.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Chapter 1: Introduction to Computers and Programming.
Rote Learning of the Week "A variable is a named section of RAM that stores data of a specific data type"
A Level Computing#BristolMet Session ObjectivesU2#S10 MUST describe the difference between constants, local and global variables SHOULD explain why constants.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CW-V1 SDD 0601 Principals of Software Design and Development Variables Constants Data Types Error Handling Starter: BlockbustersBlockbusters.
20/10/ Working with Data Declaring Variables, Overflow.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Arrays. Lesson Objectives  To understand what an array is and it’s function  To know how code and array in VB.
What is Programming? Computer programming is about telling the computer what it is we want it to do We tell the computer what we want it to do by sending.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Files Tutor: You will need ….
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
# 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
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Variables Continued In the last session we saw how variables are objects that allow us to store values in the RAM of the computer In this session we shall.
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.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Controlling Program Flow with Decision Structures.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
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 3 Vocab By: Christopher Schaffer 1B. Convert Class  The Convert class contains methods that return the result of converting a value to a specified.
Objects Allow us to represent and control the computer technology Use methods to make the object do something Use properties to find out and change the.
Unit 2 Technology Systems
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Data Types, Arithmetic Operations
Variables Data Types and Assignment
Starter Question In your jotter write the pseudocode to take in two numbers, add them together then display the answer. Declare variables RECEIVE firstNumber.
2. Understanding VB Variables
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Variables with Memory Diagram
Microsoft Visual Basic 2005 BASICS
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
VB Variables and Data
B065: PROGRAMMING Variables 1.
Introduction to Primitives
Variables Data Types and Assignment
Introduction to Primitives
B065: PROGRAMMING Variables 2.
Variables Data Types and Assignment
Presentation transcript:

Variables "There are 10 kinds of people in the world today – those who understand binary and those who don't!” Unknown

What is a Variable (Object)? Inside your computer are various bits of computer technology CD-ROM Hard Disk Drives RAM (Random Access Memory) Variables allow us to control the memory, the RAM

A Simple Calculation Consider 3 = A computer needs to set aside three areas of RAM One for the result Two for the other two numbers It will also perform the calculation in binary for example… = We as programmers really don’t want to get involved in thinking about memory and binary so we use variable objects to do the dirty work for us.

Examining the Swap Offer Functionality Once we have signed up and logged onto the system we see the following options… We shall examine the code for “Submit Offer”. To do this we shall add a break point to the code. (F9)

Declaring Variables Notice the following lines of code… These lines of code “declare” variables. “Dim” tells the language to set aside sections of RAM with the names we specify

Dim What this is doing is telling the computer to allocate one or more addresses in RAM, give it a name and limit the type of data it can hold. We are creating a variable object so that we can control some of the RAM. e.g. Dim ErrMsg as String Declares a variable called ErrMsg with the String data type. (Dim may be thought of as Declared In Memory even though it really means Dimension.)

So What is RAM? RAM may be thought of as a series of boxes that allow us to store data in. With these declarations Dim ErrMsg As String Dim OfferTitle As String Dim Description As String Dim SwapNo As Integer Dim Userno As Integer Dim As String Dim UserName As String We have created seven boxes in RAM for storing data

Data Types The data type is the name of the type of data that the variable is able to store The Integer data type only allows us to store whole numbers For example we could store in the variable SwapNo the following values… 0, 1 200, 50 i.e. all numeric whole numbers But not the following… “Kilroy Wos Ere”, 3.7 because… “Kilroy Wos Ere”text not numbers 3.7not a whole number (This would work but it would be rounded!)

Things to try What happens if… You delete the line Dim Description As String and try to run the program? (Notice the error messages and the tools to help you locate problems, use undo to fix the error.) You rename the variable SwapNo to SwapNumber and run the program? (As before notice the error messages and then fix the error.) You change the data type of Description from String to Integer? (You will need to use the program to see the problem.)

Assignment What does the word assignment mean? To assign something to a variable we use the assignment operator… = So to assign the number 1 to the FirstNumber variable we would use the following line of code… FirstNumber = 1

The Assignment Operator Using the debugger (F9, F10) we shall examine how the following highlighted line of code works.

Assigning Literal Constants There are times when we simply want to give a variable a value, often to initialise it. A literal constant is a set value specified by you the programmer. Take a look at the following… Dim DaysLeft As Integer DaysLeft = 365 This could also be written like so… Dim DaysLeft As Integer = 365

Performing Simple Calculations Take a look at the following code... Dim FirstNumber As Integer Dim SecondNumber As Integer Dim Result As Integer FirstNumber = 20 SecondNumber = 30 Result = FirstNumber + SecondNumber What is happening here and what will be the value of Result?

Finding the Definition of a Variable If you are looking at a section of code and you see a variable but cannot find where it is declared right click on it like so...

Data Type Conversion Errors The process of assigning data unsuitable for the specified data type is called a data type conversion error e.g. Dim MyAge as Integer MyAge = “Fred Bloggs”

Overflow Errors Different data types allow different ranges of values to be stored in them For example the largest number a variable with the Integer data type may store is Attempting to store a number larger than this will result in an overflow error. e.g. Dim MyAge As Integer MyAge =

Rules for Naming your Variables Make your Variable names Meaningful Don’t use A B & C for your variable names Not all characters may be used in variable names Spaces are not allowed My Variable – bad MyVariable – good Variables may contain a number but not start with one Use Camel Case when naming your variables InputVariable - good inputvariable - bad

Summary A variable allows us to control a section of RAM which has been given a name by you the programmer A variable has a data type which controls what sort of data it may store. (Any attempts to assign the wrong type of data to a variable will produce an error!) A variable must be declared before you may use it. The key word Dim is used in VB to do this A variable has a value this value is the data stored at whatever location in RAM is used The value of a variable is assigned using the assignment operator “=”. This may be a discrete value e.g. 1 Value=1 or the result of a calculation e.g.… Result = FirstNumber + SecondNumber

Questions Write down variable declarations that would store the following values: For example, an address: Answer: Dim AnAddress as String 1. A person’s name 2. A person’s age 3. A telephone number 4. A date of birth 5. The price of a kettle

Questions 6. Complete the following code that assigns the value 20 to a variable called ClassSize: Dim ClassSize as Integer ClassSize 7. Complete the following code that assigns the name “Fred Bloggs” to a variable called StudentName: Dim StudentName as String StudentName

Questions 8. A form has a text box called txtUserName. a. Write a suitable variable declaration for a variable to store this data: b. Write a suitable assignment statement that reads in this value from the form: 9. Write an assignment statement that assigns the result of to variable called Result: 10. Write an assignment statement that assigns the result of 4.5 x 2 to a variable called Result: