selection csc 171 fall 2004 lecture 8. sequences start end

30
SELECTION SELECTION CSC 171 FALL 2004 LECTURE 8

Post on 21-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

SELECTIONSELECTION

CSC 171 FALL 2004

LECTURE 8

Page 2: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

SequencesSequencesstart

end

Page 3: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Example methodExample methodpublic 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);

}

Page 4: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Same statements?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);

}

Page 5: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence StatementsSequence Statementspublic static void main(String[] args) {

int a;a = 1 ;System.out.println(“a == “ + a);//1a = a + a + a;System.out.println(“a == “ + a); //3a = a * a * a;System.out.println(“a == “ + a);// 27

}

Page 6: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

start main()

a=1;

end main()

a=a+a+a;

a=a*a*a;

System.out.println(“a == “ + a);

System.out.println(“a == “ + a);

System.out.println(“a == “ + a);

Page 7: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

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

3 27

Page 8: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

SequencesSequencesstart

end

Page 9: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

end

Page 10: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

end

Page 11: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

end

Page 12: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end
Page 13: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

a=1;

end

a=a+a; a=a*a

System.out.println(“a == “ + a);

a<3true false

Page 14: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

a=1;

end

a=a+a;

System.out.println(“a == “ + a);

a<3true

Page 15: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence branchingSequence branchingstart

a=6;

end

a=a*a

System.out.println(“a == “ + a);

a<3false

Page 16: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence BranchingSequence Branchingpublic 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

}

Page 17: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence BranchingSequence Branchingpublic 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

}

Page 18: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Slightly more practicalSlightly more practicalstart

READ INCOME

end

tax = income * .3; tax = income * .4

System.out.println(“take home == “ + (income-tax) );

income < 75000true false

Page 19: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Review Reading NumbersReview Reading Numbersstatic 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);

}

Page 20: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Sequence BranchingSequence Branchingpublic 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));

}

Page 21: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

BlocksBlocksOn 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!”);

Page 22: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Else is optionalElse is optionalif (income > 120000)

tax = tax + (income * extraTaxRate);

Page 23: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

““simple” if branchingsimple” if branchingstart

end

true

false

?

Page 24: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

What about the conditions?What about the conditions?if (income > 120000)

tax = tax + (income * extraTaxRate);

These are “boolean” expressionsThey have a value of “true” or “false”

Page 25: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Relational OperatorsRelational Operators< // less than<= // less than or equal to> // greater than>= // greater than or equal

to == // equal to!= // not equal to

Page 26: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Can be used in clausesCan be used in clauses

double income = 73000.0;

if (income > 120000.0) System.out.println(“Pay up!”);

Page 27: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Can be abstractedCan be abstractedboolean b1;double income = 73000.0;b1 = ( income > 120000.0);if (b1)

System.out.println(“Pay up!”);

Page 28: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Using “AND”Using “AND”

double inc = 73000.0;int numChild = 5;if ((inc < 50000.0) && (numChild >

3))System.out.println(“Pay less”);

Page 29: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Using “OR”Using “OR”

double inc = 73000.0;int numChild = 5;if ((inc < 50000.0) || (numChild > 3))

System.out.println(“Pay less”);

Page 30: SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end

Using “NOT”Using “NOT”

double inc = 73000.0;int numChild = 5;if !(!(inc < 50000.0) && !(numChild >

3))System.out.println(“Pay less”);