Tải bản đầy đủ (.ppt) (49 trang)

C++ lecture 4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (249.73 KB, 49 trang )

C++ Programming Language
Lecture 4

C++ Basics – Part II
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline












Arithmetic operators.
Logical operators.
Assignment operator.
Increment and decrement operators.
Bitwise Operators.
Relational Operators.
‘Size of’ operator.
Comma operator.


Operators Precedence.
cout and cin cascading.
Escape Sequences.
The Hashemite University

2


Arithmetic Operators I


Arithmetic operators:










Addition +
Subtraction Division /
Multiplication *
Modulus %

Arithmetic operators
are binary operators
(take two operands).

Result of / (division)
depends on the
operands types.



Division Result
Examples:
E.g:
E.g:
int a= 5;
int b = 2;
double c = a/b;
c= 2

double a= 5;
double b = 2;
double c = a/b;
c = 2.5

E.g:
double a= 5;
double b = 2;
int c = a/b; c = 2

E.g:
double c = 5/2;
c= 2

E.g:

double c = 5.0/2;
c = 2.5

E.g:
double c = 5/2.0;
c = 2.5

The Hashemite University

3


Arithmetic Operators II (1)
Result = number1 / number2



Division Operator General Rules:

Operand 1
data type

Operand 2
data type

Temporary
result

Holder
data type


Saved
Result

int

int

integer

int

integer

int

int

integer

double/float

integer

int

double/float

Floating
point


int

integer

int

double/float

Floating
point

double/float

Floating
point

double/float

int

Floating
point

int

integer

double/float


int

Floating
point

double/float

Floating
point

double/float

Hashemite University
double/float The
Floating
int

integer

4


Arithmetic Operators III












% (modulus which finds the remainder) is
applied for integer values only. So, 9%4 = 1, but
9%2.5  Syntax Error.
Operators precedence: *, %, and / have the same
priority which are higher than + and – (+ and –
have the same priority). double c= 5-6 * 4 /3 %3+7/3 , c= ??
If multiple operators are combined which have
the same priority you start implementation from
left to right.
Remember that parenthesis forces priority.
For nested parenthesis you start with the most
inner parenthesis pair.
For multiple parenthesis start implementation
from left to right.
The Hashemite University

5


Example
#include <iostream.h>
int main()
{
int a = 9, b = 30, c = 89, d =
35, x;
double e = 3, y;

cout <<"(a): "<< d/a << endl;
cout <<"(b): "<< d/e << endl;
x = d/a;
y = d/a;
cout <<"(c):
cout <<"(d):
x = d/e;
y = d/e;
cout <<"(e):
cout <<"(f):

"<< x << endl;
"<< y << endl;

x = d*a + c%b - b + d/e;
y = d*a + c%b - b + d/e;
cout <<"(g): "<< d*a + c%b - b + d/e <<
endl;
cout <<"(h): "<< x << endl;
cout <<"(i): "<< y << endl;
x = d*a + c%b - (b + d/e);
y = d*a + c%b - (b + d/e);
cout <<"(j): "<< d*a + c%b - (b + d/e) <<
endl;
cout <<"(k): "<< x << endl;
cout <<"(l): "<< y << endl;
return 0;

"<< x << endl;
"<< y << endl;


}
The Hashemite University

6


Example Output

The Hashemite University

7


Assignment Operator I







= assigns the value on the right hand side
to what present on the left hand side.
E.g: int x = 6;
Multiple assignments at the same time are
allowed where implementation starts from
right to left, e.g. x = y = u = 10;
Common errors:
6 = x;

x = y + 10 = 9; // you cannot use arithmetic
operators between multiple assignments.
The Hashemite University

8


Assignment 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:
d
n
e
f
g




-=
+=
*=
/=
%=

4
4
5
3
9

(d
(n
(e
(f
(g

=
=
=
=
=

d
n
e
f

g

+
*
/
%

4)
4)
5)
3)
9)

Assignment operators associate from right to left.
The Hashemite University

9


Assignment 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.
Ex. int a=3,b=7,c=11;
int d = a+=b*=c;
The Hashemite University

10


Equality and Relational
Operators I


Relational Operators:







Equality operators:










Operator

Greater than >
Less than <
Greater than or equal >=
Less than or equal <=
Equal to ==
Not equal to !=

