0% found this document useful (0 votes)
30 views

Algoritma & Pemrograman 01 Operators For Fundamental Types v1.1

Operators for Fundamental Types discusses various binary, unary, and logical operators in C++ and their use and precedence. It provides examples of arithmetic operators like addition, subtraction, multiplication, and division. It also covers increment/decrement, assignment, relational, and logical operators. The document explains operator precedence and contains sample programs and exercises to demonstrate the use of different operators.

Uploaded by

J3ane_Titahena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Algoritma & Pemrograman 01 Operators For Fundamental Types v1.1

Operators for Fundamental Types discusses various binary, unary, and logical operators in C++ and their use and precedence. It provides examples of arithmetic operators like addition, subtraction, multiplication, and division. It also covers increment/decrement, assignment, relational, and logical operators. The document explains operator precedence and contains sample programs and exercises to demonstrate the use of different operators.

Uploaded by

J3ane_Titahena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Algoritma & Pemrograman

Operators for Fundamental Types

Prepared by
Frans Panduwinata, S.Kom, M.T
Binary Operator and Operands
Binary Arithmetic Operators
Binary Arithmetic Operators (cont)

• Arithmetic operators are used to perform calculations.


– Divisions performed with integral operands will produce integral
results;
For EXAMPLE,
7/2 computes to 3.

If at least one of the operands is a floating-point number,


the result will also be a floating-point number;
e.g., the division 7.0/2 produces an exact result of 3.5.
Binary Arithmetic Operators (cont)

• Arithmetic operators are used to perform calculations


(cont).
– Remainder division is only applicable to integral operands and
returns the remainder of an integral division.
For EXAMPLE,
7%2 computes to 1.
Expressions

• In its simplest form an expression consists of only one


constant, one variable, or one function call.

• In the case of arithmetic expressions, the operands define


the type of the expression.
EXAMPLES:
int a(4); double x(7.9);
a * 512 // Type int
1.0 + sin(x) // Type double
x–3 // Type double, since one
// operand is of type double
Expressions (cont)

• An expression can be used as an operand in another


expression.
EXAMPLE:
2+7*3 // Adds 2 and 21
Expressions (cont)

• Normal mathematical rules (multiplication before addition)


apply when evaluating an expression,
i.e. the *, /, % operators have higher precedence than + and -.
However, you can use parentheses to apply a different precedence
order.
EXAMPLE :
(2 + 7) * 3 // Multiplies 9 by 3.
Sample Program
Unary Arithmetic Operators
Sign Operators

• The sign operator (–) returns the value of the operand but
inverts the sign.
EXAMPLE:
int n = –5; cout << -n; // Output: 5

The sign operator + performs no useful operation, simply returning the


value of its operand.
Increment / Decrement Operators

• The increment operator ++ modifies the operand by


adding 1 to its value

• Given that i is a variable,


 both i++ (POSTFIX notation) and ++i (PREFIX notation) raise the
value of i by 1.
 In both cases the operation i = i + 1 is performed.
Increment / Decrement Operators (cont)

• However, prefix ++ and postfix ++ are two different


operators.

• The difference becomes apparent when you look at the


value of the expression;
 ++i means that the value of i has already been incremented by 1,
whereas
 the expression i++ retains the original value of i.
Effects of Prefix and Postfix Notation
Precedence

• How is an expression with multiple operators evaluated?


EXAMPLE:
float val(5.0);
cout << val++ – 7.0/2.0;

• Operator precedence determines the order of evaluation,


i.e. how operators and operands are grouped.
EXAMPLE:
(val++) – (7.0/2.0).

The result is 1.5, as val is incremented later.


Precedence of Arithmetic Operators

Example if two operators have equal precedence:


3 * 5 % 2 is equivalent to (3 * 5) % 2
Simple Assignments

• A simple assignment uses the assignment operator = to


assign the value of a variable to an expression.
EXAMPLES:
z = 7.5;
y = z;
x = 2.0 + 4.2 * z;
Simple Assignments (cont)

• Each assignment is an expression in its own right, and its


value is the value assigned.
EXAMPLE:
sin(x = 2.5);

In this assignment the number 2.5 is assigned to x and then passed to


