4 Operatorsandexpression

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

chapter 4:

Operators and Expression


Operators (symbols)

• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Conditional operators
• Comma operators
• Unary operators
Expression (how to write in code)

• Arithmetic expression
• Logical expression
• Character/string expression
Arithmetic Operators

Arithmetic operators C operators


Addition +
Substraction -
Multiplication *
Division /
Modulo division % (remaining value after divide with integer)
Examples
What is % (modulo division)

• The remaining value after an integer was divided.


• Example 5%2=1 (5 divided by 2, remaining value =1 ) 2
2 5
4
1 (modulo division)

• Examples: 8%3 = 2
• Example: 10%5 = 0
• Example: 15%4%2= (15%4)%2=3%2 =1 (15%4 you get 3,3%2 you get 1)
Arithmetic Expression

• Integer Expression
• Floating point expression
• Mixed mode expression
Integer Expression (%d)
• Mathematical expression obtained by combining integer variables and/or integer
constant with help of arithmetic operators
• Rules
• A signed or unsigned integer variable or an integer constant is an integer expression
• An integer expression connected by arithmetic operator to an unsigned integer constant is
an integer expression
• An integer expression enclosed in parentheses is an integer expression
• 2 integers expression connected by an arithmetic operator is an integer expression
Example

Explanation Outputs:
• j+ k = 8+5 =13
• j – k = 8-5 = 3
• j/k = 8/5 = 1 (1.6 in integer only considers ‘1’)
• 1/2+1/2 = 0 (0.5 in integer is ‘0’, 0+0=0)
• j%k=8%5=3
Exercise

• Create code: sum of a+b, a=10, b=20

• Create code: sum and multiply, total=5(a+b)

• Create code: multiply and sum, total=(a x b) + c


Floating point expression (%f)

• Mathematical expression obtained by combining floating point variables


and/or floating point constant with help of arithmetic operators
• Rules- same with integer
Mixed mode expression and implicit
conversion
• Expression formed by integer, floating point or character constant
• A is integer and B floating point.. A/B??
• Before division A must be converted to integer then A is divided by B. Finally result will
be stored as floating point number.
• Only integer can be divided with integer
• During conversion lower type operand are converted to higher type operand
and the result is the higher type
Size of operator

Give the size of operand in bytes:


• Integer: 4 bytes
• Float: 4 bytes
• Char: 1 byte
Questions- determine the size of below
variable (bytes)

• int b[5],c=5;  sizeof b:


• float x=1.2,y=6.0;  sizeof y:
• double k;  sizeof k:
• char j;  sizeof j:
Mixed Arithmetic

e.g. 5 + 2 * 6 – 4 / (1+1)
• Steps to solve mixed arithmetic (left to right):
5 +2 * 6 - 4 / 2 1. If the operation in ( ), it need to be solved
first.
5 + 12 - 2 2. Solve ( * ) and ( / ) from left to right.
3. Solve ( + ) and ( - ) from left to right.
17 – 2 =15
Global declaration (#define, int)

• #include<stdio.h>
• #define a 2+4+1
a = 2+4+1

• #define b(a) a+2+45 b(a) = a+2+45


= 2+4+1+2+45
• int c=12+3-5;
c=10

• int d=5*2/4+5; d=10/4+5


=2+5 = 7
Exercise- what are the outputs?
Exercise- what are the outputs?
Mixed arithmetic (int and float)

Answer = 11 Answer = 1.23


What are the outputs?
Unary Operators

Operator Meaning Value


- negative Negative sign (-)
+ positive Positive sign
-- decrement -1
++ increment +1
Example of UNARY Rules:
Type casting (e.g. intfloat)

Purpose: to change the original identifier type


Format: (data_type) variable name
Expression Action
A= (int) 8.75 The value of A is finally 8
B= (float)x +y x is converted to float then added to y
C=(double)(x+y) x +y converted to double
D=(char) 65 65 converted to character (A) – ASCII value
E= (float)1/5 1 is converted to float 1.0 divided by 5. the value
of E becomes 0.2
I=(int) ‘D’ D converted to integer. The ASCII value of D is
stored to I (68)
Changing integer to float value format
( int  float)
Writing operators in different way
Example:
Exercise

• Given a=10, b=20, c=30;


• Show in different to write the sum expression (a+b+c), and subtraction (a-b-c)
Exercise

• Given a=10, b=20, c=30;


• Show in different to write the division expression (a/b/c), and modulo division
(a%b%c)
True or false (0,1)
True or false (0,1)
Exercise

• Develop a complete c program to compare integer x and y (user to insert x


and y value), next, the evaluate the comparisons: x<y , x=y, x!=y, x>y)
Logical Expression

