What you learned in week 2:
- Text (strings of letters and symbols) are generally
stored in the computer in ASCII code. Each symbol takes one byte.
- Your source code files:
- If you don't use the line "using namespace std;", then you must
precede each standard identifier with std::, as in std::cout, std::endl,
etc.
- What happens if you make various errors in your source code - what
the error messages from the compiler look like
- You can use the program "indent" to re-format your source code
- Give files short but descriptive names
- Input and output:
- You can string together multiple outputs into one line, as in
cout << "Here's a number: " << variable_1 << endl;
- You can do calculations in the middle of a cout statement.
For example:
cout << "The quotient is " << num1/num2;
- In an output statement, variables are displayed in the
proper format, given the type of variable.
- You can input data from the user and store it into a variable
using a "cin" statement.
- You can input several values with one cin statement - the user
must separate the inputs with spaces or enters
- When you store data into a variable using cin, the data is
automatically stored in the proper format, given the type you declared
the variable to be. Strings
are stored in ASCII, integers in binary, etc.
- Variables:
- The declaration of a variable declares the type of a variable,
reserves space in memory for that variable, and assigns it a name
(an identifier) to use throughout the program
- A variable declaration looks like:
type variable_name;
- You can declare two variables on the same line (separate them with
commas) provided they are of the same type.
- There are many different types of built-in and pre-defined types.
These include int, float, char, long, bool and string. You learned
when to use each of these.
- The type "string" is used for storing text in a variable.
To have a variable of this type, you need to
#include <string>
- Later, we'll learn more about pointer variables. These are for
storing memory locations. Here are some example declarations:
int *integer_location;
float *num_ptr;
- When you declare a variable, you must choose the appropriate type
- When you declare a variable, you should choose a short but
descriptive name
- Identifiers (names of variables) can contain letters, digits, or
underscores. They may not begin with a digit. They may begin with
an underscore but this is strongly discouraged. The case of the
letters matters (The name Num is different from num). You may not
use reserved words for variable names (Examples of reserved words
include int, char, bool, using, main, etc.)
- Arithmetic
- If you divide two integers, the result is an integer (the remainder
is truncated). If you divide two floats, or an integer and a float,
the result is a float.
- The order of precedence for arithmetic operations
follows the familiar rules of arithmetic.
- Assignment (the = sign)
- The assignment operator calculates the expression on the right
hand side of the equal sign, then stores it in the variable on the
left hand side.
- The left hand side can't contain arithmetic - it has to be a
variable.