Ch05 Visual Aids

20
© 2007 Lawrenceville Press Slide 1 Chapter 5 Chapter 5 The if Statement The if Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5.

description

Eclipse

Transcript of Ch05 Visual Aids

Page 1: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 1

Chapter 5Chapter 5

The if StatementThe if StatementChapter 5Chapter 5

The if StatementThe if Statement

Conditional control structure, also called a decision structure

Executes a set of statements when a condition is true

The condition is a Boolean expression

For example, the statementif (x == 5) {

y = 20;}

assigns the value 20 to y only if x is equal to 5.

Page 2: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 2

Chapter 5Chapter 5

Relational OperatorsRelational OperatorsChapter 5Chapter 5

Relational OperatorsRelational Operators

Operator Meaning== equal< less than<= less than or equal> greater than>= greater than or equal!= not equal

Page 3: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 3

Chapter 5Chapter 5

The if-else StatementThe if-else StatementChapter 5Chapter 5

The if-else StatementThe if-else Statement

Contains an else clause that is executed when the if condition evaluates to false. For example, the statement

if (x == 5) {y = 20;

} else {y = 10;

}assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.

Page 4: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 4

Chapter 5Chapter 5

Nested if-else StatementsNested if-else StatementsChapter 5Chapter 5

Nested if-else StatementsNested if-else Statements

Should be indented to make the logic clear. Nested statement executed only when the

branch it is in is executed. For example, the statement

if (x == 5) {y = 20;

} else {if (x > 5) {

y = 10;} else {

y = 0;}

}evaluates the nested if-else only when x is not equal to 5.

Page 5: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 5

Chapter 5Chapter 5

The if-else if StatementThe if-else if StatementChapter 5Chapter 5

The if-else if StatementThe if-else if Statement

Used to decide among three or more actions.

Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement

if (x < 5) {y = 20;

} else if (x < 10) {y = 40;

} else if (x < 15) {y = 80;

} would give very different results if the conditions were ordered differently.

Page 6: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 6

Chapter 5Chapter 5

The switch StatementThe switch StatementChapter 5Chapter 5

The switch StatementThe switch Statement

Used to decide among three or more actions.

Uses an expression that evaluates to an integer.

The break statement moves program execution to the next statement after the switch.

The default code is optional and is executed when none of the previous cases are met:switch (numLegs) {

case 2: System.out.println("human"); break;case 4: System.out.println("beast"); break;case 8: System.out.println("insect"); break;default: System.out.println("???"); break;

}

Page 7: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 7

Chapter 5Chapter 5

The Math ClassThe Math ClassChapter 5Chapter 5

The Math ClassThe Math Class

Part of the java.lang package

The random() methods generates a double between 0 and 1.0. For example,

double rNum;rNum = Math.random();

A random integer in a range is generated by using the expression:(highNum – lowNum + 1) * Math.random() + lowNum

Page 8: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 8

Chapter 5Chapter 5

Compound Boolean ExpressionsCompound Boolean ExpressionsChapter 5Chapter 5

Compound Boolean ExpressionsCompound Boolean Expressions

More than one Boolean expression in a single condition.

Formed using the logical And (&&), logical Or (||), or logical Not (!) operators.

Page 9: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 9

Chapter 5Chapter 5

And Truth TableAnd Truth TableChapter 5Chapter 5

And Truth TableAnd Truth Table

AndAnd

Exp1Exp1 Exp2Exp2 ResultResult

True True True

True False False

False True False

False False False

Page 10: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 10

Chapter 5Chapter 5

Or Truth TableOr Truth TableChapter 5Chapter 5

Or Truth TableOr Truth Table

OrOr

Exp1Exp1 Exp2Exp2 ResultResult

True True True

True False True

False True True

False False False

Page 11: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 11

Chapter 5Chapter 5

Not Truth TableNot Truth TableChapter 5Chapter 5

Not Truth TableNot Truth Table

NotNot

ExpExp ResultResult

True False

False True

Page 12: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 12

Short Circuit EvaluationShort Circuit EvaluationShort Circuit EvaluationShort Circuit Evaluation

• Java uses Short Circuit Java uses Short Circuit Evaluation to determine the Evaluation to determine the result of a compound Boolean result of a compound Boolean expressionexpression– Left side is evaluated firstLeft side is evaluated first

– If the result of the entire expression can be If the result of the entire expression can be determined by the value of the left determined by the value of the left operand, then no other operands will be operand, then no other operands will be evaluatedevaluated

Page 13: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 13

Order of OperationsOrder of OperationsOrder of OperationsOrder of Operations

• Not (!) evaluated firstNot (!) evaluated first

• And (&&) evaluated secondAnd (&&) evaluated second

• Or (||) evaluated lastOr (||) evaluated last

Page 14: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 14

Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4

size=100size=100 weight=50weight=50 value=75value=75

1.1. size > 50 && weight ==50 t or size > 50 && weight ==50 t or f?f?

2.2. size > 50 || weight ==50size > 50 || weight ==50 t or f?t or f?

Which one of the above can be Short Which one of the above can be Short Circuited?Circuited?

Page 15: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 15

Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4

size=100size=100 weight=50weight=50 value=75value=75

1.1. !(value<75) !(value<75)

2.2. size >=100 || value >=100size >=100 || value >=100

3.3. Can #2 be short circuited?Can #2 be short circuited?

Page 16: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 16

Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4

size=100size=100 weight=50weight=50 value=75value=75

1.1. value < 100 && !(weight==50)value < 100 && !(weight==50)

2.2. !(size>100 && weight>50 && !(size>100 && weight>50 && value>75)value>75)

3.3. (value<125 || weight<76) && size==100(value<125 || weight<76) && size==100

Page 17: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 17

Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4Quarter 2 Drill # 4

Write the following:Write the following:

1.1. Value not greater than 75 Value not greater than 75 or size not greater than 100or size not greater than 100

Page 18: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 18

Chapter 5Chapter 5

The Math ClassThe Math ClassChapter 5Chapter 5

The Math ClassThe Math Class

Part of the java.lang package

Methods include:

abs(num) returns the absolute value of num

pow(num1, num2) returns num1 raised to the num2 power

sqrt(num) returns the square root of num, where num is a positive number

Page 19: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 19

Chapter 5Chapter 5

Flowchart SymbolsFlowchart SymbolsChapter 5Chapter 5

Flowchart SymbolsFlowchart Symbols

decisiondecision

Page 20: Ch05 Visual Aids

© 2007 Lawrenceville PressSlide 20

Chapter 5Chapter 5

The RPS FlowchartThe RPS FlowchartChapter 5Chapter 5

The RPS FlowchartThe RPS Flowchart