Chapter 5 Conditionals and Loops Flashcards
What is meant by the flow of control through a program?
The flow of control through a program determines the program statements that will be executed on a given run of the program.
What type of conditions are conditionals and loops based on?
Each conditional and loop is based on a boolean condition that evaluates to either true or false.
What are the equality operators? The relational operators? The logical
operators?
The equality operators are equal (==) and not equal (!=). The relational
operators are less than (),
and greater than or equal to (>=). The logical operators are not (!), and
(&&), and or (||).
Given the following declarations, what is the value of each of the
listed boolean expressions?
int value1 = 5, value2 = 10;
boolean done = true;
a. value1 <= value2
b. (value1 + 5) >= value2
c. value1 < value2 / 2
d. value2 != value1
e. !(value1 == value2)
f. (value1 < value2) || done
g. (value1 > value2) || done
h. (value1 < value2) && !done
i. done || !done
j. ((value1 > value2) || done) && (!done || (value2 >
value1) )
Assuming the given declarations, the values are: a. true, b. true,
c. false, d. true, e. true, f. true, g. true, h. false, i. true, j. true
What is a truth table?
A truth table is a table that shows all possible results of a boolean expression, given all possible combinations of variables and conditions.
Assuming done is a boolean variable and value is an int variable, create a truth table for the expression:
(value > 0 ) || !done
The truth table is:
value > 0 done !done (value > 0) || !done true true false true true false true true false true false false false false true true
Assuming c1 and c2 are boolean variables, create a truth table for the
expression:
(c1 && !c2) || (!c1 && c2)
See book 5.7.
What output is produced by the following code fragment given the assumptions below?
if (num1 < num2) System.out.print(" red "); if ((num1 + 5) < num2) System.out.print(" white "); else System.out.print(" blue "); System.out.println(" yellow ");
a. Assuming the value of num1 is 2 and the value of num2 is 10?
b. Assuming the value of num1 is 10 and the value of num2 is 2?
c. Assuming the value of num1 is 2 and the value of num2 is 2?
Based on the given assumptions, the output would be:
a. red white yellow
b. blue yellow
c. blue yellow
How do block statements help us in the construction of conditionals?
A block statement groups several statements together. We use them to define the body of an if statement or loop when we want to do multiple things based on the boolean condition.
What is a nested if statement?
A nested if occurs when the statement inside an if or else clause is an if statement. A nested if lets the programmer make a series of decisions. Similarly, a nested loop is a loop within a loop.
For each assumption, what output is produced by the following code
fragment?
if (num1 >= num2) { System.out.print(" red "); System.out.print(" orange "); } if ((num1 + 5) >= num2) System.out.print(" white ");
else if ((num1 + 10) >= num2) { System.out.print(" black "); System.out.print(" blue "); } else System.out.print(" yellow "); System.out.println(" green ");
a. Assuming the value of num1 is 5 and the value of num2 is 4?
b. Assuming the value of num1 is 5 and the value of num2 is 12?
c. Assuming the value of num1 is 5 and the value of num2 is 27?
Based on the given assumptions, the output would be:
a. red orange white yellow
b. black blue green
c. yellow green
Write an expression that will print a message based on the value of
the int variable named temperature. If temperature is equal to or less
than 50, it prints “It is cool.” on one line and “Dress warmly.” on the
next. If temperature is greater than 80, it prints “It is warm.” on one
line and “Dress coolly.” on the next. If temperature is in between 50
and 80, it prints “It is pleasant.” on one line and “Dress pleasantly.”
on the next.
if (temperature <= 50) { System.out.println("It is cool."); System.out.println("Dress warmly."); } else if (temperature > 80) { System.out.println("It is warm."); System.out.println("Dress cooly."); } else { System.out.println("It is pleasant."); System.out.println("Dress pleasantly."); }
Why must we be careful when comparing floating point values for
equality?
Because they are stored internally as binary numbers, comparing floating point values for exact equality will be true only if they are the same bit-by-bit. It’s better to use a reasonable tolerance value and consider the difference between the two values
How do we compare strings for equality?
We compare strings for equality using the equals method of the String class, which returns a boolean result. The compareTo method of the String class can also be used to compare strings. It returns a positive, 0, or negative integer result depending on the relationship between the two strings.
Write an equals method for the Die class of Section 4.2. The method should return true if the Die object it is invoked on has the same facevalue as the Die object passed as a parameter, otherwise it should return false
//------------------------------------------------------ // Returns true if this Die equals die, otherwise // returns false. //------------------------------------------------------ public boolean equals(Die die) { return (this.faceValue == die.faceValue); }