Lec3 Operators
Lec3 Operators
Dr. Henry Xu
3
Operators
Category Examples
Arithmetic +, -, /, *, %, =, ++, --
Comparison/relational ==, !=, >, <, >=, <=
Logical !, &&, ||
Bitwise ~, &, |, ^, <<, >>
Compound assignment +=, &=, <<=, etc.
Member and pointer a[b], *, &, ->, etc.
Others ::, sizeof, etc.
Increment & decrement operators
• Increment and decrement operators: ++ and --
– k++ and ++k are equivalent to k=k+1
– k-- and --k are equivalent to k=k-1
5
An example
Old x New x Output
int x=3; 3 3
cout << x; 3 3 3
cout << x; 4 4 4
cout << x; 5 5 5
6
Precedence & associativity of operators
a = (b++) + c; /* right */
or a = b + (++c); /* wrong */
7
What values are printed?
int a=0,i=0;
cout << "i= " << i << endl;
a=0;
i=1+(a++);
cout << "i= " << i << endl;
cout << "a= " << a << endl;
a=0;
i=1+(++a);
cout << "i= " << i << endl;
cout << "a= " << a << endl;
8
i=1+(a++); i=1+(++a);
0 1
Value of a is 1 in
both cases
9
Answer
int a=0,i=0;
cout << “i= " << i << endl;
a=0;
i=1+(a++);
Output
cout << "i= " << i << endl;
cout << "a= " << a << endl; i=0
i=1
a=1
i=2
a=0; a=1
i=1+(++a);
cout << "i= " << i << endl;
cout << "a= " << a << endl;
10
Precedence & associativity of operators
Precedence: order of evaluation for different operators.
:: None
* / % Left to right
+ - Left to right
11
Assignment operator =
• Generic form
variable = expression;
12
Examples of assignment statements
/* Invalid: left hand side must be a variable */
a + 10 = b;
13
Swapping the values
• We want to swap the content of two variables, a and
b.
• What's wrong with the following program? [demo]
void main(){
int a=3, b=4;
a=b;
b=a;
}
• We need to make use of a temporary variable
c=b; /*save the old value of b*/
b=a; /*put the value of a into b*/
a=c; /*put the old value of b to a*/
14
Efficient/Compound assignment
• The generic form of efficient assignment operators:
variable op= expression;
where op is an operator. The meaning is
variable = variable op (expression);
• Efficient assignment operators include
+= -= *= /= %= (arithmetic operators)
>>= <<= &= ^= |= (bitwise operators)
• Examples:
a+=5; is same as a=a+5;
a-=5; is same as a=a-5;
a+=b*c; is same as a=a+(b*c);
a*=b+c; is same as a=a*(b+c);
• Also known as compound assignment operators
15
Basic I/O – Keyboard and Screen
• A program can do little if it can’t take input and
produce output
• Most programs read user input from keyboard and
secondary storage
• After processing the input data, result is commonly
display on screen or write to storage (disk)
Program
d ata
of
t r eam
S
16
Basic I/O – cin and cout
• C++ comes with an iostream package (library) for basic I/O.
• cin and cout are objects defined in iostream for keyboard
input and screen display respectively
• To read data from cin and write data to cout, we need to use
extraction/input operator (>>) and insertion/output operator
(<<)
18
cout: Output Operator <<
Type Expression Output
Integer cout << 21 21
Float cout << 14.5 14.5
Character cout << ‘a’; a
cout <<‘H’ << ‘i’ Hi
Bool cout << true 1
cout << false 0
String cout << “hello” hello
New line (endl) cout << ‘a’ << endl << ‘b’; a
b
Tab cout << ‘a’ << ‘\t’ << ‘b’; a b
Special characters cout << ‘\”’ << “Hello” << ‘\”’ <<endl; “Hello”
Expression int x=1; 8
cout << 3+4 +x;
19
cout – Change the width of output
• Change the width of output
– Calling member function width or using setw manipulator
– Must #include <iomanip> for setw
– Leading blanks are added to any value fewer than width
– If formatted output exceeds the width, the entire value is printed.
– Effect last for one field only
Approach Example Output (for
space)
cout.width(width) cout.width(10); 5.6
cout << 5.6 << endl; 57.68
cout.width(10);
cout <<57.68 << endl;
setw(width) cout << setw(5) << 1.8; 1.823
cout << setw(5) << 23 <<endl; 6.711
cout << setw(5) << 6.71;
cout << setw(5) << 1 <<endl;
20
cout – Set the precision and format of
floating point output
Must #include <iomanip>
Floating-point precision is six by default, i.e. 6 digits in total
Use setprecision, fixed and scientific manipulators to change the
precision value and printing format.
Effect is permanent
Default behavior
Example Output
cout << 1.34 << endl; 1.34
cout << 1.340 << endl; 1.34
cout << 1.3401234 << endl; 1.34012
cout << 0.0000000134 << endl; 1.34e-008
21
fixed and scientific manipulators
fixed: always uses the fixed point notation
scientific: always uses the scientific notation
They change the meaning of precision (see the example)
Example Output
22
cout setprecision
• Normally, setprecision(n) means output n significant
digits in total
• But with “fixed” or “scientific”, setprecision(n) means
output n significant digits after the decimal points
Example Output
cout << setprecision(2); 1.3
cout << 1.34 <<endl; 1.3e-08
cout << 0.0000000134 << endl; 0.00
cout <<fixed; 5.00e-004
cout << 0.0000000134 << endl;
cout << scientific << 0.0005 << endl;
23
cout – Other Manipulators
Manipulators Example Output
fill cout << setfill(‘*’); *******5.6
cout << setw(10); *****57.68
cout << 5.6 << endl;
cout << setw(10);
cout <<57.68 << endl;
radix cout << oct << 11 << endl; // octal 13
cout << hex << 11 << endl; // hexidecimal b
cout << dec << 11 << endl; 11
24
cin: Extraction Operators (>>)
Preprogrammed for all standard C++ data types
Get bytes from an input stream object
Depend on white space to separate incoming data values
25
Input Operator
Type Variable Expression Input x y
Integer int x,y; cin >> x; 21 21
cin >> x >> y; 53 5 3
26
Programming styles
• Programmers should write code that is understandable
to other people as well
• Meaningful variable names
• Which is more meaningful
– tax=temp1*temp2; // not meaningful
– tax=price*tax_rate; // good
• Meaningful Comments
– Write comments as you write the program
• Indentation
27
Indentation styles
void main() void main() {
{ int x, y;
int x, y; x = y++;
x = y++; }
}
void main()
{
int x, y; BAD!! Avoid this!!
x= y++;}
28
Style is important
Both are
goo
one and s d. Choose
tick with
it.
A vo id this!!
BAD!!
29
Use of comments
• Top of the program
– Include information such as the name of organization,
programmer’s name, date and purpose of program
• What is achieved by the function, the meaning of the arguments
and the return value of the function
• Short comments should occur to the right of the statements when
the effect of the statement is not obvious and you want to
illuminate what the program is doing
30