Download presentation
Presentation is loading. Please wait.
Published byCharla Marshall Modified over 8 years ago
1
Conditional Control Structures Chapter 5 Review
2
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 ( ) { ; }
3
Relational operators Relational operators can be used to form Boolean expressions. They are: OperatorMeaning ==Equal <less than <=less than or equal >more than >=more than or equal !=not equal
4
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”); }
5
The If-Else Statement The if statement can include an optional else clause that is executed when the if condition is false. if ( ) { ; } else { ; }
6
The If-Else Statement example if (guess==SECRET_NUM) { System.out.println(“You guessed it”); } else { System.out.println(“Try again”); }
7
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”); }
8
The If-Else if Statement The if-else if statement is used to decide among 3 or more actions. if ( ) { ; } else if ( ) { ; } else { ; }
9
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”); }
10
The switch Statement The switch statement is a conditional control that uses the result of an expression to determine which statements to execute. switch ( ) { case x: ; break; default: ; break; }
11
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.
12
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; }
13
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.
14
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
15
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
16
Truth Tables How a compound Boolean expression evaluates with && and || operators can be shown with a truth table AND - && OR - || Exp1Exp2Results True False TrueFalse Exp1Exp2Results True FalseTrue FalseTrue False
17
Logical Operator examples if (guess>=1 && guess<=10) { System.out.println(“Valid entry”); } if (guess =10) { System.out.println(“Invalid entry”); }
18
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)
19
Useful Methods of the Math Class MethodDescription 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.