Professional Documents
Culture Documents
Fundamentals
Lecture 5
2
Built-in Data Types
4
Data Type Storage
Valid:
Invalid :
4 bytes for the int data type, which restricts the values used to
represent integers from
-2,147,483,648 to 2,147,483,647
• Invalid
#include<iostream>
using namespace std;
int main()
{
cout << (6 + 15);
cout << “ The sum of 6 and 15 is ” << (6 + 15);
return 0;
}
14
Expression Types
Following rules are important:
• If both operands are integers, the result of the operation is an integer.
• If one operand is a real value, the result of the operation is a double-precision
value.
Examples
Answers:
19 12.12 3 3.33 3.33
18
Operator Precedence and
Associativity
Following certain rules when writing expressions containing more than
one arithmetic operator:
• Two binary operator symbols must never be placed side by side. For
example, 5 * % 6 is invalid because two operators, * and %, are
placed next to each other.
• Parentheses can be used to form groupings, and all expressions
enclosed in parentheses are evaluated first.
• When parentheses are included within parentheses, expressions in
the innermost parentheses are always evaluated first.
• Parentheses can’t be used to indicate multiplication; instead, the
multiplication operator, *, must be used. Example: Can’t write (2)(3),
instead it should be written as 2*3.
19
Precedence
• P2
Multiplication, division, and modulus operations are computed next
(evaluated left to right if multiple operators)
• P3
Addition and subtraction are computed last
(evaluated left to right if multiple operators)
20
Associativity
• 6.0 * 6 / 4 ???
The answer is 9.0 because C++’s operators use the same
associativity as in general mathematics, which evaluates
multiplication from left to right, as rule P2 indicates.
• 35 / 7 % 3 * 4??
Answer is : 5%3*4
2*4
8
Summary • 8+2*(3/2.0)+10-5*2=??
Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 22
Practice Task
Expressions Answers
21.3
10.0 +15/2 +4.3
21.8
10.0 +15.0/2 +4.3