4 Operatorsandexpression
4 Operatorsandexpression
4 Operatorsandexpression
• 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
• 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
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
37
Logical AND
39
Logical Expression
NOT(!)
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
•𝑓𝑓 𝑥𝑥 =
2𝑥𝑥 2
5+ 10𝑦𝑦
Exercise
2𝑥𝑥 3
𝑓𝑓 𝑥𝑥 = − 5( 20)
5𝑦𝑦
Explanation:
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?
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)