CS112 Scientific Computation Department of Computer Science Wellesley College Storing Values for Safe Keeping Variables and their Values.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Python
Chapter 2 Writing Simple Programs
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Basic Elements of C++ Chapter 2.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
CMPS 1371 Introduction to Computing for Engineers MatLab.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Variables, Expressions and Statements
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Matlab for Engineers Matlab Environment Chapter 2.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CRE Programming Club Class 2 (Import JJZ543 and Practice Your Typing!)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
C++ Basics Lecture 2.
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Python Let’s get started!.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Variables, Expressions, and IO
OUTPUT STATEMENTS GC 201.
Basic Elements of C++ Chapter 2.
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS111 Computer Programming
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
Topics Designing a Program Input, Processing, and Output
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Getting Started in Python
Electrical and Computer Engineering Department SUNY – New Paltz
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CS112 Scientific Computation Department of Computer Science Wellesley College Storing Values for Safe Keeping Variables and their Values

Variables2-2 Variables ● We store information in variables that have two parts: ● Value can be a number or string of characters ● Values are assigned to variables using an assignment statement >> result = 12 result 12 name value

Variables2-3 Assignment statements ● General format of an assignment statement: variable-name = expression ● Right hand side can be any expression that evaluates to a value: >> hypotenuse = sqrt(3^2 + 4^2) ● Executing an assignment statement: 1) evaluate the expression on the right side 2) assign value of expression to variable named on the left side

Variables2-4 Variables inside expressions ● Inside an expression, we can refer to the value of a variable by its name >> (hypotenuse^2 - 5) / 2 ● Assignment statements using variables on the right side are no problem, provided they have a value >> age = 20 >> happyBirthday = age + 1 age happyBirthday

Variables2-5 Not your typical algebra ● The same variable name can appear on both sides of an assignment statement: >> age = 20 >> happyBirthday = age + 1 >> age = age - 12 age happyBirthday

Variables2-6 Variables and the MATLAB Workspace

Variables2-7 Hunting for ‘s >> x = 17 >> x + 1 = 10 >> x = x / x + x >> diff = (x - y) / 2 >> end = x - 18 >> y = x = 2

Variables2-8 Assigning variables in a script ● Statements in script are executed as if they were typed directly into the Command Window ● Variables created in CW can be changed by assignments in scripts and vice versa! % assignVars.m % assigns variables % to values a = 1 b = 2 c = 3

Variables2-9 Speaking of ‘s ● What would happen if the script contained a syntax error? For example: % buggyScript.m a = 10 b = 20 + c = 30

Variables2-10 What’s in a name? ● Variable names must start with a letter and may contain any number of letters, digits and underscore characters (‘_’) C3PO my_monthly_pay yourTurn ● Case matters! sohie ≠ Sohie ● MATLAB reserves some words for special purposes: break case catch continue else elseif end for function global if otherwise persistent return switch try while

Variables2-11 Choose concise, meaningful names ● The good, the bad, & the ugly maxArea xxyyzz_3b totalMonthlyHedgeFunds 2pi tf result ● Case matters! sohie ≠ Sohie

Variables2-12 Time-out exercise ● Write a sequence of assignment statements that exchange the values of thing1 & thing2 ● Before ● After thing1 thing2 2 5 thing1 5 2 thing2

Variables2-13 Floating point numbers ● Decimal numbers are represented as a type called double >> number = 8.43 number = ● MATLAB normally prints 4 digits after the decimal point and rounds numbers up or down to fit* * Compact notation is used only for printing - try typing format long

Variables2-14 Really big & really little numbers ● MATLAB uses scientific notation to print very large or very small numbers >> number = number = e+009 >> number = number = e-004 ● MATLAB has a special representation for really big numbers >> number = 1.2e+9999 number = Inf * What do get when you type: 1/0 or 1/inf or 0*inf?

Variables2-15 Strings ● Strings consist of letters, digits, symbols, and white space, surrounded by single quotes: myName = ‘Sam I am’ eec = ‘ )&it:s;elf,’ thirteen = ‘6 + 7’ ● Common ‘s callMe = Ishmael reply = ‘Madam, I’m Adam’

Variables2-16 Unfriendly programs ● The following program converts pounds to stones: % weights.m pounds = input(‘ ’); stones = * pounds ● If we run it by typing >> weights it would just sit there and look at us

Variables2-17 Friendly programs ● Tell the user what’s needed and what’s printed: % weights.m pounds = input(‘Enter your weight in pounds: ’); disp(‘Your weight in stones is: ’); stones = * pounds ● When run… >> weights Enter your weight in pounds: 120 Your weight in stones is: stones =

Variables2-18 Credit Thing 1 and Thing 2 from Dr. Seuss, The Cat and the Hat