CS100J, Spring 2001
Tues 1/30
Lecture 3
-------------------------------------------------------------------------------
Announcements:
+ newcomers: see Overview on website; see Announcements for what to do about P1
+ P2 due Thurs 2/8
+ T1 (prelim 1) Tues 2/13
+ CS100J sections start this week
+ don't go to Section 5 (use 6 or another instead)! (temporarily cancelled)
+ group sessions starrt Sun 1-3pm
-------------------------------------------------------------------------------
Topics:
+ statements
+ arithmetic
+ variables and assignments
+ conditions
-------------------------------------------------------------------------------
Summary from lecture 2:
+ need written computer language to communicate with computer
+ written language makes tokens from characters
+ write statements (instructions) from tokens
+ assignment as important statement
-------------------------------------------------------------------------------
Statements:
+ combine tokens to create "sentences"
+ each "sentence" forms an instruction/command
+ "sentences" are called statements
+ end statements with semicolon
+ going to a new line does NOT mean you have a new statement!
+ Java executes each statement top-down, left-to-right
  (within a statement, operator precedence determines direction of execution)
+ see Java in Nutshell pp43-44 for full list
-------------------------------------------------------------------------------
+ Empty statement
  ex) ; 
  - doesn't actually do anything
-------------------------------------------------------------------------------
+ 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);
  - 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)
-------------------------------------------------------------------------------
+ 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;
-------------------------------------------------------------------------------
+ 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 until the current METHOD (we use main) ends
  - 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)
-------------------------------------------------------------------------------
+ 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
-------------------------------------------------------------------------------
+ Swap example
-------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------
Loops:
+ statement for repeated actions