Main
Main
Chapter 3
1
Overview of C++ Statements
C++ Statements
Objects
Operations
2
Expressions
In a C++ program, any finite sequence of objects
and operations that combine to produce a value
is called an expression.
Examples from our temperature problem:
1.8
1.8 * celsius
1.8 * celsius + 32 Even:
fahrenheit = 1.8 * celsius + 32 1.8;
32;
Aside #1: expression; is a statement.) "Hello";
1 + 1;
Aside #2: expressions may be empty ;;;;
Object Categories
See Footnote
There are three kinds of objects: on p. 48
4
5 basic
types Literals
Some Variations:
unsigned, long, short
– int literals are integers:
Variations:
-27, 0, 4, +4 float, long double
5
Variable Declarations
Variables are used to store values, and can be either
uninitialized or initialized. They must be declared
before they are used.
Declare
Examples: only once
int idNumber;
int age = 18;
Not a
double celsius,
declaration kelvin = 0;
cin >> celsius;
double fahrenheit = 1.8 * celsius + 32;
char letterGrade = 'A';
Allocates a memory
bool ok, done = false; location for values of
this type and associates
Pattern:
its address with name
type name;
type name = expression;
6
Assignment Statements
The value of a variable can be changed during
execution by an assignment statement.
Examples:
age = 19;
celsius = 1.1 * celsius;
letterGrade = 'B'; Changes value in
name's memory
location to value of
done = true; expression
12
int Objects
Three forms:
– decimal (base-10): 0 or begin with a nonzero
digit or sign (-45, -2, 0, +21, 36, 65536, ...)
– octal (base-8): begin with a 0 followed by octal digits
(01, 02, 03, 04, 05, 06, 07,
See second
item on 010 , 011 , 012, ..., 017,
Other
Course 020, 021, …)
Information
class page
– hexadecimal (base-16): begin with 0x
followed by digits with a, b, c, d, e, f = 10, 11,
12, 13, 14, 15 (0x1, 0x2, ..., 0x7, 0x8, 0x9,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0x10, 0x11, ...) 13
unsigned Objects
For objects whose values are never negative,
C++ provides the unsigned type.
They also may be expressed in decimal, octal,
and hexadecimal forms.
The major advantage of using unsigned
instead of int when values being processed
are known to be nonnegative is that larger
values can be stored.
14
Ranges of Integer Values
INT_MIN INT_MAX
#include <iostream> 2
using namespace std; 20
200
int main() 2000
{ 20000
int number = 2; 200000
2000000
for (int i = 1; i <= 15; i = i + 1) 20000000
{ 200000000
cout << number << '\n'; 2000000000
number = 10 * number; overflow -1474836480
} -1863462912
} -1454759936
-1662697472
552894464
16
Why this strange
behavior?
Check INT_MAX + 1,
INT_MAX + 2, . . .
#include <iostream>
#include <climits>
using namespace std;
int main()
{
Output:
int number = INT_MAX - 3;
2147483644
for (int i = 1; i <= 7; i = i + 1)
2147483645
{
2147483646
cout << number << '\n';
INT_MAX 2147483647
number = number + 1;
INT_MIN -2147483648
}
-2147483647
}
-2147483646
Arithmetic is modular
INT_MIN . . . INT_MAX
17
Sec. 3.3
int Representation
Integers are often represented internally in the
twos-complement format, where the high-
order (leftmost) bit indicates the number’s sign:
210 = 00000000000000102
110 = 00000000000000012
010 = 00000000000000002
-110 = 11111111111111112
-210 = 11111111111111102
Add 1
(1111111111110100)
Simpler: Flip all bits to the left of rightmost 1.
19
Why Use Two's Complement?
Because usual algorithms for +, * work!
* 0 1
+ 0 1
0 0 0
0 0 1
1 0 1
1 1 10
5 + 7:
111 carry bits
0000000000000101
+0000000000000111
0000000000001100
5 + –6:
0000000000000101
+1111111111111010
1111111111111111
20
double Objects See Ch. 3,
pp. 59-61
(11 bits)
0 10000000001 01110000000000000000000000000000000000000000000000000
sign mantissa
float (single precision):
(1 bit) bias = 127; exponent: 8 bits (52 bits) 21
mantissa: 23 bits
Problem: Binary representations of most real numbers
do not terminate; e.g.,
13.1 = 1101.00011001100110011…
So they can't be stored exactly; error in this
representation is called roundoff error.
Execution:
//-- Effect of roundoff error
0 0
0.1 0.104873
#include <iostream>
0.2 0.218968
#include <cmath>
. . .
using namespace std;
22.6 14.7859
22.7001 13.4103
int main()
22.8001 11.9884
{
. . .
for (double x = 0; x != 50.0; x = x + 0.1)
49.9998 42.937
{
50.0998 45.7826
doouble y = x * sqrt(1 + sin(x));
50.1998 48.5246
cout << x << " " << y << '\n';
. . .
//if (x > 60) break;
100.099 76.3241
}
. . .
}
Patriot missile failure
22
See Other Course Information
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
int main()
{
cout << "John Doe CPSC 104X -- Lab 1\n\n";
cout << "** Convert Celsius temps to Fahrenheit **\n";
double celsius = 0,
kelvin = 0;
double fahrenheit = 1.8 * celsius + 32;
B
Enter number (-999 to stop): 1
00000000000000000000000000000001
C
Enter number (-99 to stop): 5.75
01000000101110000000000000000000
D
INT_MAX = 2147483647
INT_MIN = -2147483648
UINT_MAX = 4294967295
SHRT_MAX = 32767
SHRT_MIN = -32768
LONG_MAX = 2147483647
#include <iostream> LONG_MIN = -2147483648
#include <climits> CHAR_MAX = 127
#include <cfloat> CHAR_MIN = -128
using namespace std; FLT_MAX = 3.40282e+038
FLT_MIN = 1.17549e-038
int main() DBL_MAX = 1.79769e+308
{ DBL_MIN = 2.22507e-308
cout << "INT_MAX = " << INT_MAX << endl LDBL_MAX = 1.79769e+308
<< "INT_MIN = " << INT_MIN << endl LDBL_MIN = 2.22507e-308
<< "UINT_MAX = " << UINT_MAX << endl FLT_EPSILON = 1.19209e-007
<< "SHRT_MAX = " << SHRT_MAX << endl DBL_EPSILON = 2.22045e-016
<< "SHRT_MIN = " << SHRT_MIN << endl LDBL_EPSILON = 2.22045e-016
<< "LONG_MAX = " << LONG_MAX << endl
<< "LONG_MIN = " << LONG_MIN << endl
<< "CHAR_MAX = " << CHAR_MAX << endl
<< "CHAR_MIN = " << CHAR_MIN << endl
<< "FLT_MAX = " << FLT_MAX << endl
<< "FLT_MIN = " << FLT_MIN << endl
<< "DBL_MAX = " << DBL_MAX << endl
<< "DBL_MIN = " << DBL_MIN << endl
<< "LDBL_MAX = " << LDBL_MAX << endl
<< "LDBL_MIN = " << LDBL_MIN << endl
<< "FLT_EPSILON = " << FLT_EPSILON << endl
<< "DBL_EPSILON = " << DBL_EPSILON << endl
<< "LDBL_EPSILON = " << LDBL_EPSILON << endl;
}
E
#include <iostream> 0 0
#include <cmath> 0.01 0.0100499
0.02 0.020199
using namespace std; 0.03 0.0304466
0.04 0.0407919
int main() 0.05 0.0512342
{ 0.06 0.0617727
0.07 0.0724066
for (double x = 0; x != 50.0; x = x + .01) 0.08 0.0831352
{ 0.09 0.0939575
double y = x * sqrt(1 + sin(x)); 0.1 0.104873
0.11 0.115881
cout << x << " " << y << endl; 0.12 0.12698
} .
.
} .
4.33 1.16366
4.34 1.1362
4.35 1.10858
4.36 1.08078
4.37001 1.05283
4.38001 1.0247
.
.
.
49.9576 41.7073
49.9676 42.0003
49.9776 42.2923
49.9876 42.5834
49.9976 42.8735
50.0076 43.1627
50.0176 43.4509
50.0276 43.7381
50.0376 44.0244
50.0476 44.3096
50.0576 44.5939
50.0676 44.8772
.
.
. F