Basic Commands in C++

Contents

page 1 of 3

Arithmetic operators and Basic I/O

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: