Download presentation
Presentation is loading. Please wait.
1
SELECTION CSC 171 FALL 2004 LECTURE 8
2
Sequences start end
3
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); }
4
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); }
5
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 }
6
start main() a=1; end main() a=a+a+a; a=a*a*a; System.out.println(“a == “ + a);
7
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
8
Sequences start end
9
Sequence branching start end
10
Sequence branching start end
11
Sequence branching start end
13
Sequence branching start a=1; end a=a+a; a=a*a System.out.println(“a == “ + a); a<3 true false
14
Sequence branching start a=1; end a=a+a; System.out.println(“a == “ + a); a<3 true
15
Sequence branching start a=6; end a=a*a System.out.println(“a == “ + a); a<3 false
16
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 }
17
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 }
18
Slightly more practical start READ INCOME end tax = income *.3; tax = income *.4 System.out.println(“take home == “ + (income-tax) ); income < 75000 true false
19
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); }
20
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)); }
21
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!”);
22
Else is optional if (income > 120000) tax = tax + (income * extraTaxRate);
23
“simple” if branching start end true false ?
24
What about the conditions? if (income > 120000) tax = tax + (income * extraTaxRate); These are “boolean” expressions They have a value of “true” or “false”
25
Relational Operators < // less than <= // less than or equal to > // greater than >= // greater than or equal to == // equal to != // not equal to
26
Can be used in clauses double income = 73000.0; if (income > 120000.0) System.out.println(“Pay up!”);
27
Can be abstracted boolean b1; double income = 73000.0; b1 = ( income > 120000.0); if (b1) System.out.println(“Pay up!”);
28
Using “AND” double inc = 73000.0; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);
29
Using “OR” double inc = 73000.0; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);
30
Using “NOT” double inc = 73000.0; int numChild = 5; if !(!(inc 3)) System.out.println(“Pay less”);
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.