chapter 3 expression and operators
DESCRIPTION
CHAPTER 3, COMPUTER PROGRAMMING (DAE 20102)TRANSCRIPT
COMPUTER PROGRAMMING
DAE 20102CHAPTER 3:
Expressions And Operators
OPERATORS(Operators and Symbols)
Operator SymbolArithmetic +,-,*,/,%Relational <,<=,>,>=,==,!=
Logic &&,||,!
• Symbols like +,-,*,/,<,> are known as operators.
• Data stored in memory can be modified using operators.
• The operators are divided into three (3) distinct types.
ARITHMETIC OPERATORS
Arithmetic operators in C are divided into two (2):
• Unary
• Binary
ARITHMETIC OPERATORS(Unary Operators Symbols)
Operator Function
+ Positive operator
- Negative operator
++ Increment operator
-- Decrement operator
! Not operator
& Address operator
* Value of address operator
• Unary operators are fixed to one (1) variable only.
EXPRESSIONS
Content of a Before
Expressions Value of expressions
Content of a After
10 a++ (a=a+1) 10 11
10 a-- 10 9
10 ++a (a=a+1) 11 11
10 --a 9 9
EXPRESSIONS
#include <stdio.h>int main(void) { int a= 1, b=1, c=3, d=1;
c--;printf(“%d %d”, 4-d++, c);printf(“%d %d %d %d \n”, --a, b++, c--, d);
return 0;}
EXPRESSIONS
Expression Value of x Value of y
y+=x;y=y+x;
5 12
y-= x++; 6 2
y*= ++x * 2; 6 84
• Assume that int x = 5; int y = 7;
EXPRESSIONS
Expression Value of x Value of y
y+=x; y+=xy = y + x = 7 + 5 = 12
5 12
• Assume that int x = 5; int y = 7;
EXPRESSIONS
Expression Value of x Value of y
y-= x++; y-=x++y = y – x++ = 7 – 5 = 2
6 2
• Assume that int x = 5; int y = 7;
EXPRESSIONS
Expression Value of x Value of y
y*= ++x * 2; y*=++x * 2y = y * (++x) * 2 = 7 * (6) * 2 = 42 * 2 = 84
6 84
• Assume that int x = 5; int y = 7;
ARITHMETIC OPERATORS(Binary Operators Symbols)
Operator Function
+ Addition operator
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator (int only)
• Operands that are fixed with arithmetic operators must be of numeric types.
• Operators is put in between two (2) operands.
ARITHMETIC OPERATORS(Binary Arithmetic Operators)
Value a Value b Integer division Real division
10 3 10/3 = 3 10.0/3.0=3.333333
5 10 5/10 = 0 5.0/10.0=0.500000
6 6 6/6 = 1 6.0/6.0=1.000000
• Division calculations
ARITHMETIC OPERATORS(Binary Arithmetic Operators)
Operation Modulos result Remainder
5 % 3 1 2
7 % 2 3 1
12 % 2 6 0
• Modulos calculations
ARITHMETIC EXPRESSIONS(Precedence levels)
• Combining one or more arithmetic operations.• Precedence level will determine the arithmetic operation
sequence in solving the expression.
Precedence level Operation
High ()
++,--
*,/,%
+,-
+=,-=,*=,/=,%=
>,<,<=,>=
Low ==,!=
ASSIGNMENT STATEMENT
• Assignment of data to the variable can be written as:Variable = expression;
e.g:-Berat_Kereta = 85;Diameter_Tayar = 18;Horse_Power = Diameter_Tayar * ( 12 +
Berat_Kereta);
• For the above statement, the left part can only be a variable. On the right, it can be made up of a combination of variables and constants.
ASSIGNMENT STATEMENT(Compound Assignment Statement)
Operation Example Meaning
+= val += 5; val = val + 5
-= Val -= 5; val = val – 5
*= val *= 5; val = val * 5
/= val /= 5; val = val / 5
%= val %= 5; val = val % 5
• Modify a variable value where the variable original value is added/minus/multiply by another value and is assigned back to the original variable.
• e.g:price = price – discount;
RELATIONAL OPERATORS
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
• Used to do comparisons.
LOGIC OPERATOR
Operator Function Meaning
&& And It is true if and only if both the expressions are true
|| Or It is true if one or both the expressions are true
! Not It is true if the expression is false and vice versa
• Useful to do complex comparisons to make decisions.