Conditional Control Structures Chapter 5 Review

advertisement
Conditional Control Structures
Chapter 5 Review
The If Statement
• The if statement is a conditional control
structure, also called a decision structure,
which executes a set of statements when a
condition is true. It takes the form:
if (<condition>) {
<statements>;
}
Relational operators
• Relational operators can be used to form
Boolean expressions. They are:
Operator
==
Meaning
Equal
<
<=
>
>=
less than
less than or equal
more than
more than or equal
!=
not equal
The If Statement example
• In this example, the user’s guess is compared
to the constant SECRET_NUM. If they are the
same, “You guessed it” is outputted.
if (guess==SECRET_NUM) {
System.out.println(“You guessed it”);
}
The If-Else Statement
• The if statement can include an optional else
clause that is executed when the if condition
is false.
if (<condition>) {
<statements>;
} else {
<statements>;
}
The If-Else Statement example
if (guess==SECRET_NUM) {
System.out.println(“You guessed it”);
} else {
System.out.println(“Try again”);
}
Nested If Statements
• Statements placed within the same type of
statements are called nested.
if (guess==SECRET_NUM) {
System.out.println(“You guessed it”);
} else {
if (guess<SECRET_NUM) {
System.out.println(“Too low”);
} else {
System.out.println(“Too high”);
}
}
The If-Else if Statement
• The if-else if statement is used to decide among 3
or more actions.
if (<condition>) {
<statements>;
} else if (<condition>) {
<statements>;
} else {
<statements>;
}
The If-Else if Statement example
• The if-else if statement is used to decide among 3
or more actions.
if (guess==SECRET_NUM) {
System.out.println(“You guessed it”);
} else if (guess<SECRET_NUM) {
System.out.println(“Too low”);
} else {
System.out.println(“Too high”);
}
The switch Statement
• The switch statement is a conditional control that
uses the result of an expression to determine
which statements to execute.
switch (<integer expression>) {
case x:
<statements>;
break;
default:
<statements>;
break;
}
The switch Statement cont’d
• break; The break statement is necessary to
move program control to the next statement
after the switch statement.
• default: The default code is optional and is
executed when non of the previous cases are
met.
The switch Statement example
switch (score) {
case 0: System.out.println(“Too bad”); break;
case 0: System.out.println(“Good”); break;
case 0: System.out.println(“Great”); break;
}
Generating Random Numbers
• Java uses the Linear Congruential Method to
generate a sequence of random numbers.
• Because the sequence eventually repeats,
random number in a computer application are
really pseudorandom.
• Java includes the Random class in the java.util
package for generating random numbers.
Random Numbers in a range
• To generate a random number in a range the
following formula is used:
(high – low + 1) * Math.random() + low
• Note that high represents the largest number
& low the lowest number in the range. To
randomly generate a number between 1-10:
(10 – 1 + 1) * Math.random() + 1
Compound Boolean Expressions
• Two or more Boolean expressions can be
joined with logical operators to form a
compound Boolean expression.
• Logical operators include && (logical And), ||
(logical Or), and ! (logical Not).
• && evaluates true only when both operands
are true
• || evaluates true when either operand is true
Truth Tables
• How a compound Boolean expression
evaluates with && and || operators can be
shown with a truth table
AND - &&
OR - ||
Exp1
Exp2
Results
Exp1
Exp2
Results
True
True
True
True
True
True
True
False
False
True
False
True
False
True
False
False
True
True
False
False
False
False
False
False
Logical Operator examples
if (guess>=1 && guess<=10) {
System.out.println(“Valid entry”);
}
if (guess<=0 || guess>=10) {
System.out.println(“Invalid entry”);
}
The Math Class
• The Math class, part of the java.lang package,
contains many useful methods for performing
math functions.
• Calling a Math method requires including the
class name. For example, Math.abs(-3)
Useful Methods of the Math Class
Method
Description
abs()
Returns the absolute value of the argument.
pow()
Returns the value of the first argument raised to the
power of the second argument.
sqrt()
Returns the square root of the argument.
Download