Tom Kelliher, CS17
Feb. 16, 1996
Characters which can be used in C++ programs ( character set):
Case sensitivity: birthyear vs. birthYear
A language element that can be used in forming higher-level language constructs.
A sequence of characters treated as one ``symbol''
Token types:
``Pre-Declared,'' unchangeable meaning
Examples: if, else, while, do signed int float
User-Defined words
Represent:
Identifiers should make plain what they represent:
Poor names: x y z
Minimal rules for constructing an identifier:
_, upper-, lower-case letters
Examples:
this_is_a_very_long_name
'\n', '\0',
'\n', '2', '\'', '\\ '
"Hi, I'm a string!\n",
"embedded quote:\", oh my!"
"Z" vs. 'Z'
Separate syntactical units.
Example:
tax = grossPay * 0.28; netPay = grossPay - tax;
Operate on Operands
Types:
/**********************************************************************
* Tom Kelliher
* Feb. 16, 1996
*
* This is just a short demonstration program. It computes the area
* of circles.
**********************************************************************/
#include <iostream.h> // library file
const float PI = 3.14159; // a symbolic constant
float area(float radius); // function declaration
int main()
{
float radius;
cout << "Enter a radius of 0.0 to exit" << endl;
cout << "Radius: ";
cin >> radius;
while (radius > 0.0)
{
cout << "The area is " << area(radius) << endl << endl;
cout << "Radius: ";
cin >> radius;
}
return 0;
}
/**********************************************************************
* area --- compute the area of a circle
*
* input: radius of the circle
* return value: area of the circle
**********************************************************************/
float area(float radius) // function definition
{
return PI * radius * radius;
}