0% found this document useful (0 votes)
10 views32 pages

Lecture 2 - Expressions

The document provides an overview of expressions and operators in C programming, explaining the concept of expressions as combinations of variables and operators that compute values. It details various types of operators, including arithmetic, relational, logical, assignment, and increment/decrement operators, along with their usage and precedence rules. Additionally, it includes examples and quizzes to reinforce understanding of these concepts.

Uploaded by

leaderatelast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Lecture 2 - Expressions

The document provides an overview of expressions and operators in C programming, explaining the concept of expressions as combinations of variables and operators that compute values. It details various types of operators, including arithmetic, relational, logical, assignment, and increment/decrement operators, along with their usage and precedence rules. Additionally, it includes examples and quizzes to reinforce understanding of these concepts.

Uploaded by

leaderatelast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Expressions

CSE 4373: Computer Programming and Applications


Ajwad Abrar
Junior Lecturer, CSE
What is an expression

E = mc2
● An expression is a combination of variables and operators that

compute a value.

● E is a variable that stores the result of the expression.

● m and c are both variables that may store any value. They are places

within operators.

2
Operators

3
Operators in C
● Operators are special symbols that tell the computer to perform special
mathematical, logical or conditional operations.
● Some well known operators are: +, -, *, /

Operators in C

● Arithmetic operators
● Relational operators
We will learn about these in other lectures
● Logical operators
● Assignment operators
● Increment & decrement operators

4
Arithmetic operators

5
Arithmetic operators
As the name suggests, these operators are used for mathematical operations in
between variables.
Unary operators
● + or -
● Used to denote negative and positive numbers
Binary operators
● + (addition)
● - (subtraction)
● * (Multiplication)
● / (Division)
● % (Remainder)
6
Arithmetic operators
average = sum / count;

Operand Operator Operand

We can chain multiple operators together to perform complex calculations

average = (value_1 + values_2 + value_3) / count;

E = m * (c * c);

7
Arithmetic operators
What will be stored in rating
int rating, power = 10; // Declared 2 variables
rating = -power;

Finally, what is the output of this line


printf(“%d”, rating + power);

8
Pop Quiz

9
Arithmetic operators
● 10 + 10.5f = ?

● 10 % 3 = ?

● 1 / 2 = ?

● 1.0f / 2.0f = ?

10
Arithmetic operators
● 10 + 10.5f = 10.5 → This is a float

● 10 % 3 = 1 → Remainder operation. Output in an int

● 1 / 2 = 0 → The output should have been 0.5 but as both are int, output
gets truncated to 0

● 1.0f / 2.0f = 0.5 → This is the expected output. As both are float

11
Arithmetic operators
● What about this?

10.2f % 3.5f = ?

This will throw a compile error.


Specifically for the remainder (%) operator, both operands must be integer

12
Operator precedence

13
Operator precedence ● Unary +, -
B → Brackets (...) ● *, /, %
● Binary +, -
O → Orders x2 + y2
D → Division /
● i+j*k → i+(j*k)
M → Multiplication * ● -i*-j → (-i)*(-j)
● -i+j/k → (-i)+(j/k)
A → Addition +
Unary operator has the highest
S → Subtraction -
precedence.
14
Operator precedence
What happens when there are operators with equal precedence?

● 5-7-12 → (5-7)-12
Left associative
● 10/2*5 → (10/2)*2

● -+i → -(+i) Right associative

Messing up precedence will produce unexpected results

15
Pop Quiz

16
Operator precedence
● a + b * c / -d = ?

● 5 + 2 / 2 - 1 = ?

● -+-+-+i + +-j = ?

● -+x + y - x * y = ?

17
Assignment operators

18
Assignment Operators
Assignment operators assign some value to variables.

int i = 3, j;

j = 2 * i;

● Several assignments can be chained together

i = j = k = 0;

i = (j = (k = 0));

● Assignment is right associative

19
Assignment Operators
Be careful about type conversion in chained assignment

int i;

float f;

f = i = 33.5f;

Here, i = 33 and f = 33.0f

20
Compound assignment

21
Compound assignment
Compound assignments use old value of a variable to compute new value

int x = 5;

x += 2; // x is now 7

This is the same as computing x = x + 2;

Thus a compound assignment is doing two things at once, calculation and


assignment

22
Compound assignment

● += First add then assign


● -= First subtract then assign
● *= First multiply then assign
● /= First divide then assign
● %= First find remainder then assign

a *= m+n is not the same as a = a*m+n


It is equal to, a = a*(m+n)
23
Increment & decrement operators

24
Increment & decrement operators

The most common usage of the compound operators are to


increment or decrement a variable
i += 1; // incrementing (adding 1)
i -= 1; //decrementing (subtracting 1)
This operation is so common that C provides a special increment (++)
and decrement (--) operator
i++; // incrementing (adding 1)
i--; //decrementing (subtracting 1)
25
Increment & decrement operators

These operators can be used as a prefix (++i) or a postfix (i++) and


they have different meanings

● ++i → increment i immediately


● i++ → use the old value of i for now, but increment i later

26
Pop Quiz

27
Increment & decrement operators

What will be the output of this?


int i = 1;
printf("i = %d\n", --i);
printf("i = %d\n", i);
i = 1;
printf("i = %d\n", i--);
printf("i = %d\n", i);
28
Increment & decrement operators

What will be the output of this?


int i = 1;
printf("i = %d\n", --i); // prints "i = 0"
printf("i = %d\n", i); // prints "i = 0"
i = 1;
printf("i = %d\n", i--); // prints "i = 1"
printf("i = %d\n", i); // prints "i = 0"
29
Updated precedence table

30
Reading Tasks
● C Programming - A Modern Approach | Chapter 4

31
Thank You

32

You might also like