1 BASIC PROGRAM STRUCTURE
A C++ program1 is made up of a mainline procedure called main(), and zero or more additional procedures to perform
operations within your program. When a program starts execution, it begins by calling the mainline procedure.
Source code for a program can be contained in a single file, or split across multiple files. Additional functionality
may also be provided via external libraries. The source code is compiled into object files. The object files are then
linked together to produce an executable program.
2 OPERATORS
Every computer language provides a set of built-in functions or operators which allow you to perform simple
opera-tions such as addition, subtraction, increment, and decrement. C++ provides a useful set of operators to
perform basic arithmetic, operator grouping, indirection, and binary logic. An expression is a combination of
variables, constants, and operators. Every expression has a result. Operators in C++ have precedence associated
with them; this means ex-pressions using operators are not evaluated strictly left to right. Results from the operators
with the highest precedence are computed first. Consider the following simple C++ expression:
6+3*4/2+2
If this were evaluated left to right, the result would be 20. However, since multiplication and division have a higher
precedence than addition in C++, the result returned is 14, computed as follows.
3*4!12
12/2!6
6+6!12
12+2!14
Of course, we can use parentheses to force a result of 20, if that's what we wanted, with the following expression:
(((6+3)*4)/2)+2
Below is a list of the basic operators in C++, along with an explanation of what they do. The operators are group
according to precedence, from highest to lowest.
1
Throughout these notes, I will use C++ conventions for comments (// rather than /* and */), memory allocation (new and delete rather than malloc()
and free()), printing (cout rather than printf), argument passing, and so on. I will not discuss any of the object-oriented aspects of C++, however. This
material is left to the reader, and can be found in any textbook on C++.
1
, Operator Description
() parentheses define the order in which groups of operators should be evaluated
++, -- pre-increment and pre-decrement
! logical NOT
&, * address of, deference
() casting
->, . member selection
*,/,% multiplication, division, integer division
+, - addition, subtraction
<<, >> bitwise shift
<, <=, >, >= less than, less than or equal, greater than or equal, greater than
==, !=, equality, not equal
&, | bitwise AND, bitwise OR
&&, || logical AND, logical OR
++, -- post-increment and post-decrement
=, *=, /=, etc. assignment operators
Note the difference between the pre-increment and post-increment operators. Pre-increment occurs before any part
of an expression is evaluated. Post-increment occurs after the result of the expression has been returned. For
example, what would the output be from the following lines of code?
a = 1;
b = (a++) - 2 * a++;
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
n = 1;
m = (++n) - 2 * ++n;
cout << "n = " << n << "\n";
cout << "m = " << m << "\n";
If we compiled and ran this code, we would get the results
a = 3
b = -1
n = 3
m = -4
3 CONDITIONALS
C++ also provides a number of built-in conditional statements. This allows us to execute certain parts of our program
based on some condition being either true or false. Unfortunately, in C++ there is no boolean type. A value of 0 is
considered false, any non-zero value is considered true. A well known example of a conditional statement is the if-then-
else conditional. If some expression evaluates to true, then execute one part of our code, else execute a different part.
Below is a list of the conditional statements available in C++, along with examples of how to use them.
2
, 3.1 if-then-else
if-then-else works exactly as you would expect. If some condition is true, the then portion of the conditional is
executed, otherwise the else part of the conditional is executed. The else part of the conditional is optional. If it
doesn't exist and the expression is false, nothing is executed. Consider the following example, which sets a student's
letter grade based on their overall percentage.
if ( n_grade >= 80 ) {
l_grade = 'A';
} else if ( n_grade >= 65 )
{ l_grade = 'B';
} else if ( n_grade >= 50 )
{ l_grade = 'C';
} else {
l_grade = 'F';
}
The braces are optional here, since the then and else code sections consist of only a single statement. If there were
more than one statement, however, the braces would be required.
3.2 for
for is a looping conditional. It executes a set of statements repeatedly a specific number of times, then exits the loop.
The condition here is a value which determines the number of times the loop will be executed. Often, a variable's
value is used for this purpose, as in the following example.
cout << "Please enter a number:\n";
cin >> loop-count;
for( i = 0; i < loop-count; i++ ) {
cout << "Loop pass " << i << "\n";
}
Notice the for statement is made up of three separate parts: an initialization part, a conditional part, and an
interaction part.
for( init-part; condition-part; iterate-part ) {
...
}
The initialization part is executed before looping begins. It is usually used to initialize variable values. In our
example, we use it to set the value of i to 0. The condition part is checked each time the loop begins executing. If
the condition is true, the loop is executed. If it is false, looping ends. In our example, we continue looping until i is
greater than or equal to the value of loop-count. The interaction part is executed each time the loop finishes. It
is usually used to update the value of a variable being used inside the loop. In our example, we increment i by 1
each time through the loop. Thus, i is used to count the number of times the loop has been executed.
3