reading this week recap white space recap errors
play

Reading This Week Recap: White Space Recap: Errors CPSC 111, - PowerPoint PPT Presentation

University of British Columbia Reading This Week Recap: White Space Recap: Errors CPSC 111, Intro to Computation logical error 2009W2: Jan-Apr 2010 Chap 1: 1.3-1.8 White space run-time error Blanks between identifiers and other


  1. University of British Columbia Reading This Week Recap: White Space Recap: Errors CPSC 111, Intro to Computation logical error 2009W2: Jan-Apr 2010 ■ Chap 1: 1.3-1.8 ■ White space run-time error ■ Blanks between identifiers and other symbols ■ Chap 2: 2.1-2.2, 2.5 Tamara Munzner compile-time error ■ Tabs and newline characters are included ■ Chap 4: 4.1-4.2 ■ White space does not affect how program runs insight source object results editing translating executing ■ reminder: weekly reading questions due now! code code Data Types, Assignment, Casting, Constants ■ Use white space to format programs we create so they’re ■ Compile-time errors easier for people to understand Lecture 5, Fri Jan 15 2010 ■ syntax/structure ■ Run-time errors borrowing from slides by Kurt Eiselt ■ Logical errors ■ semantics/meaning http://www.cs.ubc.ca/~tmm/courses/111-10 1 2 3 4 Recap: Variables Recap: Data Types Recap: Variables and Data Types Recap: Floating Point Numbers ■ Variable: name for location in memory where data is ■ Java requires that we tell it what kind of data it is working with ■ significant digits //***************************************** = 4.2 * 10 = 4.2 * 10 1 ■ For every variable, we have to declare a data type stored ■ 42 // Test3.java Author: Kurt = 4.2 * 1 = 4.2 * 10 0 ■ Java language provides eight primitive data types // ■ 4.2 ■ avoid having to remember numeric addresses // Our third use of variables! = 4.2 * 10000000 = 4.2 * 10 7 ■ i.e. simple, fundamental ■ 42000000 //***************************************** ■ like variables in algebra class ■ For more complicated things, can use data types ■ .000042 = 4.2 * .00001 = 4.2 * 10 -5 public class Test3 ■ created by others provided to us through the Java libraries ■ only need to remember { ■ that we invent ■ Variable names begin with lower case letters public static void main (String[] args) ■ nonzero digits ■ More soon - for now, let’s stay with the primitives { ■ Java convention, not compiler/syntax requirement ■ where to put the decimal point ■ We want a , b , and c to be integers int a; //these int b; //are ■ floats around when multiply/divide by 10 ■ Here’s how we do it... int c; //variable declarations b = 3; c = 5; a = b + c; System.out.println ("The answer is " + a); } } 5 6 7 8 Data Type Sizes Variable Declaration Examples Variable Declaration and Assignment Assignment Type Size Min Max ■ person’s age in years ■ variable declaration is instruction to compiler //***************************************** int 4 bytes -2,147,483,648 2,147,483,647 ■ reserve block of main memory large enough to store // Test3.java Author: Kurt // double 8 bytes approx -1.7E308 approx 1.7E308 data type specified in declaration ■ height of mountain to nearest meter // Our third use of variables! (15 sig. digits) (15 sig. digits) //***************************************** ■ variable name is specified by identifier public class Test3 ■ syntax: ■ length of bacterium in centimeters { ■ fixed size, so finite capacity ■ typeName variableName; public static void main (String[] args) { Address Data int a; ■ number of pets at home 5802 10000101 int b; int c; 5803 10110101 10110101 one integer 5804 b = 3; // these 11110001 c = 5; // are 5805 00010100 a = b + c; // assignment statements 5806 System.out.println ("The answer is " + a); 5807 } } 9 10 11 12 Assignment Statements Assignment Statements Assignment Statements Assignment Statements ■ Assignment statement assigns value to variable ■ Java first computes value on right side ■ Here’s an occasional point of confusion: ■ Here’s an occasional point of confusion: ■ sometimes say binds value to variable ■ Then assigns value to variable given on left side ■ Assignment statement is a = 7; // what’s in a? a = 7; // what’s in a? b = a; // what’s in b? b = a; // what’s in b? x = 4 + 7; // what’s in x? ■ identifier // what’s in a now??? // what’s in a now??? ■ followed by assignment operator (=) System.out.println(“a is “ + a + “b is “ +b); ■ Old value will be overwritten if variable was assigned before ■ followed by expression ■ followed by semicolon (;) x = 2 + 1; // what’s in x now? ■ Find out! Experiments are easy to do in CS b = 3; c = 8; a = b + c; weekly_pay = pay_rate * hours_worked; ■ Note that = is no longer a test for equality! 13 14 15 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend