Problem 17
Question
(What Prints?) Assume \(i=1, j=2, k=3\) and \(m=2 .\) What does each statement
print?
a) cout <<(i == 1 ) << endl;
b) cout <<(j == 3 ) << endl;
c) cout <<(i >= 1 && j < 4 ) << endl;
d) cout <<(m <= 99 && k
Step-by-Step Solution
Verified Answer
a) 1, b) 0, c) 1, d) 0, e) 1, f) 0, g) 0, h) 1, i) 0
1Step 1: Evaluating Statement a
Check if the condition in the statement is true or false. For statement a, the condition is (i == 1). Given i=1, then i == 1 is true. In C++, true is represented by the value 1.
2Step 2: Evaluating Statement b
Check if the condition in statement b is true or false. For statement b, the condition is (j == 3). Given j=2, then j == 3 is false. In C++, false is represented by the value 0.
3Step 3: Evaluating Statement c
Check the compound condition in statement c using the logical AND operator. The condition is (i >= 1 && j < 4). Given i=1 and j=2, both i >= 1 and j < 4 are true. In C++, true && true is true (represented by the value 1).
4Step 4: Evaluating Statement d
Check the compound condition in statement d using the logical AND operator. The condition is (m <= 99 && k
5Step 5: Evaluating Statement e
Check the compound condition in statement e using the logical OR operator. The condition is (j >= i || k == m). Given i=1, j=2, and m=2, j >= i is true and k == m is false. In C++, true || false is true (represented by the value 1).
6Step 6: Evaluating Statement f
Check the compound condition in statement f using the logical OR operator. The condition is (k + m < j || 3 - j >= k). Given j=2, k=3, and m=2, k + m < j is false and 3 - j >= k is false as well. In C++, false || false is false (represented by the value 0).
7Step 7: Evaluating Statement g
Check the negation condition in statement g. The condition is (!m). Given m=2, !m is the negation of true (because any non-zero value is considered true in C++), resulting in false, which is represented by the value 0.
8Step 8: Evaluating Statement h
Check the negation condition in statement h. The conditional expression is (!(j - m)). Given j=2 and m=2, j - m is 0. The negation of false (since 0 is considered false) is true, represented by the value 1.
9Step 9: Evaluating Statement i
Check the negation condition in statement i. The conditional expression is (!(k > m)). Given k=3 and m=2, k > m is true. The negation of true is false, represented by the value 0.
Key Concepts
C++ Logical OperatorsC++ Operator PrecedenceC++ Boolean Values
C++ Logical Operators
In C++ programming, logical operators are used to form compound boolean expressions, which ultimately evaluate to true or false. These operators include AND (&&), OR (||), and NOT (!).
For instance, the AND operator (&&) returns true only if both operands are true. This can be observed in our textbook exercise where a statement like
The OR operator (||), on the other hand, returns true if at least one of the operands is true. An example from the exercise is
The NOT operator (!) inverses the boolean value of its operand. For example,
Understanding these operators and how they combine conditions is crucial for creating complex logical statements in C++. They enable programmers to ensure multiple criteria are met or to check alternatives within their code.
For instance, the AND operator (&&) returns true only if both operands are true. This can be observed in our textbook exercise where a statement like
(i >= 1 && j < 4) evaluates to true because both conditions i >= 1 and j < 4 are true.The OR operator (||), on the other hand, returns true if at least one of the operands is true. An example from the exercise is
(j >= i || k == m), which evaluates to true because the first condition, j >= i, is true, even though the second condition, k == m, is false.The NOT operator (!) inverses the boolean value of its operand. For example,
!m returns false because m in our exercise is 2, which is considered true, and negation changes it to false.Understanding these operators and how they combine conditions is crucial for creating complex logical statements in C++. They enable programmers to ensure multiple criteria are met or to check alternatives within their code.
C++ Operator Precedence
Operator precedence in C++ is the set of rules to clarify which operations are performed first in a compound expression. Just like in arithmetic, where multiplication precedes addition, C++ has a well-defined order of evaluation for different types of operators.
For logical operators, NOT (!) has the highest precedence, followed by AND (&&) and then OR (||). Parentheses can be used to change the order of evaluation, which is shown in the textbook example
Becoming familiar with the precedence of operators will help avoid logical errors in code and ensure that expressions are evaluated in the intended order. When in doubt, use parentheses to make the order explicit.
For logical operators, NOT (!) has the highest precedence, followed by AND (&&) and then OR (||). Parentheses can be used to change the order of evaluation, which is shown in the textbook example
(!(j - m)), where the subtraction is performed first, followed by the negation.Example of precedence in action:
In the statement(k + m < j || 3 - j >= k), arithmetic operations (+ and -) are performed before the comparison operations (< and >=), which in turn are evaluated before the logical OR (||).Becoming familiar with the precedence of operators will help avoid logical errors in code and ensure that expressions are evaluated in the intended order. When in doubt, use parentheses to make the order explicit.
C++ Boolean Values
Boolean values in C++ are simple yet powerful. They represent truthness and falseness, and are denoted by the keywords 'true' and 'false'. However, under the hood, C++ treats the value 0 as false and any non-zero value as true.
In the exercise provided, when a condition is checked, C++ converts the result into a boolean value. For instance, the statement
Remember, boolean values are not just restricted to 'true' and 'false' or '1' and '0'. They are fundamental to controlling the flow of the program using conditional statements like if, for, while, and more. They are the backbone of making decisions in code and thus, understanding how they work in different situations is essential for any programmer.
In the exercise provided, when a condition is checked, C++ converts the result into a boolean value. For instance, the statement
(i == 1) will output '1' because 'i' equals '1', which is a true condition in C++. Conversely, if a condition evaluates to false, the output will be '0', as seen in (j == 3), since 'j' does not equal '3'.Remember, boolean values are not just restricted to 'true' and 'false' or '1' and '0'. They are fundamental to controlling the flow of the program using conditional statements like if, for, while, and more. They are the backbone of making decisions in code and thus, understanding how they work in different situations is essential for any programmer.
Other exercises in this chapter
Problem 9
\((\text {Product of Odd Integers})\) Write a program that uses a for statement to calculate and print the product of the odd integers from 1 to 15.
View solution Problem 14
(Calculating Total Sales) A mail order house sells five different products whose retail prices are: product \(1-\$ 2.98,\) product \(2-\$ 4.50,\) product \(3-\$
View solution Problem 18
\((\text {Number Systems Table})\) Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range \(1-
View solution Problem 23
\((\text {Diamond of Asterisks})\) Write a program that prints the following diamond shape. You may use output statements that print a single asterisk ( \(^{*}\
View solution