Variables

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.

Hello, World

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:

Variables

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.

A variable named aVariable containing 64.

This box is restricted to a certain type. We'll be working with four basic types of data.

charcharacter
doublenumber (like -2.3 or 1020)
intinteger
stringa 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.

2the integer 2
2.00the real number 2
'2'the character '2'
"2"the string "2"

Declarations

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:

int aVar;
declares a box for holding integers, to which we can refer with the name aVar.

When you're naming variables, choose names that describe the semantics of the value it is going to hold!

Assignments

Naturally we will want to put values into these boxes. We do this with assignments.

aVar = 64;
Now our box contains the value 64.

We might change the value later on if we want.

aVar = 6;

Print statements

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.

cout << "Now we are "
  << aVar << endl;
Printing endl in this way ends the line.

When the computer gets to this line now, it will print

Now we are 6
to the screen.

Expressions

Another thing we will want to do is to manipulate values. We can do this with expressions. Some arithmetic expressions:

expressionvalue
12.01112.011
aVar6
-aVar-6
aVar + 17
aVar * 318
12.6 / aVar2.1
5 + 2 * aVar17
19 % aVar1 (remainder of 18/6)
23 / aVar5 (remainder ignored when dividing integer types)
You can see that we can use +, -, *, /, and % to build up more complicated expressions. These are called operators.

We can use expressions both in assignment statements and in print statements.

aVar = aVar + 3; // now we are 9
cout << (aVar * 3) << endl;
Then 27 appears on the screen.

Summary example

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;
}