conditional control structures chapter 5. goals and objectives understand conditional control...

23
Conditional Control Structures Chapter 5

Upload: joy-hopkins

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

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 that evaluates to True or False  For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5.

TRANSCRIPT

Page 1: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Conditional Control StructuresChapter 5

Page 2: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Goals and ObjectivesUnderstand conditional control structures. Demonstrate the use of decision structures to control the flow of a program.Generate random numbers.Write compound Boolean expressions

Access methods in the Math class (Math.random(), Math.abs(), Math.pow(), Math.sqrt()). Create and modify solutions to problems.Develop code with correct and efficient use of conditional control structures.Understand and modify existing code.

Page 3: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

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 that

evaluates to True or False For example, the statement

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

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

Page 4: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The if statementBeware of using the assignment symbol instead of the comparison symbol.

Exampleif (x = 3) //This will not cause an error

y = 4; Correct version:if(x ==3)

y = 4;

Page 5: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Begin and End SymbolsYou don’t need a begin or an end symbol if you only have one statement for your if statement.

if(x == 4)y = 6;

Page 6: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Relational Operators

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

Page 7: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The if-else StatementContains 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 8: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Nested 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 9: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The 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 10: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The 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 11: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The 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 12: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Compound Boolean Expressions

More than one Boolean expression in a single condition.

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

Page 13: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

And Truth Table

And &&Exp1 Exp2 Result

True True TrueTrue False FalseFalse True FalseFalse False False

Page 14: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Or Truth TableOr ||

Exp1 Exp2 Result

True True TrueTrue False TrueFalse True TrueFalse False False

Page 15: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Not Truth TableNot !

Exp Result

True FalseFalse True

Page 16: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Logical OperatorsExample Compound Statement:– if ( x >= 3 && y < 6)

Page 17: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

DeMorgan’s Law!(p || q) !p && !q!(p && q) !p || !qExample– !(x >= 3 && y < 10)Evaluates– (x < 3 || y >= 10)

Page 18: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Short Circuit EvaulationAND && - If the first operand evaluates to False, the second operand will not be evaluated.OR || - If the first operand evaluates to True, the second operand will not be evaluated.

Page 19: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Order of Operations of Logical Operators

!&&||

Page 20: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Order Of Operations

()++,--, unary!, type casting*, /, %+, -<,>,<=,>=,==,!=&&||

Page 21: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The 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 22: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

Flowchart Symbols

decision

Page 23: Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to

The RPS Flowchart