Starting C++

This page is for students that have followed the prologue section on Register Machines and are starting with C++ using the official syntax. Such students will have read the page on Register machines in C++ and be wondering what else C++ can do that the original register machine syntax cannot do.

1. Variables and expressions with int

The most important advance on Register Machines is that int variables can have negative values, and that more complicated expressions can be built on the right hand side of = using operators such as + - / and * and round brackets ( ) . Int values are limited to a particular finite range (usually - 2 31 to 2 31 - 1 ). Outside this range ints behave very strangely.

So as well as writing x = x + 1; and x = x - 1; you can write things like x = x + y*z / 2; and x = ( u + 2018 ) * x - w / z;.

The precidence for these operators is mostly as normal, with multiplication and division binding tighter than addition and subtraction.

Remember. The sign = does not mean "equals". It means "calculate the right hand side and put the result into the variable on the left hand side".

Pitfall. There are many other operators in C++, and most work in some way or other on ints. They probably don't do what you expect, so avoid them unless you have carefully looked them up and understood them. In particular there is no "power" operator, and although ^ is an operator it is not in the slightest bit useful here. Don't use it!

Pitfall. Beware lists of items separated by * and / . For example, x*y/u*v means ((x*y)/u)*v and not (x*y)/(u*v) as many people might hope.

Pitfall. The division operator / on int values is division discarding remainder. When you / divide two ints the remainder is always discarded. For example 345 / 100 is 3 and -45 / 10 is -4.

There are precise rules as to when C++ thinks the two input values to / are ints and when this kind of division is used. See the page Types in C++ later on.

Pitfall. It is C++'s rules that determine when the division operator / on int values is division discarding remainder. (You will need to learn these at some point). The computer cannot read your mind to decide what kind of division you want.

C++ also defines a "remainder" operator %. This behaves just like division but gives the remainder not the quotient. So 345 % 100 is 45 and -45 / 10 is -5. This operator is really useful and is often forgotten, especially by beginners.

Note. Annoyingly, the remainder operator on negative values gives a negative remainder. I find that this is rarely what I want, but that is what C++ gives us and we have to work round this.

2. Variables and expressions with double

C++'s int variables work very fast, but are limited. They are limited in size, and also in the fact that non-integers cannot be represented. C++ also provides types of variable that don't have these limitations, the main one being double.

(The name "double" is a curious name which is here for historical reasons only. Don't let it bother you. "Double" is a kind of floating point number. Old-fashioned people call doubles "double precision numbers". For just about any normal calculation, doubles are the best ones to use on modern PCs.)

The main advantage with double over int is that more numbers can be represented. The main disadvantage is that it is much slower. However, double is also limited in its own way, and we will have to spend time looking at these limitations later.

Double values are written with either a decimal point or with an e or E meaning × 10 n , or both. (This is one of the rare places in C++ where the case of the letter doesn't matter.)

Note. If there is no decimal point and no e or E then the number is an int.

Examples are,

  3.14159
  -234.345
  23768E34
  -23.34E-2
  1e4
  42.0
  2e-2

and so on.

Pitfall. If the double number you want is also an integer, don't forget to type the ".0". If you don't, C++ will think it is an int and you might get strange results, e.g. if you are dividing.

You can declare and use double variables just as for int, with the operators + - * / (but not % and certainly not ^). Just use the double keyword instead.

  double x=0.0;
  double y=0.0;
  double z=0.0;
  x = 3.14159;
  y = -234.34 * x + 2.0;
  z = 23768E34 + x/-23.34E-2 + (1e4 - 42.0*z)/2e-2;

You can mix ints and doubles provided you are careful.

  double x=0.0;
  double y=0.0;
  int z=0;
  z = 876;
  x = 3.14159;
  y = -234.34 * x + 2.0 * z; // z is converted to double
  x = 23768E34 + x/-23.34E-2 + (1e4 - 42.0*z)/2e-2;

but beware,

  double x=0.0;
  double y=0.0;
  int z=0;
  z = 876;
  x = 3.14159;
  y = x + ( 234 / z );   // integer division, same as y = x;
  y = x + ( 234.0 / z ); // z is converted to double

There will be more on this point later.

3. Notes

Note also that C++ provides a number of other versions of int and double, with different precision and different speed of execution. For beginners, these two (int and double) suffice for now.

If time permits, you might want to read the page on floating point and how int and double variables are stored.