Chapter3 - Introduction To Computer Programming Language
Chapter3 - Introduction To Computer Programming Language
#include <iostream>
using namespace std;
int main()
{
double radius;
double area;
return 0;
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream> allocate memory
using namespace std; for radius
area no value
// Step 1: Read in radius
radius = 20;
radius 20
int main() {
double radius; area 1256.636
double area;
print a message to the
// Step 1: Read in radius console
radius = 20;
}
10 ELEMENTARY PROGRAMMING
[Reading Multiple Input in One Statement]
#include <iostream>
int main()
// Display result
cout << "The average of " << number1 << " " <<
11
ELEMENTARY PROGRAMMING
[Identifiers]
• An identifier is a sequence of characters that consists of letters,
digits, and underscores (_).
• An identifier must start with a letter or an underscore. It cannot
start with a digit.
• An identifier cannot be a reserved word.
• An identifier can be of any length, but your C++ compiler may
impose some restriction. Use identifiers of 31 characters or
fewer to ensure portability.
12 ELEMENTARY PROGRAMMING
[Variables]
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
cout << area;
Step]
• int x = 1;
• double d = 1.4;
16
ELEMENTARY PROGRAMMING
[Named Constants]
cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(double);
20
ELEMENTARY PROGRAMMING
[Synonymous Types]
short int is synonymous to short. unsigned short int is
synonymous to unsigned short. unsigned int is synonymous to
unsigned. long int is synonymous to long. unsigned long int is
synonymous to unsigned long. For example,
short int i = 2;
is same as
short i = 2;
21
ELEMENTARY PROGRAMMING
[Numeric Literals]
A literal is a constant value that appears directly in a program.
For example, 34, 1000000, and 5.0 are literals in the following
statements:
int i = 34;
long k = 1000000;
double d = 5.0;
22
ELEMENTARY PROGRAMMING
[octal and hex literals]
By default, an integer literal is a decimal number. To denote an octal integer
literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a
leading 0x or 0X (zero x). For example, the following code displays the decimal
value 65535 for hexadecimal number FFFF and decimal value 8 for octal number
10.
cout << "1.0 / 3.0 is " << 1.0 / 3.0 << endl;
displays 1.0 / 3.0 is 0.33333333333333331
16 digits
cout << "1.0F / 3.0F is " << 1.0F / 3.0F << endl;
The float and double types are used to represent numbers with a decimal point.
Why are they called floating-point numbers? These numbers are stored into
scientific notation. When a number such as 50.534e+1 is converted into
scientific notation such as 5.0534, its decimal point is moved (i.e., floated) to a
new position.