Arithmetic Expressions in C
16
17
18
What is an arithmetic expression?
An arithmetic expression is a combination of:
Constants
Variables
Arithmetic operators
that evaluates to a numeric value.
19
Example:
a + b * c
This is not evaluated left to right blindly. C follows rules.
Arithmetic operators in C
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Example:
int x = 10, y = 3;
int r1 = x + y; // 13
int r2 = x / y; // 3 (integer division)
int r3 = x % y; // 1
Note: If both operands are integers, division discards decimals.
Operator Precedence
What is precedence?
Precedence decides which operator is evaluated first in an expression.
Just like mathematics:
Multiplication before addition
Parentheses override everything
Basic precedence order
Priority Operators
Highest ()
* / %
Lowest + -
20
Example:
int result = 10 + 2 * 3;
Evaluation:
2 * 3 = 6
10 + 6 = 16
Not 36. Never 36.
Parentheses rule everything
int result = (10 + 2) * 3;
Now:
10 + 2 = 12
12 * 3 = 36
Parentheses force the order. Use them generously. Clarity beats cleverness.
Associativity
If operators have the same precedence, associativity decides direction.
Arithmetic operators associate left to right.
Example:
int x = 20 / 5 * 2;
Evaluation:
20 / 5 = 4
4 * 2 = 8
Not:
5 * 2 = 10
20 / 10 = 2
Left to right wins.
21
Integer vs floating-point arithmetic
int a = 5, b = 2;
float c = a / b;
Result:
a / b = 2
c = 2.0
Correct version:
float c = (float)a / b; // 2.5
22
Conditional Branching
What is conditional branching?
Conditional branching means:
Execute different blocks of code depending on whether a condition is true or false.
In plain terms: if this happens, do that; otherwise, do something else.
Conditions
A condition is an expression that produces a boolean result.
Relational operators
Operator Meaning
< Less than
> Greater than
23
Operator Meaning
<= Less than or equal
>= Greater than or equal
== Equal to
!= Not equal to
Example:
a > b
x == 0
marks >= 40
Each of these evaluates to either true or false.
if statement (single branching)
if (condition) {
statement;
}
Example:
if (marks >= 40) {
printf("Pass");
}
Evaluation logic
Condition is checked
If true → block executes
If false → block is skipped
if–else (two-way branching)
if (condition) {
consequent_block;
} else {
alternative_block;
}
Example:
if (age >= 18) {
printf("Eligible to vote");
} else {
printf("Not eligible");
}
Here:
Condition → age >= 18
24
Consequent branch → executed if condition is true
Else branch → executed if condition is false
Exactly one branch runs. Never both.
Nested if–else (multiple conditions)
if (marks >= 90) {
grade = 'A';
} else if (marks >= 75) {
grade = 'B';
} else if (marks >= 60) {
grade = 'C';
} else {
grade = 'F';
}
Evaluation flow
Conditions are checked top to bottom
First true condition wins
Remaining conditions are ignored
Order matters. Bad ordering equals bad logic.
switch statement (multi-way branching)
Used when a variable is compared against fixed constant values.
switch (choice) {
case 1:
printf("Add");
break;
case 2:
printf("Subtract");
break;
default:
printf("Invalid choice");
}
25
Key rules:
Expression must be integer or char
case labels must be constants
break prevents fall-through
default runs if no case matches
Without break, C keeps falling like a domino chain.
………………………………………….
26