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:

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Introduction to Python
Input & Output: Console
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Primitive Data Types. Identifiers What word does it sound like?
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.
Chapter 2 Variables.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Data And Variables Chapter Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science 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?
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.
1 Variables and Arithmetic Operators in JavaScript.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
A Sample Program #include using namespace std; int main(void) { cout
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Bill Tucker Austin Community College COSC 1315
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Basic Computation
Basic Elements of C++.
Multiple variables can be created in one declaration
Chapter 2 Basic Computation
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Variables ICS2O.
Numbers.
Unit-1 Introduction to Java
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.
Chapter 2 Variables.
Chapter 2: Introduction to C++.
The Data Element.
Primitive Types and Expressions
Unit 3: Variables in Java
The Data Element.
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

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: ▫At the start of a program: x = 1 ▫In the middle of the program: x = 5 ▫At the end of the program: x = 0 2

Why do we need variables? The exact same program/code can run with different results ▫interactive vs. non-interactive ▫The difference between a movie and a video game Programs are easier to read ▫Variables can have meaningful names ▫Numbers don’t have much meaning to us beyond their value 3

3 Parts to a Variable 1)The variable type 2)The variable identifier 3)The variable value 4

Understanding Variables as Pictures A variable is like a box in a computer’s memory The type of variable is like the shape of the box. The variable identifier is like a label on the box The value is what is stored inside the box (string) name Ovechkin 23 5

Common Data Types in Turing int Whole numbers real Decimal numbers boolean true or false string Sequence of characters e.g. “Mr. Lane”, “Computer” 6

Data Type Value Ranges string: Essentially any length of text boolean: true or false int:  real: x  x

Using Variables – Identifier Rules Identifiers ▫Can contain numbers but cannot start with one  E.g. number1 is valid, 1number is NOT valid ▫Can contain underscores, _ ▫Cannot contain any spaces ▫Cannot contain special characters, e.g. *, -, “ ▫Cannot be reserved words, e.g. int, put, View, etc. 8

Using Variables – Identifier Conventions Conventions are what is expected, but are not rigid rules Identifiers ▫Should describe the variable’s purpose ▫Should use camel-case. This means that for a variable identifier with multiple words, each word except the first starts with a capital letter. E.g., studentNumber, myFavouriteColour 9

Using a Variable The value can be any expression as long as it matches the same type as the identifier Expression: Something that results in a value E.g. (5 + 4), “bob”, userAge + birthYear 10 1)Declare the type and identifier var : 2) Assign the value := operator :=

Assignment Statements 11 Give a value to a variable. Use the assignment operator ( := ) The variable to be assigned a value is placed on the left-hand side of the := operator. The value to be assigned is placed on the right- hand side of the := operator. The value can be a constant, a literal, another variable, or an expression.

Assigning Variables to Other Variables Just like assigning a variable to an expression we follow the same steps E.g. var twinBrotherAge : int var twinSisterAge : int twinBrotherAge := 15 twinSisterAge := twinBrotherAge How does a computer handle this last command? 12

Using a Variable Examples %Variable declarations and assignments var sum : int% Declaration sum := 4% Variable assignment %What exactly is the line below doing? sum := sum + 1% Variable assignment 13

A Shortcut Most times you know what the initial (starting) value of a variable should be when you create it. Instead of declaring and then assigning a variable you may put these two lines into one Example: var name : string := “Mr. Lane” var sum : int := 0 14

Operators OperatorExampleDescription +Price + Tax Adds two values -Cost - Discount Subtracts one value from another *Hours * Wage Multiplies two values /AnnualSalary / 12 Divides one value by another, remember you cannot store a decimal into an int +FirstName + LastName Joins / Concatenates two strings div5 div 2 Divides an integer by the second number and truncates (cuts off) all decimal values 15

Increment and Decrement Operators Increment means to increase a value by a consistant amount Conversely Decrement means to decrease a value by a consistant amount E.g. To increment a variable named count by 3 count := count + 3 NOTE: we need to add the increment value to the current value of count to get the new result 16

Expressions Examples 17 A statement that returns a value when executed ExpressionEvaluates to trueReturns true 5Returns Returns * 9Returns 74.7 “Mr. ” + “Lane”Returns “Mr. Lane”

18 Expressions A statement that returns a value when executed Examples %alphabet = “ABCDE”; var alphabet : string alphabet := “A” + “B” + “C” + “D” + “E” %final value of year is “The year is 2009”; var year : string year := “The year is ” ;

Expressions You should NEVER do calculations in an output statement. Instead assign the result of a calculation into a variable then output the variable Example: var sum : int := 0; sum := var average : real := sum/4 put average 19

Expressions 20 ExpressionEvaluates to “ York Mills Road”“490 York Mills Road” var maker : string := “Aston Martin” var model : string := “Vanquish” var car : string := maker + “ ” + model%“Aston Martin Vanquish”

Using a Variable Examples % Variable declarations and assignments var isSkyBlue : boolean%var : isSkyBlue := true% Variable assignment % := NOTE: We only declare the variable once then we can assign and access the data as much as we want for the rest of the program! 21

Using a Variable Examples % Declaration and assignment var myMark : real myMark := put myMark%outputs myMark := myMark / 3%Assignment put myMark%outputs

Constants Constants are similar to variables, except that you provide a value for the constant when you declare it, and its value can never change const : := We cannot assign new values, but can use them like normal variables 23

Constants Same identifier rules as for variables ▫No spaces, special characters ▫Cannot start with a number ▫Cannot be a reserved word Conventions are different ▫All uppercase letters ▫Instead of camel-case, separate all words by an underscore ▫E.g., THIS_IS_A_CONSTANT_IDENTIFIER 24

Discussion Why do we have variables with different sizes? ▫More efficient space usage in memory – think differently sized aircraft, the right size for the job ▫Importance depends on size of program 25