Programming Element
Prepared By Dr Goh Wan Inn
Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1
(Operators & Expression)
Statements,
Arithmetic
Operators &
Operators
Operands
Relational
Logical Operators
Operators
Assignment
Operators
Statement
• Statements are elements in a program which
(usually) performing with suitable object
– e.g. below is a statement of calling input value
length = float(input(“Insert Length: “))
An expression statement is a statement that results a value
SOME EXAMPLES OF EXPRESSION VALUE
• Literal expression The literal itself
e.g. 2, “A+”, ‘B’
• Variable expression The content of the variable
e.g. Variable1
• Arithmetic/Mathematic The result of the operation
expression
e.g. 2 + 3 -1
Operators & Operands
• Operands is something that make the operator
be act
• Operators can be classified according to
– the type of their operands and of their output
• Arithmetic
• Relational
• Logical
• Assignment
– the number of their operands
• Unary (one operand)
• Binary (two operands)
• Ternary (unique to the C language)
• A combination of operands and operators called
as Operations!
Arithmetic operators
• They operate on numbers and the result is
a number.
• The type of the result depends on the
types of the operands.
• If the types of the operands differ (e.g. an
integer added to a floating point number),
one is "promoted" to other.
– The "smaller" type is promoted to the "larger"
one.
int → float
10 + 11.8 = 21.8
Arithmetic operators: +, *
• + is the addition operator
• * is the multiplication operator
• They are both binary
Arithmetic operator: −
◼ This operator has two meanings:
◼ subtraction operator (binary) e.g. 31 - 2
◼ negation operator (unary) e.g. -10
Arithmetic operator: /
◼ Division operator
◼ CAREFUL! The result of integer division is an integer:
e.g. 5 / 2 is 2, not 2.5
Arithmetic operator: %
• The modulus (remainder) operator.
• It computes the remainder after the first operand is divided by
the second
e.g. 5 % 2 is 1, 6 % 2 is 0
Relational operators
• These perform comparisons and the result is what
is called a boolean: a value TRUE or FALSE
• FALSE is represented by 0; anything else is TRUE
• The relational operators are:
– < (less than)
– <= (less than or equal to)
– > (greater than)
– >= (greater than or equal to)
– == (equal to)
– != (not equal to)
Logical operators
(also called Boolean operators)
• These have Boolean operands and the
result is also a Boolean.
• The basic Boolean operators are:
– and (logical AND)
– or (logical OR)
– not (logical NOT) -- unary
Assignment operator(=)
• Binary operator used to assign a value to
a variable.
Combined Assignment Operators
Special assignment operators
• write a += b; instead of a = a + b;
• write a -= b; instead of a = a - b;
• write a *= b; instead of a = a * b;
• write a /= b; instead of a = a / b;
• write a %= b; instead of a = a % b;
Precedence & associativity
• How would you evaluate the expression
17 - 8 * 2 ?
Is it 17 - (8 * 2)
or (17 - 8) * 2 ?
• These two forms give different results.
• We need rules!
Precedence & associativity
• When two operators compete for the same
operand (e.g. in 17 - 8 * 2 the operators -
and * compete for 8) the rules of
precedence specify which operator wins.
– The operator with the higher precedence wins
• If both competing operators have the same
precedence, then the rules of associativity
determine the winner.
Precedence & associativity
not Unary – higher precedence
* / %
+ –
Associativity: execute left-to-right (except
< <= >= > for = and unary – )
== !=
and
or
lower precedence
=
Example: Left associativity
3*8/4%4*5
Example: Right associativity
a += b *= c-=5
Precedence & associativity
• Examples:
X =17 - 2 * 8 Ans: X=17-(2*8) , X=1
Y = 17 - 2 - 8 Ans: Y = (17-2)-8, Y=7
Z = 10 + 9 * ((8 + 7) % 6) + 5 * 4 % 3 *2 + 1 ?
Not sure? Confused? then use parentheses in your code!
Algebraic Expressions
◼ Multiplication requires an operator:
Area=lw is written as Area = l * w;
◼ There is no exponentiation operator:
Area=s2 is written as Area = s*s or s**2
◼ Parentheses may be needed to maintain order of
operations:
y 2 − y1
m= is written as
x 2 − x1 m = (y2-y1) /(x2-x1);
Algebraic Expressions (cont…)