✍️ Practice (Logic)
1. Substitution
Let x = true, y = false, z = false. What does the following expression evaluate to?
(x || y) && z
👀Hint
Try substituting x, y, and z then follow your new order of operations rules
💡Solution
(x || y) && z
(true || false) && false // substitute the values
true && false // 1) parentheses first
false // 2) AND
2. Substitution (Again)
Let x = true, y = false, z = false. What does the following expression evaluate to?
! x && y || z
👀Hint
Try adding parenthesis based on which operators takes precedence over the others.
💡Solution
Highest first: !, then &&, then ||. Parentheses override all of them.
!x && y || z
((!x) && y) || z // implicit grouping by precedence
((!true) && false) || false // substitute the values
(false && false) || false // 1) NOT
false || false // 2) AND
false // 3) OR
3. Substitution (Yet Again)
Let x = true, y = false, z = false. What does the following expression evaluate to?
! (x && y || z)
💡Solution
!(x && y || z)
!((x && y) || z) // implicit grouping by precedence
!((true && false) || false) // substitute the values
!(false || false) // 1) AND
!(false) // 2) OR
true // 3) NOT
4. Mixed Types
What does false || 1 / (int) 3.7 < 3.5 evaluate to?
👀Hint
Division come before comparison, and comparison comes before ||
💡Solution
false || 1 / (int) 3.7 < 3.5
false || 1 / 3 < 3.5 // 1) cast
false || 0 < 3.5 // 2) divide
false || true // 3) compare
true // 4) OR
5. Mixed Types (Hard)
Let int a = 7, double b = 2.5, bool flag = false. What does the following expression evaluate to?
!flag && (int) b * 3 > a / 2 + 4 || (double) a / 2 >= b + 1
👀Hint
Give it your best shot lol
💡Solution
Highest first: casts and !, then * /, then + -, then > >=, then &&, then ||.
!flag && (int) b * 3 > a / 2 + 4 || (double) a / 2 >= b + 1
!false && (int) 2.5 * 3 > 7 / 2 + 4 || (double) 7 / 2 >= 2.5 + 1 // substitute the values
true && 2 * 3 > 7 / 2 + 4 || 7.0 / 2 >= 2.5 + 1 // 1) casts and NOT
true && 6 > 3 + 4 || 3.5 >= 2.5 + 1 // 2) * and / (left to right)
true && 6 > 7 || 3.5 >= 3.5 // 3) +
true && false || true // 4) comparisons
false || true // 5) AND
true // 6) OR