SELECTION CSC 171 FALL 2004 LECTURE 8
Sequences start end
Example method public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a); a = a + a + a; System.out.println(“a == “ + a); a = a * a * a; System.out.println(“a == “ + a); }
Same statements? public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a); a = a + a + a; System.out.println(“a == “ + a); a = a * a * a; System.out.println(“a == “ + a); }
Sequence Statements public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a);//1 a = a + a + a; System.out.println(“a == “ + a); //3 a = a * a * a; System.out.println(“a == “ + a);// 27 }
start main() a=1; end main() a=a+a+a; a=a*a*a; System.out.println(“a == “ + a);
start main() a=1; System.out.println(“a == “ + a); a=a+a+a; System.out.println(“a == “ + a); a=a*a*a; System.out.println(“a == “ + a); end main() 1 a 327
Sequences start end
Sequence branching start end
Sequence branching start end
Sequence branching start end
Sequence branching start a=1; end a=a+a; a=a*a System.out.println(“a == “ + a); a<3 true false
Sequence branching start a=1; end a=a+a; System.out.println(“a == “ + a); a<3 true
Sequence branching start a=6; end a=a*a System.out.println(“a == “ + a); a<3 false
Sequence Branching public static void main(String[] args) { int a; a = 1 ; if (a < 3) { a = a + a; } else { a = a * a; } System.out.println(“a == “ + a);//2 }
Sequence Branching public static void main(String[] args) { int a; a = 6 ; if (a < 3) { a = a + a; } else { a = a * a; } System.out.println(“a == “ + a);//36 }
Slightly more practical start READ INCOME end tax = income *.3; tax = income *.4 System.out.println(“take home == “ + (income-tax) ); income < true false
Review Reading Numbers static double myGetDouble() { String localInput = ""; InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader) try { localInput = console.readLine(); } catch (IOException e) { System.out.println(e + " bad read "); System.exit(0); } return Double.parseDouble(localInput); }
Sequence Branching public static void main(String[] args) { double income, tax; final double taxRate1 = 0.3, taxRate2 = 0.4; income = getMyDouble() ; if (income < 75000) { tax = income * taxRate1; } else { tax = income * taxRate2; } System.out.println(“take == “ + (income - tax)); }
Blocks On single statements brackets are optional if (x < 4) System.out.println(“x less than 4”); else System.out.println(“x is not less than 4”); But Beware: if (x < 4) System.out.println(“x less than 4”); System.out.println(“that’s a small num!”);
Else is optional if (income > ) tax = tax + (income * extraTaxRate);
“simple” if branching start end true false ?
What about the conditions? if (income > ) tax = tax + (income * extraTaxRate); These are “boolean” expressions They have a value of “true” or “false”
Relational Operators < // less than <= // less than or equal to > // greater than >= // greater than or equal to == // equal to != // not equal to
Can be used in clauses double income = ; if (income > ) System.out.println(“Pay up!”);
Can be abstracted boolean b1; double income = ; b1 = ( income > ); if (b1) System.out.println(“Pay up!”);
Using “AND” double inc = ; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);
Using “OR” double inc = ; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);
Using “NOT” double inc = ; int numChild = 5; if !(!(inc 3)) System.out.println(“Pay less”);