CH 03
CH 03
Data:
Instructions:
A Second program
#include <iostream> // a.
int main(){ // c.
num = 10; // e.
return 0; // h.
Notes:
d. num , square are the data items (which we often call variables)
e. they get initial values from instructions via assignment (the = is the assignment command)
f. multiplying value num by itself and putting the result in square
g. the result is displayed via cout: "The square is: 100"
h. return an integer: the function main "promised" to provide an integer "answer". In this program
(and usually) the "answer" doesn't matter. But to keep the promise, we just
return 0.
i. on any line, typing // turns the rest of the line into a comment. A comment is not "seen" by the
computer ‐ it is not part of the program. It is used by human readers of the
program to explain something in the program.
j. every statement shall end with a semicolon, except what?
1. Compile error ‐ invalid C++ statements; so your code cannot be translated to machine language.
Examples:
a) integer x, y;
b) num1 = num1 + num2
In a) use int, not integer; while in b) the “;” at the end of the line is missing.
2. Link error: one of the functions used in the program is not found when it's time to run the program.
4. Logic error ‐ program runs to completion, but the results are wrong. Example:
Fixing 3 and 4 is called debugging; the computer does what you said, not necessarily what you meant!
Language Basics
These programmer‐defined names are also called identifiers, such that they:
Each data item has a type and a name chosen by the programmer. The type determines the range (set) of
possible values it can hold as well as the operations that can be used on it. For example, you can add a
number to a numeric data type, but you cannot add a number to a character or string.
To create an integer variable that your program can use, you must declare it, like:
So you could picture the results of the last two lines as:
Note: It is critically important that variables have values before they are used in a program. In the
examples above, x and y have no values even though they have been declared. More accurately, they do
have values but those values could be anything between ‐2 billion and +2 billion. Before you use x or y
you would have to give them values.
2 and 3. float and double (floating point or real numbers). These data types are also commonly referred
to as real variables (following mathematics usage) when it is not important which one we mean and we
don't want to always have to say "float or double".
floating point constants are written as 3.08 ‐32.0 0.15 9.3e7 (9.3 times 10 raised to 7)
Declaration Examples:
char ch;
string s;
Arithmetic Operators
Arithmetic Expressions are formed by operands and operators. Operands are the values used, operators
are the things done to the numbers. Parts of arithmetic expressions are often grouped by () for clarity or
to affect the meaning of the expression:
// declaration
int x, y;
double realnum;
// initialization
x = 11;
y = 2;
realnum = 2.0;
Also ‐ the spaces written between operands and operators are not required by C++, but do improve
legibility. More complex expressions are evaluated by C++ using Rules of Precedence (like algebra)
1. sub‐expressions in ()
2. unary ‐
3. * and / ‐ left to right
4. + and ‐ ‐ left to right
In the examples below, the red numbers show what operation is done as C++ evaluates and expression:
3 + 5 * 4 / 2 = 3 + 20 / 2 = 3 + 10 = 13
But () can be used to change this (or to make expression more readable)
(3 + 5) * 4 / 2 = 8 * 4 / 2 = 32 / 2 = 16
You must use a type cast ‐ to tell C++ that it should temporarily use the variable as if it were some other
data type. To do this, put the name of the data type you want in parenthesis before the variable name. It
will not change the variable type or value permanently, but it will force C++ to temporarily treat the
variable as a different type:
double average;
You cannot do just any type cast: (char) sum would not make sense and would not compile.
Assignment Statement/Operation
It means: "evaluate the expression on the right and store this value in the variable on the left"
Notice that there is always exactly one variable on the left to receive the value of the expression on the
left.
Notes:
1. All variables in expressions on the right must have defined values to avoid unpredictable results. This is
an example of what I meant before when I said variables must have values before they are used.
x = y + 1; // ??? in x
Inadvertent use of uninitialized variables is one of the most common causes of program errors. Learn to
be very careful about this.
3. Naming Variables: use meaningful names that explain themselves to reader of the program.
Relational Operators
Logical Operators