A C++ program is a collection of functions.
Each function is a collection of statements.
A declaration statement creates a variable. An assignment statement provides a value for that variable.
The following program shows a new capability for cout.
#include <iostream>
/*from w w w. j av a 2 s .co m*/
int main() {
using namespace std;
int examples; // declare an integer variable
examples = 25; // assign a value to the variable
cout << "I have ";
cout << examples; // display the value of the variable
cout << " examples.";
cout << endl;
examples = examples - 1; // modify the variable
cout << "I have " << examples << " examples." << endl;
return 0;
}
The code above generates the following result.
To store an item of information in a computer, you must identify both the storage location and how much memory storage space the information requires.
The program has this declaration statement (note the semicolon):
int examples;
An assignment statement assigns a value to a storage location.
The following statement assigns the integer 25 to the location represented by the variable examples:
examples = 25;
The = symbol is called the assignment operator.
One feature of C++ is that you can use the assignment operator serially.
For example, the following is valid code:
int a; int b; int c; a= b = c = 88;
The assignment works from right to left.
The second assignment statement demonstrates that you can change the value of a variable:
examples = examples - 1; // modify the variable
The following code uses cin (pronounced "see-in"), the input counterpart to cout.
Also the program shows yet another way to use that master of versatility, the cout object.
#include <iostream>
// w w w . j a v a 2 s . c o m
int main()
{
using namespace std;
int examples;
cout << "How many examples do you have?" << endl;
cin >> examples; // C++ input
cout << "Here are two more. ";
examples = examples + 2;
// the next line concatenates output
cout << "Now you have " << examples << " examples." << endl;
return 0;
}
The code above generates the following result.
The following statement reads value into variable.
cin >> examples;
The iostream file defines the << operator so that you can combine output as follows:
cout << "Now you have " << examples << " examples." << endl;
This allows you to combine string output and integer output in a single statement.
The resulting output is the same as what the following code produces:
cout << "Now you have "; cout << examples; cout << " examples"; cout << endl;
You can also rewrite the concatenated version this way, spreading the single statement over four lines:
cout << "Now you have " << examples << " examples." << endl;
The following code outputs the values of expressions.
#include <iostream>
using namespace std;
int main() { //w w w . j a v a 2 s .com
int x;
cout << "The expression x = 100 has the value ";
cout << (x = 100) << endl;
cout << "Now x = " << x << endl;
cout << "The expression x < 3 has the value ";
cout << (x < 3) << endl;
cout << "The expression x > 3 has the value ";
cout << (x > 3) << endl;
cout.setf(ios_base::boolalpha); //a newer C++ feature
cout << "The expression x < 3 has the value ";
cout << (x < 3) << endl;
cout << "The expression x > 3 has the value ";
cout << (x > 3) << endl;
return 0;
}
The code above generates the following result.