Expressions ๐Ÿ”ข#

Expressions are one ore more operators grouped together.

Ex.

5 + 20 / 5
3 * 4 - 1

When C hits an expression, it will attempt to โ€œsolveโ€, or evaluate the expression to simplify it down to just one value. This means you can use expressions whereevere you can use a single value!

Ex.

int some_integer = 350 + 2 / 29;
int other_integer = some_integer;

printf("%d is my number!", some_integer);
printf("%d is an even number!", some_integer * 2);

Expressions can also use parenthesis to tell C which operators should be evaluated first.

Ex.

(5 + 10) * 2
4 / ((2 - 20) * 10)

Order of Operations#

Cโ€™s math operators follow the order of operations from math. This means the order operators are evaluated is

  1. () Expressions in Parentehsis

  2. *, /, % Mulitplication, Division, and Modulo

  3. +, -, Addition and Subtraction

EX.

5 - 3 * 3 % 2     = 5 - 9 % 2   = 5 - 1   = 4
(5 - 3) * 3 % 2   = 2 * 3 % 2   = 6 % 2   = 0

Tasks ๐ŸŽฏ#

What does the expression 5 - 2 * 3 + 1 evaluate to?

Solution โœ…

5 - 2 * 3 + 1ย ย ย  = 5 - 6 + 1ย ย  = -1 + 1ย ย  = 0

What does the modulo operator do?

Solution โœ…

The modulo operator gets the remainder from dividng the first number by the second number

What does the experssion 3 / 2 + 2 evaluate to?

Solution โœ…

3 / 2 + 2ย ย  = 1 + 2ย ย  = 3 due to operatorsinteger_division.

What does the experssion (5 + 2) % 3 / 2

Solution โœ…

(5 + 2) % 3 / 2ย ย  = 7 % 3 / 2ย ย  = 1 / 2ย ย  = 0