C/C++ data types, basic operators, and control structures

Data types
Data types in C++ are similar to Java. There are ints, floats, doubles, etc. In C++ it's bool instead of boolean. There are long and short ints although you are not assured of the number of bytes for each. There are also signed and unsigned ints. Unsigned ints can only hold ints >= zero.

Some operations are not completely defined, e.g., division involving negative numbers, so it is possible to get different answers using different compilers. You shouldn't have problems though with the computations in your courses.

Characters are similar with literals in single quotes. The char type in C++ does not have a specified byte size although they are typically stored in 8 bits.

String terminology can be ambiguous. There is a string class with all the usual functionality, but C/C++ programmers also refer to char arrays as strings. If you have an array of char, always store the NULL character, '\0' as the final character of the string. If you do this, then you can traverse and compare char by char, but you can also use the whole thing as you would a string object for I/O, e.g., if   s   is a valid char array, cout << s << endl;   displays the string.

Note that when initializing on the declaration, the '\0' char will automatically be stored. So the following char array will have six elements:
   char s[] = "hello";
Basic operators
The basic operators are the same as in Java, e.g., +, -, *, /, %, ++, --, etc.

The question mark operator, ?:, is also found in C++. Some people call it the ternary operator because it is the only operator in C++ (and Java) that takes three operands. If you are not familiar with it, it's works like an if-else, but combines operators. If used simply, it is elegant, but it can easily become unreadable if nesting is used.

For example, suppose you are given some int and wish to assign it to a variable as long as it is not negative, but if it is negative, you wish to assign zero. Using an if-else, you write the code:
   int number;
   // user inputs a value and it is put into number
   int answer;
   if (number >= 0) {
      answer = number;
      answer = 0;
Using the ?: operator, you can combine these operators:
   int number;
   // user inputs a value and it is put into number
   int answer = (number >= 0 ? number : 0);
The code inside the parens evaluates to either number or 0 as the ? acts like an if, and then that value is assigned to answer.

C++ also includes operators to act on bits, e.g., and (&), or (|), xor (^), not (~), left shift (<<), and right (>>). These will not be covered.

Control structures
Not much to say here. Java's control structures were based on the control structures in C++. The if, for, while, do-while, and switch are identical. There are also the break and continue statements.