CH4

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

54. T F x == 5 || y > 3 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:

True

56. T F 2 != y && z != 4 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:

True

57. T F x >= 0 || x <= y 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:

True

25. You use the __________ operator to determine whether one string object is greater then another string object.

. >

3. What is a 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.

24. A variable with __________ scope is only visible when the program is executing in the block containing the variable's definition.

block

14. For an if statement to conditionally execute a group of statements, the statements must be enclosed in a set of __________.

braces {}

20. The __________ logical operator has higher precedence than the other logical operators.

!

10. A relational expression is either __________ or __________.

true, false

7. 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.

18. If the sub-expression on the left of the __________ logical operator is false, the right sub-expression is not checked.

&&

12. The if statement regards an expression with the value 0 as __________.

False

42. T F The = operator and the == operator perform the same operation when used in a Boolean expression.

False

43. T F A variable defined in an inner block may not have the same name as a variable defined in the outer block.

False

46. T F It's safe to assume that all uninitialized variables automatically start with 0 as their value.

False

62. The following statement should determine if x is not greater than 20. What is wrong with it? if (!x > 20)

It should read if (!(x > 20))

64. The following statement should determine if count is outside the range of 0 through 100. What is wrong with it? if (count < 0 && count > 100)

It should read if (count < 0 || count > 100)

63. The following statement should determine if count is within the range of 0 through 100. What is wrong with it? if (count >= 0 || count <= 100)

It should use && instead of ||.

6. 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.

65. 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;

The : and ? are transposed. The statement should read: z = (a < 10) ? 0 : 7;

4. Can an if statement test expressions other than relational expressions? Explain.

Yes. The if statement can test any value that yields a Boolean value (true or false) or a numeric value. When testing a numeric expression, a nonzero numeric value is considered true, and the value 0 is considered false.

11. The value of a relational expression is 0 if the expression is __________ or 1 if the expression is __________.

. False, True

5. 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.

13. The if statement regards an expression with a nonzero value as __________.

. True

30. What value will be stored in the variable t after each of the following statements executes? A) t = (12 > 1); __________ B) t = (2 < 0); __________ C) t = (5 == (3 * 2)); __________ D) t = (5 == 5); __________

A) 1 B) 0 C) 0 D) 1

59. // This program divides a user-supplied number by another // user-supplied number. It checks for division by zero. #include <iostream> using namespace std; int main() { double num1, num2, quotient; cout << "Enter a number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; if (num2 == 0) cout << "Division by zero is not possible.\n"; cout << "Please run the program again "; cout << "and enter a number besides zero.\n"; else quotient = num1 / num2; cout << "The quotient of " << num1 << cout << " divided by " << num2 << " is "; cout << quotient << endl; return 0; }

The conditionally executed blocks in the if/else construct should be enclosed in braces. The following statement: cout << "The quotient of " << num1 << should read: cout << "The quotient of " << num1;

2. In an if/else if statement, what is the purpose of a trailing else ?

The trailing else provides code that is executed when none of the conditions in the if/else if statement are true.

44. T F A conditionally executed statement should be indented one level from the if statement.

True

45. T F All lines in a block should be indented one level.

True

47. 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

48. 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.

True

28. The expression following a case statement must be a(n) __________ __________.

integer constant

49. T F The scope of a variable is limited to the block in which it is defined.

True

50. T F You can use the relational operators to compare string objects.

True

51. T F x != y is the same as (x > y || x < y)

True

22. The __________ logical operator works best when testing a number to determine if it is within a range.

&&

1. Describe the difference between the if/else if statement and a series of if statements.

. 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. 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

29. A program will "fall through" a case section if it is missing the __________ statement.

. break

27. The expression that is tested by a switch statement must have a(n) __________ value.

. integer

21. The logical operators have __________ associativity.

. left-to-right

17. The if/else if statement is actually a form of the __________ if statement.

. nested

9. An expression using the greater-than, less-than, greater-than-or-equal to, less-than-or equal-to, equal, or not-equal to operator is called a(n) __________ expression.

. relational

19. If the sub-expression on the left of the __________ logical operator is true, the right sub-expression is not checked.

. ||

23. The __________ logical operator works best when testing a number to determine if itis outside a range.

. ||

61. // This program uses a switch-case statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. #include <iostream> using namespace std; int main() { double testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; switch (testScore) { case (testScore < 60.0): cout << "Your grade is F.\n"; break; case (testScore < 70.0): cout << "Your grade is D.\n"; break; case (testScore < 80.0): cout << "Your grade is C.\n"; break; case (testScore < 90.0): cout << "Your grade is B.\n"; break; case (testScore <= 100.0): cout << "Your grade is A.\n"; break; default: cout << "That score isn't valid\n"; return 0; }

A switch statement cannot be used to test relational expressions. An if/else if statement should be used instead.

58. // This program averages 3 test scores. // It uses the variable perfectScore as a flag. include <iostream> using namespace std; int main() { cout << "Enter your 3 test scores and I will "; << "average them:"; int score1, score2, score3, cin >> score1 >> score2 >> score3; double average; average = (score1 + score2 + score3) / 3.0; if (average = 100); perfectScore = true; // Set the flag variable cout << "Your average is " << average << endl; bool perfectScore; if (perfectScore); { cout << "Congratulations!\n"; cout << "That's a perfect score.\n"; cout << "You deserve a pat on the back!\n"; return 0; }

// This program averages 3 test scores. // It uses the variable perfectScore as a flag. #include <iostream> using namespace std; int main() { int score1, score2, score3; double average; cout << "Enter your 3 test scores and I will " [delete;] << "average them:"; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; cout << "Your average is " << average << endl; if (average == 100) [delete ;] { bool perfectScore; perfectScore = true; // Set the flag variable if (perfectScore) [delete ;] { cout << "Congratulations!\n"; cout << "That's a perfect score.\n"; cout << "You deserve a pat on the back!\n"; } } return 0; }

60. // This program uses an if/else if statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. #include <iostream> using namespace std; int main() { int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else cout << "That is not a valid score.\n"; else if (testScore <= 100) cout << "Your grade is A.\n"; return 0; }

// This program uses an if/else if statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. #include <iostream> using namespace std; int main() { int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) { cout << "Your grade is F.\n"; } else if (testScore < 70) { cout << "Your grade is D.\n"; } else if (testScore < 80) { cout << "Your grade is C.\n"; } else if (testScore < 90) { cout << "Your grade is B.\n"; } else if (testScore <= 100) { cout << "Your grade is A.\n"; } else { cout << "That is not a valid score.\n"; } return 0; }

52. T F y < x is the same as x >= y

False

53. T F x >= y is the same as (x > y && x = y)

False

55. T F 7 <= x && z > 4 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:

False

8. Why do most programmers indent the conditionally executed statements in a decision structure? Fill-in-the-Blank

Indentation and spacing are for the human readers of a program, not the compiler. By indenting the conditionally executed statement you are causing it to stand out visually. This is so you can tell at a glance what part of the program the if statement executes.

26. An expression using the conditional operator is called a(n) __________ expression.

conditional

16. The trailing else in an if/else if statement has a similar purpose as the __________ section of a switch statement.

default


Set pelajaran terkait

chapter 7a constraint and capacity

View Set

CPTR.5772.61.Info Security Tech EXAM1

View Set

Wk 1 - Practice: Week 1 Knowledge Check

View Set

opma chap 4 ls, opma ch6 l.s., ch8 learnsmart

View Set