Introduction to Programming and C++

Contents Previous Topic Next Topic

Before we continue to for loops, we will make a diversion to discuss some of the more common syntactic short cuts used in C, C++, Java, C#, etc.

When C was originally developed, compiler technology was in its infancy, and therefore, many of the operators used in C (inherited by C++ et. al.) were a way of allowing the programmer to explicitly state efficient CPU instructions. For example, it is possible to evaluate

	a = a + 3;

using fewer CPU instructions than is required to evaulate

	b = a + 3;

An intelligent compiler will make the first example run in fewer instructions than the second while a less intelligent compiler would not have made the same optimization.

In light of this, a number of operators were added to allow the programmer to ensure that the compiled code was as efficient as possible. While these are not technically necessary today, they are ubiquitous throughout the programming world.

Binary Operators and Assignment

Given most binary operators, it is common that a variable is updated by making a change to itself, for example:

  • x = x + 5;
  • x = 3*x;

These may be abbreviated to

  • x += 5;
  • x *= 3;

A summary of some of these operators is given in Table 1.

Table 1. Self-assignment binary operators.

OperatorExampleEquivalent Expression
+=a += 3;a = a + 3;
-=a -= 3;a = a - 3;
*=a *= 3;a = a * 3;
/=a /= 3;a = a / 3;
%=a %= 3;a = a % 3;

As these tutorials continue, we will see other examples of binary operators. Often, though not always, there is an associated self-assignment binary operator associated with each binary operator. Examples which are not allowed are &&= and ||=.

Variable Increment and Decrement

Most CPUs have a single instruction which increments (increases by 1) or decrements (decrease by 1) a variable. Consequently, it is possible to turn

	x = x + 1;

into a single machine instruction. While modern compilers will do this automatically, in 1970, it was more desirable to indicate explicitly to the compiler which instructions to use, hence, the above can be abbreviated to.

	++x;

Similarly, if you want to decrement (decrease by one) a variable, you can use

	--x;

There are two other versions of increment and decrement which are similar, x++ and x--, however, they are essentially identical if they are the only statement which appears in one line.

Program 5. Approximating the exponential function.

#include <iostream>

using namespace std;

int main() {
	double x;

	cout << "Enter a real number: ";

	cin >> x;

	// Approximate exp(x) using a 21-term Taylor series:
	//
	//         1   2   1   3          1   20
	// 1 + x + -- x  + -- x  + ... + --- x
	//         2!      3!            20!

	double result = 1.0;
	double term = 1.0;
	int n = 1;

	while ( n <= 20 ) {
		term *= x / static_cast<double>( n );
		                   // was term = term * x / static_cast<double>( n )

		result += term;    // was result = result + term

		++n;               // was n = n + 1
	}

	cout << "The approximation of exp(" << x << ") is " << result << endl;

	return 0;
}

With modern compilers, this should not make any difference, however, unfortunately, the default Java compiler (last time I checked) will treat ++i; differently from i++; when they form the entire statement.


Contents Previous Topic Top Next Topic