CS100J, Fall 2001 Weds 9/13 Lecture 5 ------------------------------------------------------------------------------- Announcements: + new? See Overview on www.cs.cornell.edu/Courses/cs100j/2001fa/! see also Grading-->Adding Late + P1 leftovers goto Carpenter by Fri 5pm + P2 still due 9/18 -- late submission for holidays and recent events OK (need to attach note and/or letter to front) + Next week: Chap 4.1, Chap 5.1-5.4 (9/25,9/27) + Next-Next week: All of Chap 4 + group sessions! + MATLAB pol + New section: Maybe Weds? ------------------------------------------------------------------------------- Topics: + more operators: mod (%) + more on assignments: named constants, final, swapping values + selection statements + loops (another kind of statement), using $while$ ------------------------------------------------------------------------------- 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) + string equality use $.equals$ (why? Strings are technically objects!) ------------------------------------------------------------------------------- More on assignments: + named constants - use variables for commonly used numbers - style: improves reuse and readability - but not truly constant because you could reassign + constants - can make variables "unvariable" with $final$: $final$ type variable = value - final varibles known as CONSTANTS - won't be able to reassign a constant - style: usually write a constant as UPPERCASE + swapping variable values int tmp, x, y; x=1;y=2; tmp = y; y = x; x = y; ------------------------------------------------------------------------------- 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 ------------------------------------------------------------------------------- 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 ------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------