# PEG:C++ Lesson 4 for Pascal Users

← Previous Lesson | Next Lesson →

C++ has a few operators that Pascal doesn't, but you don't need to know about them right now.

Arithmetic operators (`+`, `-`, `*`, and `/`) are the same in C++ as in Pascal, with one exception. In Pascal, / always yields a real, even if the operands were both integers and the quotient is an integer. In C++, dividing two integers always gives an integer (but if at least one operand is real then the result will be real too.) C++ also has the unary plus and minus (although unary plus is pretty useless...) We are able to do arithmetic with chars too, as they are stored as `shortint`s.

div and mod: In C++, using ordinary division `/` on two integers has the same effect as using `div` in Pascal --- the fractional part is discarded and an integer results. In C++, the operator `%` is the same as `mod` in Pascal - taking two integer operands and returning an integer result. Again, we can use `char`s in C++ with these operations.

`shl` and `shr`, Pascal operators, find their equivalents as `<<` and `>>` in C++. (These operators are also used for input and output, as you will see shortly.) Once again these operators work with `char`s too.

The operators `<`, `<=`, `>`, and `>=` are the same in Pascal and C++. The operator `<>` in Pascal is replaced with `!=` in C++.

Now for the tricky part. Whereas in Pascal we wrote `<left side> := <right side>` for assignment, in C++ assignment is just a single equals sign (`=`). Now, the single equals sign is used in Pascal for testing if two values are equal. In C++, we use a double equals sign, `==`.
For example:

```x = 3;  //sets the value of x to 3
x := 3; //compile error
x == 3; //true if and only if x is 3
5 = 3;  //compile error
5 := 3; //compile error
5 == 3; //equal to false```

(Yes, you are allowed to write a statement that does nothing but has a value, such as `5==3;`, as in Pascal. But `5=3` is just plainly wrong, since we can't change the value of 5.)

When using the `<`, `<=`, `>`, `>=`, `!=`, `==`, and `=` operators in C++, `false` is equivalent to 0, and `true` is equivalent to 1. If we assign an `integer` or a `real` to a `bool`, 0 is `false`, and anything else is `true`.

Pascal has the bit-string operators `and`, `or`, `not`, and `xor`. In C++ these are denoted by `&`, `|`, `~`, and `^`, respectively. However, C++ also has the operators `&&`, `||`, and `!` for logical `and`, `or`, and `not`, respectively. The difference is that these operators act on `boolean` values. So for example, `2&5` is zero since we take the binary representations `010` and `101`, but `2&&5` is `true`, since both 2 and 5 are considered `true`.

The precedence of operators is as follows (operators on the same line have the same precedence, operators on a higher line have higher precedence):

```+ - ! ~ (unary plus and minus)
* / %
+ - (binary plus and minus)
<< >>
< <= > >=
== !=
&
^
|
&&
||
=
```

Both `BEDMAS` and `NAXO` are followed (yes, you can use parentheses, ( ), in C++ as well.)