Logical operator truth table for AND

AND (&&) Logical Operator Result


False AND False False
Value 0 1
False AND True False
0 0 0
True AND False False
1 0 1
True AND True True

37
Logical AND

• Given a = 10, b = 20, c =30


• a < b and b<c , return TRUE (1) result
• a > b and b < c, return FALSE (0) result
Logical Expression

Logical operator truth table for OR

OR (||) Logical Operator Result


False OR False False
Value 0 1
False OR True True
0 0 1
True OR False Trrue
1 1 1
True OR True True

39
Logical Expression

Logical operator truth table for NOT

NOT(!)

Value Result Logical Operator Result


!0 1 Not false True

!1 0 Not true False

40
Math Library Functions
#include<math.h>
Mathematic function C Code Mode of output Header file
Power (ab) pow (a,b) double (float) math.h
Exponential exp(x) double (float) math.h
logarithm log(x) double (float) math.h
squareroot sqrt(x) double (float) math.h
Sin sin( x) double (float) math.h
Cos cos(x) double (float) math.h
Tangent tan(x) double (float) math.h
Absolute |x| abs(x) int stdlib.h
Absolute |x| fabs(x) double math.h
Round up to next floor(x) double math.h
integer value
Examples

Explanation:

C = 53

𝑪𝑪 = 𝒂𝒂 = 𝟏𝟏𝟏𝟏𝟏𝟏

X = Sin 15

X= log 10

X = e2
More
examples
Exercise

• Develop a complete c program to evaluate f(x) value below:

•𝑓𝑓 𝑥𝑥 =
2𝑥𝑥 2
5+ 10𝑦𝑦
Exercise

2𝑥𝑥 3
𝑓𝑓 𝑥𝑥 = − 5( 20)
5𝑦𝑦

• Please compute a program to calculate the below function:


Conditional operator (?)
• The general synthax is Expression1 ? Expression2: expression 3;
• Value of expression1 is evaluate first. If value true, expression2 is performed otherwise
expression3 is performed.

Explanation:

C =a>b ?  you want to know whether a>b (since the program


already defined a=100, b=150)

C = 100>150 ?
If yes (true) = the program will select the proposed answer 20
If no (false) = the program will select the proposed answer 30
(user can set the TRUE AND FALSE VALUE themselves)
What is the answer for below c code?

True (1) : Not True (0)


Exercise

• Please design a software to evaluate x and y, user


to give input to x and y variable. The conditional
“?” operator to measure y>x? is Yes and No
Exercise- what is the c program output?
Comma operator ( , )
• A set of expression within parentheses and separated by commas can be
assigned to variable
• The expression evaluated left to right and the value of last expression is the
final value of variable

Explanation:

f=(b=10,b=b*a,c=2,b/c)
Solve f from left to right:
f=b=10
f=b=10*2=20
f=c=2
f=b/c=20/2 =10 (final answer for f is 10)
Unary operator (++,--)
(e.g. ++ is +1; -- is -1)

To print ‘a’ value with “-ve” sign, no new ‘a’ generated


To print ‘a’ in “+ve”, no new ‘a’ generated
Just a basic “+” operation, no new ‘a’ generated
Unary operation take effects, new ‘a1’ generated = +1+(-2)= -1
Unary operation take effects, new ‘a2’ generated = +1+(-1)= 0
To present ‘a2’ in “+ve”, no new ‘a’ generated
Just a basic operation of new ‘a2’ + b = 0 + 2 = 2
Unary operation take effects, new “a3” generated = -1+0 = -1
Exercise 1-Unary Operator

• Extract the answers and explain/justify your answer.


Thank you.

You might also like