Reading: Section 4.2, Chapter 5
Today we get to look at our first programs!
The examples are going to look a little somewhat contrived today, because it's just not possible to write interesting programs until we handle both variables and control structures. We're doing variables today and control structures tomorrow.
To begin our introduction, we look at one very simple program, the hello, world program (Program 1 on the assignment).
#include <iostream>
#include <string>
using namespace std;
int main() {
// this program prints the string ``hello, world'' and exits
cout << "hello, world" << endl;
return 0;
}
There are a few things you should understand about this program:
Think of a variable as a named box that can hold data whose value we might change occasionally. On paper, it's a blank whose value you can erase and rewrite at pleasure.
This box is restricted to a certain type. We'll be working with four basic types of data.
| char | character |
| double | number (like -2.3 or 1020) |
| int | integer |
| string | a sequence of possibly several characters |
These types can get a little weird at times. The compiler makes decisions on types based on a set of rules, not based on the value. If 15.0 appears in the program, the compiler treats it as a double (because of the decimal point). Here are constants of all four types. These are all completely different as far as the compiler is concerned.
| 2 | the integer 2 |
| 2.00 | the real number 2 |
| '2' | the character '2' |
| "2" | the string "2" |
The first thing we want to do with a variable is declare it so that the computer knows to make a box to hold the data. For example:
declares a box for holding integers, to which we can refer with the name aVar.int aVar;
When you're naming variables, choose names that describe the semantics of the value it is going to hold!
Naturally we will want to put values into these boxes. We do this with assignments.
Now our box contains the value 64.aVar = 64;
We might change the value later on if we want.
aVar = 6;
The cout << business up there is a print
statement that displays things to the screen. We can string
together several things into a print statement.
Printing endl in this way ends the line.
cout << "Now we are "
<< aVar << endl;
When the computer gets to this line now, it will print
to the screen.Now we are 6
Another thing we will want to do is to manipulate values. We can do this with expressions. Some arithmetic expressions:
| expression | value |
|---|---|
| 12.011 | 12.011 |
| aVar | 6 |
| -aVar | -6 |
| aVar + 1 | 7 |
| aVar * 3 | 18 |
| 12.6 / aVar | 2.1 |
| 5 + 2 * aVar | 17 |
| 19 % aVar | 1 (remainder of 18/6) |
| 23 / aVar | 5 (remainder ignored when dividing integer types) |
We can use expressions both in assignment statements and in print statements.
Then 27 appears on the screen.aVar = aVar + 3; // now we are 9 cout << (aVar * 3) << endl;
In class we'll run this complete program and step through how it works.
#include <iostream>
#include <string>
using namespace std;
int main() {
double fahrenheit;
cout << "What temperature? "; // prompt user and read number
cin >> fahrenheit;
double celsius = (fahrenheit - 32) / 1.8; // print conversions
cout << "It is " << celsius << 'C' << endl;
cout << "It is " << (celsius + 273.15) << 'K' << endl;
return 0;
}