Chapter 04 Flashcards
- Write an if statement that assigns 100 to x when y is equal to 0.
if ( y== 0)
x = 100;
- 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.
if (amount1 > 10)
if (amount2 < 100)
cout «_space;(amount1 > amount2 ? amount1 : amount2);
- Write an if statement that prints the message The number is valid if the variable
temperature is within the range -50 through 150.
if (temperature >=-50 && temperature «_space;“The number is valid”;
- 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.
if (str1 > str2)
cout
- T F A variable defined in an inner block may not have the same name as a variable
defined in the outer block.
false
- T F All lines in a block should be indented one level.
true
- 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.
true
- T F The scope of a variable is limited to the block in which it is defined.
true
- T F x != y is the same as (x > y || x < y)
true
- T F x >= y is the same as (x > y && x = y)
false
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
- T
- F
- T
- T
- The following statement should determine if count is within the range of 0 through
- What is wrong with it?
if (count >= 0 || count <= 100)
63. It should use && instead of || .
- 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;
65. The \: and ? are transposed. The statement should read: z = (a < 10) ? 0 : 7;
- Describe the difference between the if/else if statement and a series of if
statements.
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.
- In an if/else if statement, what is the purpose of a trailing else?
executed when all the preceding conditional expressions are false
- What is an flag and how does it work?
- 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.
- Can an if statement test expressions other than relational expressions? Explain.
Yes.
- Briefly describe how the && operator works.
- It takes two expressions as operands and creates a single expression that is true only when
both subexpressions are true.
- Briefly describe how the || operator works.
- It takes two expressions as operands and creates a single expression that is true when either of the subexpressions are true.
- Why are the relational operators called relational?
- 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.
- Why do most programmers indent the conditionally executed statements in a decision
structure?
because they need to execute based on the condition evaluation either executes true or false block.
- 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.
relational
- A relational expression is either __________ or __________.
true or false
- The value of a relational expression is 0 if the expression is __________ or 1 if the
expression is __________.
false true
- The if statement regards an expression with the value 0 as __________.
false
- The if statement regards an expression with a nonzero value as __________.
true
- For an if statement to conditionally execute a group of statements, the statements
must be enclosed in a set of __________.
braces
- In an if/else statement, the if part executes its statement or block if the expression
is __________, and the else part executes its statement or block if the expression is
__________.
true/false
- The trailing else in an if/else if statement has a similar purpose as the
__________ section of a switch statement.
default
- The if/else if statement is actually a form of the __________ if statement.
nested
- If the sub-expression on the left of the __________ logical operator is false, the right
sub-expression is not checked.
&&
- If the sub-expression on the left of the __________ logical operator is true, the right
sub-expression is not checked.
||
- The __________ logical operator has higher precedence than the other logical operators.
!
- The logical operators have __________ associativity.
left to right
- The __________ logical operator works best when testing a number to determine if it
is within a range.
&&
- The __________ logical operator works best when testing a number to determine if it
is outside a range.
||
- A variable with __________ scope is only visible when the program is executing in the
block containing the variable’s definition.
local
- You use the __________ operator to determine whether one string object is greater
then another string object.
>
- An expression using the conditional operator is called a(n) __________ expression.
conditional
- The expression that is tested by a switch statement must have a(n) __________ value.
integer
- The expression following a case statement must be a(n) __________ __________.
integer literal or constant
- A program will fall through a case section if it is missing the __________ statement.
break
- What value will be stored in the variable t after each of the following statements executes?
A) t = (12 > 1);__________
B) t = (2
1
0
0
1
- Write an if/else statement that assigns 0 to x when y is equal to 10. Otherwise it
should assign 1 to x.
if (y==0)
x=0;
else
x=1;
- Write an if statement that sets the variable hours to 10 when the flag variable
minimum is set.
if (minimum)
hours = 10
- Write an if statement that prints the message The number is valid if the variable
grade is within the range 0 through 100.
if (grade >= 0 && grade «_space;“The number is valid.”;
- Write an if statement that prints the message The number is not valid if the variable
hours is outside the range 0 through 80.
if (hours < 0 || hours >80 )
cout «_space;“The number is not valid.”;
- Convert the following if/else if statement into a switch statement:
if (choice == 1)
{
cout
switch (choice)
{
case 1: cout
- T F The = operator and the == operator perform the same operation when used in
a Boolean expression.
F
- T F A conditionally executed statement should be indented one level from the if
statement.
T
- T F It s safe to assume that all uninitialized variables automatically start with 0 as
their value.
F, un initialized variable are having garbage values in it.
- T F When an if statement is nested in the else part of another statement, as in
an if/else if, the only time the inner if is executed is when the expression
of the outer if is true.
F
- T F You can use the relational operators to compare string objects.
T
- T F y < x is the same as x >= y
F, x is greater than y but may not equal to y
- The following statement should determine if x is not greater than 20. What is wrong
with it?
if (!x > 20)
if (!(x>20))
- The following statement should determine if count is outside the range of 0 through
- What is wrong with it?
if (count < 0 && count > 100)
||