Decision Structures Flashcards
Relational operator
An operator that compares, or tests the relationship, two values, yielding a Boolean result. Java has six relational operators:
- > greater than
- >= greater than or equal
- < less than
- <= less than or equal
- == equal
- != not equal
Boolean operator
An operator that can be applied to Boolean values. Java has three Boolean operators: &&, ||, and !.
Logical operators
- && and
- || or
- ! not equal
De Morgan’s Law
A law about logical operations that describes how to negate expressions formed with and and or operations in order to simplify Boolean expressions.
It states that:
! (A && B) is the same as !A || !B
! (A || B) is the same as !A && !B
Conditional
A conditional statement of code is a statement that may or may not be executed depending upon whether the controlling condition evaluates to true or false. In most programming languages, these can be recognized with the keyword if.
Combining boolean variables with conditional statements (if x, do y) allows you to make your programs do different things under different situations. This gets our programs closer to modeling real world problems.
For example, writing a method called checkFull to see if a gas tank was full or not. We set the method to return a boolean, true or false.
If statement
Allows a program to implement decisions. That is, one set of instructions is executed if the condition is satisfied. Otherwise, another set of instructions is carried out. For example:
int actualFloor;
if (floor > 13)
{
actualFloor = floor - 1;
}
else
{
actualFloor = floor;
}
*Note that if there is no second condition then the else statement can be omitted entirely.
int actualFloor = floor;
if (floor > 13)
{
acutalFloor–;
}
Switch Statement
A “multiple alternative decision structure” that allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute. It can be used as an alternative to an if-else-if statement that compares the same variable or expression to several different values. It isn’t much of a shortcut, but has one advantage - it’s obvious that all branches test the same value.
For example:
switch (digit)
{
case 1: digitName = “one”; break;
case 2: digitName = “two”; break;
case 3: digitName = “three”; break;
case 4: digitName = “four”; break;
case 5: digitName = “five”; break;
}
* Note that you can test a variable or expression in the switch statement.
What does it mean to “fall through” code?
(Specifically when executing a switch statement)
Without a break statement, the program will continue running. However, this can be helpful in some cases, such as the following example in which the programmer wants to return a value for a grade regardless of whether or not the user enters it in upper or lower case:
switch(foodGrade)
{
case ‘a’ :
case ‘A’ : system.out.println(“$30”); break;
case ‘b’ :
case ‘B’ : system.out.println(“$25”); break;
}
Lexicographic order
Ordering strings in the same order as in a dictionary, by skipping all matching characters and comparing the first non-matching characters of both strings. For example, “orbit” comes before “orchid” in lexicographic ordering. Note that in Java, unlike a dictionary, the ordering is case sensitive: Z comes before a.
Null reference
A reference that does not refer to any object.
How do you program multiple alternatives into a decision?
(As opposed to a two-way branch thats implemented with an if/ else statement.)
You add “else if” statements for each additional scenario.
For example:
if (richter >= 8.0)
damage = “everything is destroyed”;
else if (richter >= 6.0)
damage = “somethings are destroyed”;
else
damage = “everything is okay”;
How should multiple alternative tests be ordered?
From most specific to most general. This is important since once the first condition is met, or the first test passes, none of the rest will be executed.
What does this evaluate to : ( i < 2 ) && ( 2 < 2 ) ?
False. ( 2 < 2 ) is False, which makes the entire statement false.
Short-circuit evaluation
Evaluating only part of an expression if the remainder cannot change the result.