Professional Documents
Culture Documents
Chapter4part2 Arithmeticoperation Class
Chapter4part2 Arithmeticoperation Class
2x + 3 – z = y
expression
Expression in C :
Operand
arithmetic
Consists of
relational
logical
5 Prepared by Norhamreeza Abdul Hamid
Known as
Mathematic Expression
using
Arithmetic Operator
example
Computer memory cell
a = -20
a -20
b = +15
b +15
Increment Decrement
Prefix ++h --h
Postfix h++ h--
int A = 5;
++A;
printf(“%d”,A); // output??
A--;
printf(“%d”,A); // output??
A++;
printf(“%d”,A); // output??
11 Prepared by Norhamreeza Abdul Hamid
EXERCISE 2
int B;
B = 2;
printf(“%d”, 3 + --B); // output??
printf(“%d”, B); // output??
B = 9;
printf(“%d”, 3 + B--); // output??
printf(“%d”, B); // output??
example
A + z
Operator
Operand
example
A * z
Operator
Operand
example
A / z
Operator
Operand
There is no promotion
result = 5 / 2 occurred. Both operands
result = 2 are the same type.
example
A % z
Operator
Operand
int A, B;
2
float C; 2 5
A = 2; 4
B = 5; 1
Output for
B % A;
B%A=1
2nd operand
is a value
example
(a > 2) ? 1 : 0
Assignment statement
1. average = (6 + 4) / 2; average 5
5 + 12 - 4 / 2 3*4/2 + 2
5 + 12 - 2 12 / 2 + 2
17 - 2 6+2
15 8
29 Prepared by Norhamreeza Abdul Hamid
Example
3. Prefix unary arithmetic expression
4 30
4 35
3*8/4%4*5
a += b *= c -= 5
int main()
{
int x = 25, y;
y = sqrt(x);
printf(“%d”, y);
}
5
a) b = 3 + b b) x = (a – b)(a – c2)
a+4
c. ++a d. c + a * c / (3 * c)
Consists of
Produce
!= Not equal
1) a+ b == c 2) a != b
6 + 1== -2 6 != 1
7 == -2
3) b < c 4) b + c <= a
1 < -2 1 + -2 <= 6
-1 <= 6
#include <stdio.h>
int main()
{
int age;
printf(“Please enter your age: ”);
scanf(“%d”, &age);
if (age > 21)
printf(“You are qualified to vote”);
return 0;
}
Consists of
Produce
&& AND
|| OR
! NOT
NOT (!)
Logical Operator Result
Value Result
False OR False False
!0 1
False OR True True
!1 0
1 && 1 (1 < 2) || (6 == 6)
1 1 || 1
1
51 Prepared by Norhamreeza Abdul Hamid
Evaluate the following logical expression:
Given a = 3, b = 4;
!(1) ! ( 0)
0 1
52 Prepared by Norhamreeza Abdul Hamid
An Example program which using Logical Expression
#include <stdio.h>
int main()
{
int marks;
printf(“\nPlease enter your marks >> ”);
scanf(“%d”, &marks);
if (marks >= 85 && marks <= 100)
printf(“\nGred A”;
else if (marks >= 80 && marks <= 84)
printf(“\nGred A-”;
return 0;
}