Section 4 - Simple Numeric Statements

2A.4.1 Integers

Programming is all about putting values into memory locations and methodically changing them, occasionally sending them to the screen or file so someone who lives outside the computer can take a look at them.

The simplest kinds of values are numbers. hand holding dollars

The simplest kind of numbers are integers, or ints. These are the numbers

    1, 2, 3, 4, ...

as well as

    0, -1, -2, -3 ...

Integers don't include decimals or fractions. For decimals we need more accommodating data types, like floats or doubles, which we shall introduce soon.

2A.4.2 Data Types

The int represents for us the first of several kinds of data that we can use in our programs.  The different kinds of data are called data typesfloat, double, char and long are also data types in C++, whose meanings we shall learn shortly.

Some data types are so simple they are called primitive types.  They can't be broken down any further.  The above types, int, float, double, etc. are all primitive types.  Some data types are more complicated and contain an internal structure.  These are usually called classes.  Therefore, a class is nothing more than a fancy data type.

So far, the only data type we have formally introduced is the int type.

2A.4.3 Constants, Variables and Assignment Statements

I use the term variable or object to refer to the memory location that holds the data. You can think of a variable or object as an entity that can take on different values as the program progresses.

The word object will have a richer meaning as we proceed. Variables can be thought of as the simplest kinds of objects, just as atoms can be thought of as the simplest kinds of molecules. Variables are normally used to refer to instances of primitive data types, while objects are used to refer to instances of complex data types (instances of classes).

To define an int variable (or object)  I do this:

int someNumber;

This causes someNumber to become an identifier, or variable name, used to label one location in the computer's memory. That location currently has a garbage value in it (which means nothing useful). We can improve that situation and place a useful value into the storage location known as someNumber like this:

someNumber = 34;
Assignment Statement

The above statement puts the int constant, 34, into the object location, someNumber. It is called an assignment statement because it assigns the value 34 to the variable someNumber.  The = sign is also called the assignment operator.

When I say 34 is a constant, I mean that no matter how hard I try, I can't change it to be 35. It was born, and will die 34. But I can change the value of someNumber to be something else.  This is done with other assignment statements.  We can have as many assignment statements as we wish, even if they all have the same variable on the LHS (left hand side) of the assignment operator:

someNumber = -10;            // overwrites the 34 with a -10
someNumber = someNumber + 3; // adds 3 to the -10 making
// it -7
someNumber++;          // special notation that adds 1 to
// someNumber making it -6
someNumber = (someNumber + 1) / 2;  // adds 1 to someNumber, making
// a -5, then divides by 2
// making it a ...

What about that last statement.  If someNumber is -6 when we reach that statement, what will someNumber be after the statement is done executing?  You might think, (  -6  +  1  ) / 2 equals ...-2.5.  But that's not correct.

The answer lies in the underlying data type, in this case, int. Integer arithmetic never uses decimal points or fractions. When you divide two ints the result is the quotient, as in quotient without the remainder. So -5 / 2 = -2 with a remainder of -1. We throw away the remainder, and the quotient is just -2. Another way, perhaps easier, is to get the complete answer, -2.5, then throw away, or truncate, the fractional part, leaving -2.

A NOTE OF INTEREST: If you want the remainder of an integer division, use the modulo operator, %. In other words, in the integer world,

14 / 3 evaluates to 4
14 % 3 evaluates to 2

This is a very useful operator.