Home

Expressions Practice


Chart of C order of operations: here.
Explanations are at the bottom of this page.

Order of Operations:
1: Unary (Increment, Decrement, Negate)
2: Parenthesis
3: Multiplication, Division, Modulo
4: Addition, Subtraction
5: Relational Operators
6: Logical Operators

Expressions:

Assume that:
- x equals 3
- y equals 4
- u equals true
- v equals false
for all of these expressions. Remember that true and 1 are equivalent, and likewise false and 0 are equivalent.

A1) 10 = 5 + 2 * 4 - 3
A2) 14 = (5 + 2) * 6 / 3
A3) 23 = 10 / 2 + 12 / 2 * 3
A4) -1 = 4 * 2 + (6 + 3 * (7 - 4)) - 3 * 8
A5) -6 = 6 + 9 * (2 + 4) - (47 + 19)

B1)  1 = x - 3 < y + 4
B2)  0 = (x + 7) / 5 + 4 > y * (7 + x)
B3)  5 = (x + (2 * y - x) * y <= y * 6 - 1) * 5
B4)  1 = x + y + (x * y) >= 7 + x * (5 - 2) / (2 * y)
B5)  6 = (x + 1 == y) * (4 + 5 / (y + 1)) - x + y

C1)  0 = u AND v == u OR v
C2)  1 = v == (NOT u) OR v
C3)  1 = (u AND x < y) OR (y > x * 3)
C4) 10 = 2 * y - (1 - x) * (u OR v AND u)
C5) 13 = ((u != v) AND u) + x * y

Bonus: 0 = x++ != y OR y-- != x

Explanations:

General advice: the first thing I do when I see an expression is format it to make it more readable. For example, if I see multiplication or division, such as 5 * 8, I'll squish it together: 5*8. If I see a logical or relational operator, I'll put the two operands in parenthesis to make them really clear. If I see a negative before a parenthesis, I'll distribute it and turn it into a positive. Little things like that can save you a lot of confusion when programming.

A1: 5 + 2 * 4 - 3
Firstly, let's group the multiplication: 5 + 2*4 - 3. Now, evaluate the multiplication first: 5 + 8 - 3. Then, evaluate everything from left to right: 13 - 3 = 10.

A2: (5 + 2) * 6 / 3
Evaluate the parenthesis: 7 * 6 / 3. Next, evaluate from left to right: 42 / 3 = 14.

A3: 10 / 2 + 12 / 2 * 3
First, group the multiplication/division: 10/2 + 12/2*3. Now we'll evaluate those clumps from left to right: 5 + 18. Finally, add to get 23.

A4: 4 * 2 + (6 + 3 * (7 - 4)) - 3 * 8
Evaluate the innermost parenthesis and group the multiplication: 4*2 + (6 + 3*3) - 3*8. Now, evaluate the multiplication: 8 + (6 + 9) - 24. Lastly, evaluate the parenthesis and addition/subtraction: 8 + 15 - 24 = 23 - 24 = -1.

A5: 6 + 9 * (2 + 4) - (47 + 19)
First, evaluate the parenthesis and group multiplication: 6 + 9*6 - 66. Now, multiply and then evaluate from left to right: 6 + 54 - 66 = 60 - 66 = -6.

B1: x - 3 < y + 4
Firstly, evaluate x - 3 and y + 4 (you can put parenthesis around them if it helps you visualize it better): (x - 3) < (y + 4). Because x equals 3 and y equals 4, we end up with 0 < 8, which is True, so the result is True or 1.

B2: (x + 7) / 5 + 4 > y * (7 + x)
Evaluate parenthesis and group multiplication/division: (10)/5 + 4 > y*(10). Next, evaluate the multiplication/division: 2 + 4 > 40. Clearly 6 is not greater than 40, so the result is False or 0.

B3: (x + (2 * y - x) * y <= y * 6 - 1) * 5
Group multiplication: (x + (2*y - x)*y <= y*6 - 1) * 5. Evaluate it: (x + (8 - x)*y <= 24 - 1) * 5. Evaluate everything else: (3 + (8 - 3)*4 <= 23) * 5. Now, we have (23 <= 23) * 5, and the relational <= operator evaluates to True, so we have 1 * 5 = 5.

B4: x + y + (x * y) >= 7 + x * (5 - 2) / (2 * y)
Substitute in your variables and group your multiplication: 3 + 4 + (3*4) >= 7 + 3*(5 - 2) / (2*4). Evaluate parenthesis: 3 + 4 + 12 >= 7 + 3*3 / 8. Go left to right: 19 >= 7 + 9/8. Clearly the answer is True or 1.

B5: (x + 1 == y) * (4 + 5 / (y + 1)) - x + y
Substitute and evaluate the parenthesis first: (3 + 1 == 4) * (4 + 5/5) - 3 + 4. 3 + 1 obviously equals True, so that mini expression evaluates to 1: 1 * (4 + 1) + 1. Finish evaluation: 5 + 1 = 6.

C1: u AND v == u OR v
This one is tricky. Relational operators come before logical operators, so it really looks like this: u AND (v == u) OR v. Substituting in: True AND (False == True) OR False --> True AND False OR False. Last evaluation: False OR False --> False.

C2: v == (NOT u) OR v
I think you get the idea now so I'm going to leave off the commentary and just put the steps.

False == False OR False
False == False
True

C3: (u AND x < y) OR (y > x * 3)

(True AND 3 < 4) OR (4 > 3*3)
(True AND True) OR (4 > 9)
True OR False
True

C4: 2 * y - (1 - x) * (u OR v AND u)

2*4 - (1 - 3)*(True OR False AND True)
8 - -2*(True AND True)
8 + 2*1
10

C5: ((u != v) AND u) + x * y

((True != False) AND True) + 3*4
(True AND True) + 12
1 + 12
13

Bonus: x++ != y OR y-- != x

x++ != y OR y-- != x
3 + 1 != 4 OR 4 - 1 != 3
False OR False
False


Home    Back to Top