the function as an argument.
Simple Assignments (cont)

• Multiple assignments, which are always evaluated from


right to left, are also possible.
EXAMPLE :
i = j = 9;

In this case the value 9 is first assigned to j and then to i.


Compound Assignments

• Compound assignment operators simultaneously perform


an arithmetic operation and an assignment.
EXAMPLES:
i += 3; is equivalent to i = i + 3;
i *= j + 2; is equivalent to i = i * (j+2);

The following compound operators are thus available:


+=, -=, *=, /=, and %=
Sample Program
Relational Operators
The Result of Comparisons

• Each comparison in C++ is a bool type expression with a


value of true or false,
EXAMPLE:
length == circuit // false or true

– If the variables length and circuit contain the same number,


the comparison is true and the value of the relational expression is
true.

– But if the expressions contain different values, the value of the


expression will be false.
The Result of Comparisons (cont)

• When individual characters are compared, the character


codes are compared.
The following expression results in the value true when ASCII code is
used.
EXAMPLE:
A' < 'a' // true, since 65 < 97
Examples for comparisons
Precedence of Relational Operators

• Relational operators have lower precedence than


arithmetic operators but higher precedence than
assignment operators.
EXAMPLE:
bool flag = index < max – 1;

– max – 1 is evaluated first,


– then the result is compared to index, and
– the value of the relational expression (false or true) is assigned
to the flag variable.
Precedence of Relational Operators (cont)
Precedence of Relational Operators (cont)

• EXAMPLE:
int result;
result = length + 1 == limit;

– length + 1 is evaluated first,


– then the result is compared to limit, and
– the value of the relational expression is assigned to the result
variable.
– Since result is an int type, a numerical value is assigned
instead of false or true,
i.e. 0 for false and 1 for true.
Precedence of Relational Operators (cont)

• It is quite common to assign a value before performing a


comparison, and parentheses must be used in this case.
EXAMPLE:
(result = length + 1) == limit

Our example stores the result of length + 1 in the variable result


and then compares this expression with limit.
Operands and Order of Evaluation

• The operands for boolean type operators are of the bool


type.

• The OR operator || will return true only if at least one


operand is true, so the value of the expression
EXAMPLE:
(length < 0.2) || (length > 9.8)

is true if length is less than 0.2 or greater than 9.8.


Operands and Order of Evaluation (cont)

• The AND operator && will return true only if both


operands are true, so the logical expression
EXAMPLE:
(index < max) && (cin >> number)

is true, provided index is less than max and a number is successfully


input.

If the condition index < max is not met, the program will not attempt
to read a number!
Operands and Order of Evaluation (cont)

• One important feature of the logical operators && and || is


the fact that
– there is a fixed order of evaluation.
– The left operand is evaluated first and if a result has already been
ascertained, the right operand will not be evaluated!

• The NOT operator ! will return true only if its operand is


false.
– If the variable flag contains the value false (or the value 0),
– !flag returns the boolean value true.
“Truth” Table for Logical Operators

The logical operators


comprise the boolean
operators
&& (AND), || (OR), and
! (NOT).
Examples for Logical Expressions
Precedence of Boolean Operators

• The && operator has higher precedence than ||.

• The precedence of both these operators is


 higher than the precedence of an assignment operator, but
 lower than the precedence of all previously used operators.
 This is why it was permissible to omit the parentheses.

• The ! operator is a unary operator and thus has higher


precedence.
Exercises

EXERCISE 1
What values do the following arithmetic expressions have?

a. 3/10 b. 11%4 c. 15/2.0

d. 3 + 4 % 5 e. 3 * 7 % 4 f. 7 % 4 * 3
Exercises (cont)

EXERCISE 2
a. How are operands and operators in the following expression
associated?
x = –4 * i++ – 6 % 4;

Insert parentheses to form equivalent expressions.

b. What value will be assigned in part a to the variable x if the variable


i has a value of –2?
Exercises (cont)

EXERCISE 3
The int variable x contains the number 7. Calculate the value of the
following logical expressions:

a. x < 10 && x >= –1

b. !x && x >= 3

c. x++ == 8 || x == 7
Exercises (cont)

EXERCISE 4
What screen output does the
program generate?
Examine the results!

You might also like