CS100J, Spring 2001 Thurs 2/1 Lecture 4 ------------------------------------------------------------------------------- Announcements: + E2 (posted weds) due Tues 2/6 (part of P2) + P2 due Thurs 2/8 (include E2 even if you did it!) + T1 (prelim 1) Tues 2/13 + group sessions start Sun 1-3pm ------------------------------------------------------------------------------- Topics: + assignments (named constants) + conditions (swap example) + loops (another kind of statement) + using $while$ for loops ------------------------------------------------------------------------------- Summary from lecture 3: + representing information with variables + Java is strongly typed + variables: declare, assign, use + conditions for making choices + use $if$ and booleans ------------------------------------------------------------------------------- From past lectures, you should be able to: + write comments with // and /* */ + know Java data types + store and use information with variables + perform arithmetic with operators and literals + generate output with $System.out.println$ + swap data (see Swap.java in Examples) + find min/max values with conditions (see Max.java) ------------------------------------------------------------------------------- Important Examples: + See Spring 2000 examples for additional help + everything up to nested2 ------------------------------------------------------------------------------- More on values: + random numbers: Math.random() -> generates double between [0,1) see random.java ------------------------------------------------------------------------------- More on operators: + remainder: % + x % y generates the remainder of x/y + ex) 10 % 5 -> 0 (10/5 = 2 with 0 as remainder) 10 % 3 -> 1 (10/3 = 3 with 1 as remainder) ------------------------------------------------------------------------------- More on assignments: + named constants - use variables for commonly used numbers - style: improves reuse and readability - but not truly constant because you could reassign + can make variables "unvariable" with $final$: $final$ type variable = value - final varibles known as CONSTANTS - won't be able to reassign a constant + swapping variable values int tmp, x, y; x=1;y=2; tmp = y; y = x; x = y; ------------------------------------------------------------------------------- More on conditions: + nested selection statements + each selection statement is indeed a statements + so, the statements in the selection statements could be another selection! + ex) if (temperature < 100) if (temperature > 80) System.out.println("hot"); else System.out.println("not hot"); + sometimes you can compress nested ifs with logical operators ------------------------------------------------------------------------------- More on statements: + sometimes you need to do more than one thing in selection statement + other statements might need {} - look at { } for $main$ and the class containing $main$ - group statements together with { } + ex) if (x > 10) { y = x; System.out.println("hi!"); } + both the assignment and output will happen if x > 10 + called BLOCK STATEMENT ------------------------------------------------------------------------------- Loops: + what if need to repeat something? + could use lots of assignments and conditions, but.... + avoid redundancy to clarify code (spaghetti code) + repetition? use loops (repetition statements) ------------------------------------------------------------------------------- Syntax: + $while$, $for$, $do$ + focus on $while$ + while(cond) s; ------------------------------------------------------------------------------- Occasions to use: + definite - repeat/echo - accumulate + indefinite - conditional update -------------------------------------------------------------------------------