Professional Documents
Culture Documents
Arithmetic Operations
Arithmetic Operations
Expressions
• Assignment expression
⚫ Uses assignment operators
• Logical expressions
⚫ Uses relational and logical operators
• a + b * c –d / e a + (b * c) – (d / e)
• a * –b + d % e –f a * (– b) + (d % e) – f
• a –b + c + d (((a – b) + c) + d)
• x*y*z ((x * y) * z)
• a+b+c*d*e (a + b) + ((c * d) * e)
Type of Values
Integer Arithmetic
⚫ If all operands of an operator are integer (int variables or integer
Real Arithmetic
Arithmetic operations involving only real or floating-point operands.
• Since floating-point values are rounded to the number of significant digits
permissible, the final value is an approximation of the final result.
1.0 / 3.0 * 3.0 will have the value 0.99999 and not 1.0
Mixed Arithmetic
⚫ If at least one operand is real, the value is real
Assignment Expression
⚫ Uses the assignment operator (=)
⚫ General syntax:
⚫ variable_name = expression
⚫ variable_name1=varable_name2
⚫ A = A + 10
⚫ v = u + f * t
⚫ s = u * t + 0.5 * f * t * t
Assignment Expression
⚫ Several variables can be assigned the same value using multiple
assignment operators
⚫ a = b = c = 5;
a value
⚫ Multiple assignment operators are right-to-left associative;
Types of l-value and r-value
⚫ Usually should be the same
⚫ If not, the type of the r-value will be internally converted to the type of the l-value, and then
assigned to it
⚫ Example1:
⚫ double a;
⚫ a = 2*3;
⚫ Type of r-value is int and the value is 6
⚫ Type of l-value is double, so stores 6.0
⚫ Example2
⚫ int a;
⚫ a = 2*3.2;
⚫ Type of r-value is float/double and the value is 6.4
⚫ Type of l-value is int, so internally converted to 6
⚫ So a stores 6, not the correct result
More on Assignment
⚫+=, -=, *=, /=, %=
⚫ Operators for special type of assignments
⚫ a += b is the same as a = a + b
⚫ x-=y, x=?
⚫ x*=y, x=?
⚫ x/=y, x=?
⚫ X % y, x=?
Problem Solution: Typecasting
⚫ Changing the type of a variable
int a=10, b=4, c;
during its use
float x;
c = a / b; ⚫ General form
x = a / b; ⚫ (type_name) variable_name
The value of c will be 2
The value of x will be 2.0
⚫ Example
But we want 2.5 to be stored in x ⚫ x = ((float) a)/ b;
⚫ Evaluates to value 0 or 1
⚫ != is not equal to
Relational Operator
⚫ (! (grade == ’A’))
Examples
⚫ (4 > 3) && (100 != 200)
⚫ Value?
⚫ Value?
⚫ Value?
⚫ a = 3 && b = 4
⚫ Value?
Examples
⚫ (4 > 3) && (100 != 200)
⚫ Value?
⚫ Value?
⚫ Value?
⚫ a = 3 && (b = 4)
⚫ Value?
Increment and Decrement Operator
+ + or − −
++m; or ++m;
--m; or − −𝑚;
The operator + + add 1 to the operand
The operator − − subtract 1 to the operand
Increment and Decrement Operator
𝑚 = 5; 𝑚 = 5;
𝑦 =+ +𝑚; 𝑦 = 𝑚 + +;
𝑚 = 6; 𝑚 = 6;
𝑦 = 6; 𝑦 = 5;
Conditional Operator
𝑎 = 10;
𝑏 = 15;
𝑥 = 𝑎 > 𝑏 ? 𝑎 ∶ 𝑏;
Bitwise Operator
~ One’s complement
& bitwise AND
| bitwise OR
^ bitwise XOR
≪ shift Left
≫ shift right
Example
#include <stdio.h> // The result is 00001100 (12)
int main()
{ printf("a^b = %d\n", a ^ b);
unsigned char a = 5, b = 9;
// a = 5(00000101), // The result is 11111010 (250)
// b = 9(00001001) printf("~a = %d\n", a = ~a);
Unary Postfix++, -- L to R
High
Unary ++, --Prefix R to L
Binary */% L to R
Binary +- L to R
Compound statements
⚫ A sequence of statements enclosed within { and }
True
Marks >=70 Print Grade “B”
Print “Passed”
False
Exit
Example1: Largest of three numbers
void main()
{
int a,b,c;
scanf (“%d%d%d”, &a, &b, &c);
if ((a >= b) && (a >= c))
printf (“\n The largest number is: %d”, a);
if ((b >= a) && (b >= c))
printf (“\n The largest number is: %d”, b);
if ((c >= a) && (c >= b))
printf (“\n The largest number is: %d”, c);
}
Example2:Calculation of Bonus
Void main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and year of joining " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}}
Branching: The if-else statement
if (expression)
{
if (expression) Block of statements;
{ }
Block of statements; else if
{
} Block of statements;
else }
else
{ {
Block of statements; Block of statements;
}
}
Example1:Largest of three numbers
void main ()
{
int x, y, z, max;
scanf (“%d%d%d”,&x,&y,&z);
if (x > y)
max = x;
else
max = y;
if (max > z)
printf (“max is %d”, max) ;
else
printf (“max is %d”,z);
}
Example2:Largest of three numbers
/* Calculation of max */ /* Calculation of max */
void main () void main ()
{ {
int x, y, z, max; int x, y, z, max;
scanf (“%d%d%d”,&x,&y,&z); scanf (“%d%d%d”,&x,&y,&z);
if ((x > y) && (x >z)) if ((x > y) && (x >z))
max = x; printf (“Max is %d”, x) ;
else if ((y > x) && (y >z)) else if ((y > x) && (y >z))
max = y; printf (“Max is %d”, y) ;
else else if ((y > x) && (y >z))
max=z; printf (“Max1 is %d”, z) ;
printf (“Max is %d”, max) ;
} }
Branching: The nested if-else statement
if (expression)
if (expression)
{
{
Block of statements;
if (expression)
}
{
else
Block of statements;
{
}
if (expression)
Else
{
{
Block of statements;
Block of statements;
}
}
Else
}
{
else
Block of statements;
{
}
Block of statements;
}
}
Thanks
Any Questions?