Equality and relational
operators are binary
operators, used for
comparison between two
operands.
Their result is either “true” or
“false”, i.e. boolean data type.
Very useful for conditional
control structures, e.g.
conditional if.

Operation Performed

x>y

Is x greater than y?

x
Is x less than y?


x>=y

Is x greater than or equal to
y?

x<=y

Is x less than or equal to y?

x==y

Is x equal to y?

x!=y

Is x not equal to y?

The Hashemite University

11


Equality 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.
The Hashemite University

12


Example Output

The Hashemite University

13


Example
#include <iostream.h>
int main()
{
int a = 9, b = 30, c = 89, d = 35;
bool e, f;

e = a > b;
cout <<"(a): "<< e << endl;
f = d == b;
e = d == d;
cout <<"(b): "<< f << endl;
cout <<"(c): "<< e << endl;
e = c != d;
cout <<"(d): "<< e << endl;
e = a>=b == d;
cout<<"(e): "<f = a<=b!=a>=d;
cout <<"(f): "<return 0;
}

The Hashemite University

14


Confusing Equality (==) and
Assignment (=) Operators I


These errors are damaging because they do
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

Example:
if ( payCode == 4 )
cout << "You get a bonus!" << endl;





Checks the paycode, and if it is 4 then a bonus is
awarded

If == was replaced with =
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.
The Hashemite University
15



Confusing 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)

rvalues or r-values



What can appear on the right side of an equation
Can be:







Constants, such as numbers
Variables
Or expressions (y=x+5)

lvalues can be used as rvalues, but not vice versa
The Hashemite University

16


Logical Operators I




Logical operators allows the programmer to combine more
than one condition with each other to form more complex
conditions.
&& (logical AND)





|| (logical OR)






It is a binary operator.
Returns true if either of its conditions is true.

! (logical NOT or logical negation)







It is a binary operator.
Returns true if both conditions are true.

Reverses the truth/falsity of its condition (reverse the meaning
of a condition).
Returns true when its condition is false.
It is a unary operator, only takes one condition.

Logical operators used as conditions in loops, e.g. for and
while, and conditional statements, e.g. if/else.
The Hashemite University

17



Logical Operators II


Truth tables:

A

B

A && B

A

B

A ||B

true

true

true

true

true

true

true


false

false

true

false

true

false

true

false

false

true

true

false

false

false

false


false

false

A

!A

true

false

false

true

The Hashemite University

18


Logical Operators III




Note that logical operators are applied
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= a>=b||b!=b;
cout<c = !(a&&b
The Hashemite University

19


Logical 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<The Hashemite University

20


Logical Operators -Examples
Math

C++

10 < x < 100

(x > 10) && (x <
100)
!(x == 90) or
x != 90
(x < 0) || (x > 40)

x ≠ 90
x < 0 or x >
40

The Hashemite University

21


Increment & Decrement

Operators I






Increment operator (++) - can be used instead of c += 1 (unary
operator)
Decrement operator (--) - can be used instead of c -= 1 (unary
operator)
 Pre-increment/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
 cout << ++c; prints out 6 (c is changed before cout is
executed)
 cout << c++; prints out 5 (cout is executed before the
increment. c now has the value of 6)
The Hashemite University

22


Increment & Decrement II



When Variable is not in an expression


Preincrementing and postincrementing have the same
effect.

and

++c;
cout << c;
c++;
cout << c;

have the same effect
 Int a=9;int b=0;



b = a++;
cout<++ and -- cannot be applied to expressions:
int x = 0;
cout << ++ (x + 100); //Error
The Hashemite University

23



Increment & Decrement III






Post-increment and post-decrement
has higher priority than predecrement and pre-increment.
Post-increment and post-decrement
associates from left to right.
Pre-increment and pre-decrement
associates from right to left.
The Hashemite University

24


Bitwise Operators I


Operates on individual bits of the operands,
i.e. on the binary representation of the data,
not on entire expressions as in logical
operators.
Bitwise Operator Symbol

&

Function

AND individual bits

|

OR individual bits

^

XOR individual bits

~

NOT of individual bits (or
complement operator, computes
the one’s complement)

>>

Right-shift operator

<<

Left-shift operator
The Hashemite University

25


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×