CS100J, Fall 2001 Tues 9/11 Lecture 4 ------------------------------------------------------------------------------- Announcements: + new? See Overview on www.cs.cornell.edu/Courses/cs100j/2001fa/! + P1 handed back in Section -- no section #, goes to.... + Readings: all of Chap 3 for 9/11,9/13,9/18,9/20 + Future reading: Chap 4.1, Chap 5.1-5.4 (9/25,9/27) + office hours set: see Staff-->Staff Listing + P2 due 9/18: holidays? need letter from parents and/or rabii Try to do early submission on Mon to avoid getting behind, but will accept work on 9/20 in lecture + group sessions! + Macs poll, MATLAB poll/talk + New section: Mon 11:15, 12:20, 1:10 ------------------------------------------------------------------------------- Topics: + statements - empty - expression - method - declare - assignment - selection ------------------------------------------------------------------------------- Statements: + combine tokens to create "sentences" + each "sentence" forms an instruction/command + "sentences" are called statements + end statements with semicolon, which is a punctuation token + starting a new line does NOT mean you have a new statement! (in MATLAB, a new line DOES start a new statement) + Java executes each statement top-down, left-to-right (within a statement, operator precedence determines direction of execution) + group "related" statements together with a BLOCK STATEMENT with {} + see Java in Nutshell pp43-44 for full list ------------------------------------------------------------------------------- Statement: Empty + doesn't effectively do anything ex) ; (Java succeeds and moves to next statement) ------------------------------------------------------------------------------- Statement: Expression + combine constants with operators + precedence: some operators "come before" others + associativity: operators "work" left to right or right to left ex) 1 + 1 2 * 2 - 4 // technically means (2*2) - 4 because of precedence 2 - 1 - 3 // technically means (2-1) - 3 due to associativity + use () to force some expressions to evaluate first ex) (1 + 1) // () not needed 2 * (2 - 4) 2 - (1 - 3) + Java does not actually allow arithmetic expressions as individual statements ex) 1+1; // causes a syntax error (other languages, like MATLAB, do!) + beware of mixing types! ex) System.out.println(1/2) yields 0, not 0.5 + mixing doubles and ints makes doubles ex) System.out.println(1.0/2) yields 0.5 + use CAST to force different value syntax: (type) expression ex) System.out.println( (int) 9.8) yields 9 (an integer) ------------------------------------------------------------------------------- Statement: Declaration: + want identifiers to represent other values + variables (identifiers) can hold values + values can be primitive types + can also be references, but that deals with objects, so we'll skip this for now + Java is strongly typed, so must tell Java about variables before using syntax: variabletype var ; ex) char c; int value; + cannot redeclare a variable once it is declared inside { } ------------------------------------------------------------------------------- Statement: Assignment + special kind of expression statement + store value in a variable that's been declared + cannot use variable until it's declared! + syntax: declaredvar = value + the value's type MUST match the variable's type ex) int x; x = 1; + now, x has the value 1 + think x <- 1 because assign operator associates right-to-left + can say in English "x gets 1" + later in code, Java "knows" x means 1 + cannot redeclare variable unless "elsewhere" in code (too hard to explain for now -- so, just don't redeclare!) + shortcut: o can combine syntax for shortcut: o syntax: type var = value o ex) int y = 7; o called INITIALIZING a variable (declaring and assigning initial value) ------------------------------------------------------------------------------- Statement: Method Call + something1(something2) has the syntax name(expression) + think function --> Java will do whatever something1 instructs ex) System.out.println("Hello"); ex) ------------------------------------------------------------------------------- Statement: Style issues + usually one statement per line + leave space between operators and operands (values operated upon) + use descriptive variable names + avoid hard coding values (using specific values that) o use named constants o variables initialized "early" in code and used instead of the constants ------------------------------------------------------------------------------- Conditions: + execution of program goes in order of operators, starting from 1st statement + some say "top-down" execution + some statements can change FLOW OF CONTROL (order of which statements execute) + sometimes need to make choices (example: comparison) + tree concept + use if, if-else (and if-else if) + syntax: if (c) // if c is true s; // do s // otherwise, skip this statement if (c) // if c is true s1; // do s1 else // otherwise s; // do s2 + other varieties: if (c1) s1 else if (c2) s2 if (c1) s1; else if (c2) s2; else s3; + can have as many else-ifs as you wish + indent statements, but entire body counts as SINGLE statement! (indenting does not make a new statement, just improves clarity) + can do multiple statements under each condition: - use statement block - { statements } + language elements: - if, else are keywords - conditions? must evaluate to true or false (Boolean) o relations: < (less than) > (greater than) <= (less than or equal) > (greater than or equal) == (equal, but do NOT use =) != (not equal) o logic: && (and) || (or) ! (not) o values: true false -------------------------------------------------------------------------------