If relational operators are associated, precedence is implemented from left to right.. If equality operators are associated, precedence is implemented from left to right.. Confus
Trang 3are binary operators
(take two operands).
Trang 4The Hashemite University 4
Arithmetic Operators II (1)
Operand 1
data type Operand 2 data type Temporary result Holder data type Saved Result
Result = number1 / number2
Trang 5Arithmetic Operators III
applied for integer values only So, 9%4 = 1, but
9%2.5 Syntax Error.
priority which are higher than + and – (+ and –
the same priority you start implementation from left to right.
inner parenthesis pair.
from left to right.
Trang 6cout <<"(a): "<< d/a << endl;
cout <<"(b): "<< d/e << endl;
Trang 7Example Output
Trang 8Assignment Operator I
to what present on the left hand side.
allowed where implementation starts from right to left, e.g x = y = u = 10;
6 = x;
x = y + 10 = 9; // you cannot use arithmetic
operators between multiple assignments.
Trang 9Assignment Operator II
Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the
addition assignment operator
Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
Examples of other assignment operators include:
Trang 10Assignment Operator III
Note that the priority of the
assignment operators, i.e +=, /=,
etc., is different from the operators alone, i.e +, /, etc.
Also, they are associated from right
to left.
int d = a+=b*=c;
Trang 11Equality and Relational
Operators I
Greater than >
Less than <
Greater than or equal >=
Less than or equal <=
Equal to ==
Not equal to !=
operators are binary
operators, used for
comparison between two
operands.
“false”, i.e boolean data type.
control structures, e.g
conditional if.
x>y Is x greater than y?
x<y Is x less than y?
x>=y Is x greater than or equal to
Trang 12Equality and Relational
Operators II
Relational operators have the same priority which is higher than the equality operators.
If relational operators are associated,
precedence is implemented from left to
right
If equality operators are associated,
precedence is implemented from left to
right.
Again, parenthesis forces priority.
Trang 13Example Output
Trang 15Confusing Equality (==) and Assignment (=) Operators I
not ordinarily cause syntax errors.
Recall that any expression that produces a value can
be used in control structures Nonzero values are
true, and zero values are false
if ( payCode == 4 ) cout << "You get a bonus!" << endl;
Checks the paycode, and if it is 4 then a bonus is
awarded
if ( payCode = 4 ) cout << "You get a bonus!" << endl;
Sets paycode to 4
4 is nonzero, so the expression is true and a bonus is
awarded, regardless of paycode.
Trang 16Confusing Equality (==) and Assignment (=) Operators II
lvalues or l-values
What can appear on the left side of an equation
Their values can be changed
Variable names are a common example (as in x = 4;)
Constants cannot be used as l-values (i.e you cannot write
4 = x;)
Also, expressions cannot be used as l-values, e.g.: x+5 =
10 Syntax error (also x+5=y; gives error)
Trang 17Logical Operators I
Logical operators allows the programmer to combine more than one condition with each other to form more complex conditions
&& (logical AND)
of a condition).
Logical operators used as conditions in loops, e.g for and while, and conditional statements, e.g if/else
Trang 18
Logical Operators II
Truth tables:
true false false
false true false
false false false
true false true false true true false false false
true false false true
Trang 19Logical Operators III
to Boolean values only, if other types are given as operands implicit casting will work to convert them to Boolean.
Examples:
int a=9; int b=1; int d=0;bool c;
c= a&&b; cout<<c; // will print 1
c= a>=b||b!=b;
cout<<c; // will print 1 because the expression gives true
c = !(a&&b<d) // will print 1
Trang 20Logical Operators IV
Operators precedence:
! has the highest priority followed by && and then || (i.e || has the lowest priority).
When any of these operators are
combined, implementation starts from
left to right.
Example:
bool a=true;bool b=false; bool c=false;
bool result = a&&!c ||a&&a
cout<<result; // will print 1
Trang 22Increment & Decrement
When the operator is used before the variable (++c or c)
Variable is changed, then the expression it is in is
evaluated
Post-increment/decrement
When the operator is used after the variable (c++ or c )
Expression the variable is in executes, then the variable is changed
E.g.: If c = 5, then
executed)
Trang 23Increment & Decrement II
When Variable is not in an expression
Preincrementing and postincrementing have the same
cout<<b; \\ will print 9
++ and cannot be applied to expressions:
int x = 0;
cout << ++ (x + 100); //Error
Trang 24Increment & Decrement III
Post-increment and post-decrement has higher priority than pre-
decrement and pre-increment.
Post-increment and post-decrement associates from left to right.
Pre-increment and pre-decrement associates from right to left.
Trang 25Bitwise Operators I
i.e on the binary representation of the data, not on entire expressions as in logical
operators.
| OR individual bits
^ XOR individual bits
~ NOT of individual bits (or
complement operator, computes the one’s complement)
Trang 26Bitwise Operators II
Also, it has an assignment operator abbreviation
form
i.e x = x &y x &= y,
and so for all operators listed in the previous table, i.e we have >>=, <<=, |=, etc.
except the complement operator ~ has no
assignment operator.
Note that the shift operators <<, >> are the same as extraction and insertion operators This is an example
of what so called “Operators Overloading” where the
operator function depends on the used operands.
A^B = (~A&B) | (A&~B), which is called XOR function
or gate in digital logic.
Using && instead of & and || instead of | and ! Instead
of ~ as bitwise operators are logical errors not syntax.
Trang 27Bitwise Operators III
The right operand of the shift operators must be:
Integer
Positive
Less than the number of bits used by the left operand
If the right operand of the shift operator is –ve
logical error, the output is unexpected (incorrect
error, the output is unexpected (incorrect output).
Trang 29Bitwise Operators V
shift operators (both left and right shift), then & operator, ^ operator, and finally the
| operator which has the lowest priority.
which is associated from right to left.
version, i.e &=, |=, etc., is different from the operators alone, i.e &, |, etc Also, they are associated from right to left.
Trang 30Left Shift bitwise operation
Int numbers are saved in 2 bytes (16) bit or 4 bytes (32) bit
Shifting a number to left will move all bits one location to the left
Example , perform the following operation 157 << 3
Trang 31Right Shift bitwise operation
Int numbers are saved in 2 bytes (16) bit or 4 bytes (32) bit
Shifting a number to right will move all bits one location to the right
Example , perform the following operation 157 >> 3
Trang 32Example on bitwise operations
int main()
{ unsigned short int x = 23,y=11;unsigned short int z;
z=x&y;
cout<<"the result of x&y is "<<z<<endl;
z=x|y;
cout<<"the result of x|y is "<<z<<endl;
z=x^y;
cout<<"the result of x^y is "<<z<<endl;
return 0;
}
1) Remember that the first step is to convert the number into binary 23 = 10111, 11 = 1011
10111 10111 10111
& | ^
01011 01011 01011
00011 11111 11100
2) Perform the
operation
Trang 34‘sizeof’ Operator I
It is a unary operator, i.e Unary operators
operate on only one operand, which could be:
A constant value (e.g 10, 300, ‘c’, etc.).
A variable name (e.g a, number1)
A data type name (e.g int, double, etc.)
Return the size in bytes of the input variable
or data type as a size_t value (which is
usually unsigned integer).
Parenthesis are required after it only when
its operand is a data type name, otherwise these parenthesis are optional (but in this but in this
case leave a single white space after sizeof operator ).
Trang 35cout << sizeof kk; //output is 4
cout << sizeof 100; //output is 4 (taken as integer)
cout << sizeof 1.2; //output is 8 (taken as double)
cout << sizeof ‘z’; //output is 1
cout << sizeof “hello”; //output is 6 (the size of the string in addition to the ‘\0’ chracter)
Trang 36Comma Operator
Accept two operands or expressions on either side (i.e binary operator).
Implementation starts from left.
The value of the entire expression will be the value of the
right most expression.
It has the least priority among all operators.
It is called also sequential evaluation operator
E.g; (consider each operation to be executed independently)
int x=0;int y=0;int z=0;
cout << (x = 5, y = 8); //will print 8
y = (z, x = 7, x + 8); //y = 15
cout << x = 5, y = 8; // syntax error
What is the value of y for this expression
Trang 37Operators Precedence I
2
() []
++
Grouping operator Array access
Post-increment Post-decrement
left to right
3
!
~ ++
- +
*
&
(type) sizeof
Logical negation Bitwise complement Pre-increment
Pre-decrement
Unary minus Unary plus Dereference Address of
Cast to a given type Return size in bytes
right to left
Trang 38Operators Precedence II
%
Multiplication Division
Modulus left to right
5 +- AdditionSubtraction left to right
6 <<>> Bitwise shift leftBitwise shift right left to right
Comparison greater-than-or-equal-to
left to right
Trang 39Operators Precedence III
8 ==!= Comparison equal-toComparison not-equal-to left to right
9 & Bitwise AND left to right
10 ^ Bitwise exclusive OR left to right
11 | Bitwise inclusive (normal) OR left to right
12 && Logical AND left to right
13 || Logical OR left to right
14 ? : Ternary conditional (if-then-else) right to left
Trang 40Operators Precedence IV
15
= +=
Bitwise shift right and assign
right to left
16 , Sequential evaluation operator or comma left to right
Trang 41Operators Precedence
Notes
of the textbook.
associatively of the different operators.
and its effect are compiler dependent.
Trang 42Operators Precedence
Examples
this is a one program):
int x = 5, y = 9, u = 30, z;
z = ++x – (y-=3) + (u*=2) + u&y; //z = 0
After this step x=6, y=6 , u = 60
z = y && x || u^y || ~x; // z = 1
z = y > x + u == y; // z = 0
z = u / x % y * 2 + y / 4.0 - x ; // z = 3
(without parenthesis) the compiler will give
you a syntax error, why?
z = ++x – y-=3 + u*=2 + u&y; // two syntax errors
Trang 43Escape Sequences
An escape sequence begins
with a \ (backslash or called
escape character) followed by
an alphanumeric character
Note that the two characters of
an escape sequence are
construed as a single character
and indicates a special output
on the screen
Also, it is used to allow the
usage of some characters
within a character constant
which are reserved for C++
(e.g \, “)
Note that the escape sequence
is considered as one character
by the compiler So, writing
both of the following is correct:
cout << “\n”;
Or
Escape Sequence Meaning
\b Backspace (place the cursor one
character space back not deleting
characters).
\r Carriage return (place the cursor at the
beginning of the current line not a new one)
\0 Null character (used in strings)
Trang 44cout << "Jordan\b\b " << endl;
cout << "Jordan\b\b" << endl;
cout << "Jordan\b\byy" << endl;
cout << "\"Jordan\"" << endl;
cout << "\\Jordan\\" << endl;
cout << "Hello" << "\r" << "Bye" << endl;
Trang 45Example Output
Trang 46 Concatenating or cascading or chaining of stream insertion
operators: output many values using one cout and multiple
insertion operators
The evaluation of the cascaded expressions starts from right to left but the printing on the screen starts from left to right
cout << x <<“\t”<< ++x << “\t”<< x++ << “\t” << x << endl;
Trang 47cout function (2)
int x = 10; int z=10;
cout << x <<"\t"<< x+1 << "\t"<< x+3 << "\t" << x << endl; cout << z <<"\t"<< (z+=1) << "\t"<< (z+=3) << "\t" << z << endl;
modified during the cout statement
Trang 48cin Function
Istream class.
Tied to the standard input device (keyboard).
When reading a string cin will stop at the first white space encountered in the string or when you press enter.
int x=0;
cin>>x; // try to enter 5 6
cout<<x; // will print 5 only
Cascaded extraction operator can be applied to read
more than one variable from the keyboard using one
statement (after entering each variable value press
Trang 49Additional Notes
material from the textbook: