Operators:
Assignment & Arithmetic
Procedural Programming
Mario Simaremare, S.Kom., M.Sc.
Program Studi Sarjana Sistem Informasi
Institut Teknologi Del
Objectives
• The objective of this session is the following:
• students are able to elaborate the role of operators.
• students are able to use assignment and arithmetic operators.
Procedural Programming 2
Please see this material first
Procedural Programming 3
Outlines
1. PEMVIS’ “Operators”.
2. Operator.
3. Assignment operator.
4. Arithmetic operators.
5. Prefix and postfix increment and
decrement operations.
Procedural Programming 4
Operator
Procedural Programming 5
Operator
• Solution = algorithm + data.
• Operation is needed to use and manipulate data.
• By following the algorithm.
• Operators operate one or two operands in an operation.
• One-operand operator is also called as unary operator.
• Two-operand operator is called as binary operator.
• Different data types might be operated differently,
hence different operators are needed.
Procedural Programming 6
Assignment Operator
Procedural Programming 7
Assignment operator
• It sets a value (right side expression) to a variable (left side).
• The operator is labeled with the assignment sign (=).
• Used for assigning a value into a particular variable.
• When the value’s type and the variable’s types are different:
• If the designated variable’s type is smaller, truncation might occurs.
• Automatic value conversion takes place.
• Multiple assignments are possible.
identifier = expression;
identifier1 = identifier2 = identifiern = expression;
Procedural Programming 8
Arithmetic Operators
Procedural Programming 9
Arithmetic operators
Action Operator To operate numerical values.
Addition +
Subtraction & negation - Compound operations, where arithmetic and
Multiplication * assignment operator applied at the same time.
E.g. s += 3;
Division /
short int s = 10;
Modulus % s += 2; //equals to s = s + 2; 12
Increment (pre & post) ++ s /= 3; //equals to s = s / 3; 4
s *= 4; //equals to s = s * 4; 16
Decrement (pre & post) -- s %= 2; //equals to s = s % 2; 0
Procedural Programming 10
Pre- and -post increment and decrement
short int r, s;
r = 0;
• Unary, one operand operator.
s = 1; • They affect the operands
r = ++s; //pre-increment; r:2; s:2; differently.
r = s++; //post-increment; r:2; s:3; • prefix increment/decrement
r = --s; //pre-decrement; r:2; s:2; will operate the operand first.
r = s--; //post-decrement; r:2; s:1;
• postfix increment/decrement
r = ++s + s; //r:4; s:2; will operate the operand after
r = s++ + s; //r:5; s:3; what? (later).
r = s++ + s++; //r:7; s:5; what?
r = s++ + ++s; //r:12; s:7; what?
r = ++s + s++ + s++; //r:25; s:10; what?
Procedural Programming 11
prefix and postfix
short int r, s; increment and decrement
r = 0;
s = 1; Increment first r = (s += 1) + s;
followed by anaddition
r = ++s + s; r = s + (s += 1);
2 2 Addition first followed
by an increment
r = s++ + s; r = s + (s += 1); s += 1;
2 3
Increment first followed by an
addition, then another increment
r = s++ + s++; 5
3 4
Procedural Programming 12
Todos
• Try playing with
• compound operators.
• prefix and postfix increment and decrement operations.
Procedural Programming 13
References
• Kerninghan & Ritchie. The C Programming Language.
• Herbert Schildt. C: The Complete Reference.
Procedural Programming 14
Thank
you
Procedural Programming 15