Skip over navigation

Contents

Basic Commands in C++

Arithmetic operators and Basic I/O

Terms

Problems

You can add two numbers with a plus sign:


int num = 3 + 4;

Now the variable num has the value 7. As always, command lines end with a semi-colon. You can also add variables and carry out multiple sums at once:


double a = 4.10009, b = 9.02, c = 15.0;
double d = a + b + c;

The other basic arithmetic operators are used similarly:


int n1 = 3, n2 = 4, n3 = 8;
int sub = n1 - n2;	// subtraction
int mul = n4 * n1;	// multiplication
int div = n3 / n2;	// make sure the denominator is not 0
int rem = n3 % n1;	// remainder (modulo)

Parentheses can be used to clarify order of calculations:


int parens = ((n1 + n2) * (n3 / n2)) % n1;

Sometimes you might want to change a variable by adding to (or subtracting from, multiplying by, etc.) its current value. Suppose integer n has some value and you want to increment its value by 3. The following is a valid statement:


n = n + 3;	// adds 3 to the value of n

However, the designers of C++ added a more concise way of doing this:


n += 3;	// also adds 3 to n

And this will work similarly with other arithmetic operators:


n *= 2;	// doubles the value of n

Furthermore, there is a special way to increment a value by 1:


n++;

You can even use this shortcut in the middle of another expression:


int a=2, b=3;
int c = a * (b++);

In this case, c will take on the value 6 (= 2*3) and b will subsequently be incremented to 4. If you want b incremented before the rest of the evaluation takes place, you could write:


c = a * (++b);

Notice the placement of the ++ operator. This example increments b first, and then carries out the rest of the evaluation. Thus c will be equal to 8 since 8 = 2*(3 + 1) . The -- operator works in the same fashion, except that it decrements the variable's value by 1.

Basic Input and Output

The standard method to output something to the screen in C++ is to use the output operator (<<) to send values to the cout (pronounced "see-out") stream:


cout << output1 << output2 <<...<< outputN;

Each of the outputs can be a string, character, number, variable, an expression, endl (which inserts a new line and flushes the output stream), or a function whose return value is any of these.

Similarly, the cin ("see in") stream asks for user input. Both cin and cout are defined in the iostream.h header file, as described in section #1 of "Getting Started". Here is a simple example that demonstrates the usage of cout and cin:


#include <iostream.h>
main()
{
	// variable declarations
	char initial;
	int age;
	double dog_years;
	
	// get the user's initial and age
	cout << Enter your first initial, and then your age: ";
	cin >> initial >> age;	// sets the variables 
				// initial and age to
				//  whatever the user inputs.
	dog_years = age / 7;	// calculate user's dog age...
	// ...and output it
	cout << "Your age in dog years is: " << dog_years << endl;
}

Because the first cout statement does not print out a newline character \n or endl, there is no new line, and the next input or output will take place after the end of the sentence. In this case, the user will be asked to type his or her input after the colon. Notice that cin can also take several arguments; that is, you can ask for more than one input value with a single line of code.

Follow Us