conditional control structures chapter 5. goals and objectives understand conditional control...
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
Conditional Control StructuresChapter 5
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.
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.
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;
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;
Relational Operators
Operator Meaning== equal< less than<= less than or equal> greater than>= greater than or equal!= not equal
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.
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.
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.
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;
}
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
Compound Boolean Expressions
More than one Boolean expression in a single condition.
Formed using the logical And (&&), logical Or (||), or logical Not (!) operators.
And Truth Table
And &&Exp1 Exp2 Result
True True TrueTrue False FalseFalse True FalseFalse False False
Or Truth TableOr ||
Exp1 Exp2 Result
True True TrueTrue False TrueFalse True TrueFalse False False
Not Truth TableNot !
Exp Result
True FalseFalse True
Logical OperatorsExample Compound Statement:– if ( x >= 3 && y < 6)
DeMorgan’s Law!(p || q) !p && !q!(p && q) !p || !qExample– !(x >= 3 && y < 10)Evaluates– (x < 3 || y >= 10)
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.
Order of Operations of Logical Operators
!&&||
Order Of Operations
()++,--, unary!, type casting*, /, %+, -<,>,<=,>=,==,!=&&||
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
Flowchart Symbols
decision
The RPS Flowchart