Transcript 5-operators

STRUCTURED
PROGRAMMING
C++ Operators
Content
2

C++ operators

Assignment operators

Arithmetic operators

Increment and decrement operators

Decision making operators (logical and relational)

Conditional operator

Precedence and associativity of operators

Common errors
Objectives
3

By the end you should be able to:

Use the assignments and arithmetic operators

How decisions are made in your programs

Write simple decision –making statements

Recognize the precedence and associatively of operators

Identify and use the C++ conditional operator (?:)

Identify and use Increment & decrement operators

Use operators in output statements

Properly mix data types in expression and calculations
Operators
4

Data connectors within expression or equation

Concept related


Operand: data that operator connects and processes

Resultant: answer when operation is completed
Operators types based on their mission

Assignment

Arithmetic: addition, subtraction, modulo division, ...etc

Relational: equal to, less than, grater than, …etc

Logical (decision-making): NOT, AND, OR
Operators (cont.)
5

Operators types based on number of operands



Unary operators

Have only one operand

May be prefix or postfix

e.g. -- ++ !
Binary operators

Have two operands

Infix

e.g. == && +
Ternary operators

Have three operands

e.g. ? :
Assignment operators
6

Assignment statement takes the form below
varName = expression;

Binary operators

Expression is evaluated and its value is assigned to the variable on
the left side

Shorthand notation

varName = varName operator expression;
c = c + 3;

varName operator = expression;
c += 3;
Assignment operators (cont.)
7

Assignment between objects of the same type is always supported
Assignment
operator
Sample
expression
Explnation
Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+=
c += 7
-=
10 to c
d -= 4
c=c+7
d=d–4
*=
e *= 5
e=e*5
20 to e
/=
f /= 3
f=f /3
2 to f
%=
g %= 9
g=g%9
3 to g
1 to d
Arithmetic Operators
8

All of them are binary operators
C++ operation
Algebraic
expression
C++ expression
Addition
C++ arithmetic
operator
+
f+7
f + 7
Subtraction
-
p-c
p - c
MUltiplication
*
bm or b . m
b * m
Division
/
x / y or x ÷ y x / y
Modulus
%
r mod s
r % s

Arithmetic expressions appear in straight-line form

Parentheses () are used to maintain priority of manipulation
Arithmetic Operators Precedence
9

Operators in parentheses evaluated first

Nested/embedded parentheses


Multiplication, division, modulus applied next


Operators in innermost pair first
Operators applied from left to right
Addition, subtraction applied last

Operators applied from left to right
Example
10

The statement is written in algebra as
z = pr % q + w / (x – y)

How can we write and evaluate the previous statement in C++ ?
z
=
6
p
*
2
r
%
3
q
+
5
w
/
4
(x
-
1
y);
Increment and Decrement Operators
11

Unary operators


Adding 1 to or (subtracting 1 from) variable’s value
Increment operator gives the same result of
(c=c+1) or (c+=1)

Decrement operator gives the same result of
(c=c-1) or (c-=1)
Increment and Decrement Operators (cont.)
12
Operator
Called
Sample
expression
Explanation
++
Preincrement
++a
Increment a by 1, then use the new value of
a in the expression in which a resides.
++
Postincrement
a++
Use the current value of a in the expression
in which a resides, then increment a by 1.

Pridecrement
--b
Decrement b by 1, then use the new value of
b in the expression in which b resides.

Postdecrement
b--
Use the current value of b in the expression
in which b resides, decrement b by 1.
Examples
13
Example # 1
int
x = -10 , y;
output # 1
y = ++x;
cout << “x = “ << x << endl;
cout << “y = “ << y << endl;
x = -9
y = -9
Example # 2
int
x = -10 , y;
y = x++;
cout << “x = “ << x << endl;
cout << “y = “ << y << endl;
output # 2
x = -9
y = -10
Relational and Equality Operators
14

Binary operators

Used in decision -making statements
Standard algebraic
equality operator or
relational operator
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
>
>
x > y
x is greater than y
<
<
x < y
x is less than y

>=
x >= y
x is greater than or equal to y

<=
x <= y
x is less than or equal to y
=
==
x == y
x is equal to y

!=
x != y
x is not equal to y
Relational operators
Equality operators
Relational and Equality Operators (cont.)
15

Have the same level of precedence

Applied from left to right

Used with conditions

Return the value true or false

Used only with a single condition
Logical Operators
16

Used to combine between multiple conditions

&& (logical AND)

true if both conditions are true
1st condition
gender == 1

2nd condition
&&
age >= 65
|| (logical OR)

true if either of condition is true
semesterAverage >= 90 || finalExam >= 90
Logical Operators (cont.)
17

! (logical NOT, logical negation)

Returns true when its condition is false, and vice versa
!( grade == sentinelValue )
Also can be written as
grade != sentinelValue
Conditional operator (?:)
18


Ternary operator requires three operands

Condition

Value when condition is true

Value when condition is false
Syntax
Condition ? condition’s true value : condition’s false value
Examples
19
Example # 1
grade >= 60 ? cout<<“Passed” : cout<<“Failed”;
Can be written as
cout << (grade >= 60 ? “Passed” : “Failed”);
Example # 2
int i = 1, j = 2, Max;
Max = ( i > j ? i : j );
Can be written as .. ?
20
Summary of Operator Precedence
and Associativity
Operators
Associativity
Type
()
[]
left to right
highest
++
--
static_cast< type >( operand )
left to right
unary (postfix)
++
--
+
right to left
unary (prefix)
*
/
%
left to right
multiplicative
+
-
left to right
additive
<<
>>
left to right
insertion/extraction
<
<=
left to right
relational
==
!=
left to right
equality
&&
left to right
logical AND
||
left to right
logical OR
?:
right to left
conditional
right to left
assignment
left to right
comma
=
,
+=
>
-=
-
!
&
>=
*=
/=
%=
*
bool Variables in Expressions
21

false is zero and true is any non-zero

The following codes applies implicit conversion between bool and
int
Code # 1
int
bool
x = -10 ;
flag = x ;
// true
bool
int
test1
int
int
a = flag ;
// assign the value 1
b = !flag;
// assign the value 0
x = flag + 3;
// assign the value 4
Code # 2
test1,test2,test3 ;
x = 3 , y = 6 , z = 4 ;
= x >
y ;
// false
test2 = !(x == y );
// true
test3 = x < y && x < z ;
// true
test3 = test1 || test2 ;
// true
test2 = !test1;
// true
Common Compilation Errors
22

Attempt to use % with non-integer operands

Spaces between pair of symbols e.g. (==, !=, …etc)

Reversing order of pair of symbols e.g. =!

Confusing between equality (==) and assignment operator (=)
Exercise - 1
23
What is the output of the following program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x;
int y;
x = 30;
y = 2;
int z;
z = 0;
cout << (++++x && z ) << endl;
cout << x * y + 9 / 3 << endl;
cout << x << y << z++ << endl;
return 0;
} // end main
Exercise - 2
24
What is wrong with the following program?
1
2
3
4
5
6
int main()
{
int a,b,c,sum;
sum = a + b + c ;
return 0;
}
Included Sections
25
Chapter 2: from section 6 and 7
Chapter 4: section 11 and 12