click below
click below
Normal Size Small Size show me how
Chapter 5
Making Decisions
Term | Definition |
---|---|
Pseudocode | A tool that helps programmers plan a program's logic by writing plain English statements |
Flowchart | Is similar to pseudocode, but the steps are written in diagram form as a series of shapes connected by arrows |
Sequence Structure | A logical structure in which one step follows another unconditionally |
Decision Structure | A structure that involves choosing between alternative courses of action based on some value within a program |
if Statement | The simplest statement you can use to make a decision. Ex: if(quizScore == 10)... System.out.println("The score is perfect"); |
Equivalency Operator | The double equal sign (==) is used to determined equality |
Single-Alternative if | When the program only performs an action, or not, based on one alternative |
Dual-Alternative if | The decision structure you use when you need to take one or or the other of two possible courses of action |
if...else Statement | Provides the mechanism to perform one action when a Boolean expression evaluates to true and to perform a different action when a Boolean expression evaluates as false |
Nested if Statements | Statements in which an if structure is contained inside another if structure. Are particularly useful when two conditions must be met before some action it taken |
Logical AND Operator | For an alternative to some nested if statement, you can use the logical AND operator between two Boolean expressions to determine whether both are true. In Java, the AND operator is written as two ampersands(&&) |
Logical OR Operator | When you want some action to occur even if only one of two conditions is true, you can use nested if statements, or you can use the logical OR operator, which is written as ||. |
Short-Circuit Evaluation | The expression on each side of the && and the || operators are evaluated only as far as necessary to determine whether the entire expression is true or false. |
Range Check | A series of statements that determine to which of several consecutive series of values another value falls |
1. switch Statement | Is useful when you need to test a single variable against a series of exact integer(including int, byte, and short types), character, or string values. The switch statement uses four keywords... |
2. switch Statement | 1. "switch" starts the structure and is followed immediately by a test expression enclosed in parentheses.. 2. "case" is followed by one of the possible values for the test expression and a colon.. |
3. switch Statement | 3. "break" optionally terminates a switch statement at the end of each case.. 4. "default" optionally is used prior to any action that should occur if the test variable does not match any case. |
4. switch Statement | switch(year).. {.. case 1:.. System.out.println("Freshman");.. break;.. case 2:.. System.out.println("Sophomore");.. break;.. case 3:. System.out.println("Junior");.. break;.. default:.. System.out.println("Invalid year");.. } |
1. Conditional Operator | Requires three expressions separated with a question mark and a colon and is used as an abbreviated version of the if...else structure. The syntax is:.. testExpression? trueResult : falseResult;... |
2. Conditional Operator | The first expression, testExpression, is a Boolean expression that is evaluated as true or false. If its is true, the entire conditional expression takes on the value of the expression following the question mark(trueResult). |
3. Conditional Operator | If the value of the testExpression is false, the entire expression takes on the vale of falseResult. Ex: smallerNum = (a < b) ? a : b; |
Ternary Operator | An operator that needs three operands. A conditional operator is ternary |
NOT Operator | Written as the exclamation point(!), and used to negate the result of any Boolean expression |
Operators with Highest Precedence | Operator[Logical NOT]..Symbol[!] |
1. Operators with Intermediate Precedence | 1. Operators[Multiplication, division, modulus]..Symbols[*, /, %]... 2. Operators[Addition, Subtraction]..Symbols[+, -]... 3. Operators[Relational]..Symbols[>, <, >=, <=]... 4. Operators[Equality]..Symbols[==, !=]... |
2. Operators with Intermediate Precedence | 5. Operators[Logical AND]..Symbols[&&]... 6. Operators[Logical OR]..Symbols[||]... 7. Operators[Conditional]..Symbols[?:] |
Operators with Lowest Precedence | Operator[Assignment]..Symbol[=] |