Chapter 04 Flashcards
1
Q
- Write an if statement that assigns 100 to x when y is equal to 0.
A
if ( y== 0)
x = 100;
2
Q
- Write nested if statements that perform the following tests: If amount1 is greater
than 10 and amount2 is less than 100, display the greater of the two.
A
if (amount1 > 10)
if (amount2 < 100)
cout «_space;(amount1 > amount2 ? amount1 : amount2);
3
Q
- Write an if statement that prints the message The number is valid if the variable
temperature is within the range -50 through 150.
A
if (temperature >=-50 && temperature «_space;“The number is valid”;
4
Q
- Assume str1 and str2 are string objects that have been initialized with different
values. Write an if/else statement that compares the two objects and displays the
one that is alphabetically greatest.
A
if (str1 > str2)
cout
5
Q
- T F A variable defined in an inner block may not have the same name as a variable
defined in the outer block.
A
false
6
Q
- T F All lines in a block should be indented one level.
A
true
7
Q
- T F When an if statement is nested in the if part of another statement, the only
time the inner if is executed is when the expression of the outer if is true.
A
true
8
Q
- T F The scope of a variable is limited to the block in which it is defined.
A
true
9
Q
- T F x != y is the same as (x > y || x < y)
A
true
10
Q
- T F x >= y is the same as (x > y && x = y)
A
false
11
Q
Assume the variables x = 5, y = 6, and z = 8. Indicate by circling the T or F whether each of
the following conditions is true or false:
54. T F x == 5 || y > 3
55. T F 7 4
56. T F 2 != y && z != 4
57. T F x >= 0 || x
A
- T
- F
- T
- T
12
Q
- The following statement should determine if count is within the range of 0 through
- What is wrong with it?
if (count >= 0 || count <= 100)
A
63. It should use && instead of || .
13
Q
- The following statement should assign 0 to z if a is less than 10, otherwise it should
assign 7 to z. What is wrong with it?
z = (a < 10) : 0 ? 7;
A
65. The \: and ? are transposed. The statement should read: z = (a < 10) ? 0 : 7;
14
Q
- Describe the difference between the if/else if statement and a series of if
statements.
A
1. In an if/else if statement, the conditions are tested until one is found to be true. The conditionally executed statement(s) are executed and the program exits the if/else if statement. In a series of if statements, all of the if statements execute and test their conditions because they are not connected.
15
Q
- In an if/else if statement, what is the purpose of a trailing else?
A
executed when all the preceding conditional expressions are false
16
Q
- What is an flag and how does it work?
A
- A flag is a Boolean variable signaling that some condition exists in the program. When the
flag is set to
false
it indicates the condition does not yet exist. When the flag is set to
true
it indicates that the condition does exist.
17
Q
- Can an if statement test expressions other than relational expressions? Explain.
A
Yes.
18
Q
- Briefly describe how the && operator works.
A
- It takes two expressions as operands and creates a single expression that is true only when
both subexpressions are true.
19
Q
- Briefly describe how the || operator works.
A
- It takes two expressions as operands and creates a single expression that is true when either of the subexpressions are true.
20
Q
- Why are the relational operators called relational?
A
- Because they test for specific relationships between items. The relationships are greater-than,
less-than, equal-to, greater-than or equal-to, less-than or equal-to, and not equal-to.
21
Q
- Why do most programmers indent the conditionally executed statements in a decision
structure?
A
because they need to execute based on the condition evaluation either executes true or false block.
22
Q
- An expression using the greater-than, less-than, greater-than-or-equal to, less-than-orequal-
to, equal, or not-equal to operator is called a(n) __________ expression.
A
relational