C++ PROGRAMMING
ARITHMETIC 1
SOME PARTS OF THE PROGRAM
Comments - a type of program documentation
// indicates that the remainder of the line is a comment
/* comments can also look like this */
/* also
like
this
*/
ARITHMETIC 2
SOME PARTS OF THE PROGRAM
#include <iostream> - a preprocessor directive
Tells the pre-processor to include in the program the contents of the I/O stream
header file called iostream.h . This allows us to use standard stream input
and output objects like cout (displays to the screen).
As you can see, we need to also code the using namespace std; statement.
For now, let’s just say that this is so that we don’t have to use ugly prefixes on
cout, like std::cout.
ARITHMETIC 3
SOME PARTS OF THE PROGRAM
int main( ) - main function header
Every C++ program has at least one function, called main. And there is ONLY
ONE main. Program execution begins with the first statement in main.
{ brackets to denote the body of the function }
ARITHMETIC 4
SOME PARTS OF THE PROGRAM
; statement terminator
Every C++ statement must end with a semicolon.
<< stream insertion operator
Expression to the right of the operator is inserted (sent) to the cout object (the
display screen or console window).
ARITHMETIC 5
SOME PARTS OF THE PROGRAM
\n newline escape sequence
The backslash is an escape character. The character following it takes on a
different meaning, e.g.:
\t - tab
\a - alert; ring bell
\\ - prints a backslash
\” - prints a double quotation mark
return - exits from the function
In this case control over execution is transferred back to the operating system.
ARITHMETIC 6
ARITHMETIC OPERATORS
The built-in arithmetic operations are
Addition +
Subtraction
Multiplication *
Division / (is integer division if operators are integers)
Modulus % (remainder)
ARITHMETIC 7
ARITHMETIC OPERATORS
e.g., if the value of
is to be assigned to variable x, it is coded:
x = b + c - d * e / f;
Parentheses may be used. These are evaluated first. e.g.,
x = (b + c - d)* e / f; is evaluated as:
ARITHMETIC 8
ORDER OF OPERATIONS
1. ( )
2. * / %
3. + -
4. left to right
Example. What is the order of operations in the following expression?
z= p*r%q+w/x-y;
Z = P * R % Q + W / X - Y ;
6 1 2 4 3 5
ARITHMETIC 9
ASSIGNMENT
We will usually see an assignment operation in this format:
<variable> = <expression>
An expression is a combination of operators and operands. For example,
c = c + 3;
or
average = sum / count;
ARITHMETIC 10
INCREMENT / DECREMENT OPERATORS
a++; is the same as a = a + 1; and also the same as ++a;
a; is the same as a = a 1; and also the same as a;
a++ postincrement
++a preincrement
a postdecrement
a predecrement
These are unary operators. They operate on only a single operand. The
operators we are more familiar with are binary operands; they operate on two
operands, much like the + operator in the expression a+b.
The degree of an operator refers to the number of operands it takes.
ARITHMETIC 11
INCREMENT / DECREMENT OPERATORS
Example:
int c;
c = 5;
cout << c << endl; // outputs 5
cout << c++ << endl; // outputs 5 (then increments)
cout << c << endl << endl; // outputs 6
c = 5;
cout << c << endl; // outputs 5
cout << ++c << endl; // outputs 6 (after incrementing)
cout << c << endl; // outputs 6
ARITHMETIC 12
FUNCTIONS IN THE MATH LIBRARY
To use these built-in functions we need to include the <math.h> header file
function what it does returned value
abs(a) absolute value of a same data type as argument
pow(a1,a2) a1 raised to the power of a2 data type of argument a1
sqrt(a) square root of a same data type as argument
sin(a) sine of a (a in radians) double
cos(a) cosine double
tan(a) tangent double
log(a) natural logarithm of a double
log10(a) base 10 log of a double
exp(a) e raised to the power of a double
ARITHMETIC 13
INTEGERS, DOUBLES AND FLOATS
Each variable or constant in C++ must have a type (e. g. int double
or float)
Whole numbers with no fractional parts are integers, type int.
Floating point numbers, numbers with fractional points (even
when the fractional part is 0) are type float or type double
ARITHMETIC 14
C++ ENDL
Consider the output window (the window in which the output from
your program, and the input from your keyboard is displayed.)
When your code executes the instruction
cout << endl;
The cursor in the output window is moved to
the beginning of the next line
ARITHMETIC 15
C++: MOVING TO A NEW OUTPUT LINE
C++ style: endl
C++ can also use C style: "\n"
escape sequence for the char "newline“
cout << "Hello World" << endl;
cout << "Hello World\n";
In C++ both the statements print string
"Hello World" to display, then move to next line
ARITHMETIC 16
C++ OUTPUT WINDOW OUTPUT
What can you print to your computer’s output window using the
insertion operator <<?
string name=“John”;
const double scale_factor = 2.2;
cout << scale_factor // Prints 2.2, the value of constant scale_factor
cout << name; // Prints John, the name stored in variable name
cout << “This is a string literal”; // Prints This is a string literal
Each cout above prints the value of one variable or constant
ARITHMETIC 17
C++ OUTPUT WINDOW OUTPUT
You can print the values of more than one variable or constant in a single
cout statement
cout << “the number of games was ” << numGames << endl
<< “our team won “ << numWin << “ games” << endl;
Given that the variables have the values
numGames = 23 and numWin = 15
This single cout statement prints
the number of games was 23
our team won 15 games
to the output window
ARITHMETIC 18
INPUT USING CIN
Differences:
">>" (extraction operator) points opposite
Think of it as "pointing toward where the data goes"
Object name "cin" used instead of "cout"
No literals allowed for cin
Must input "to a variable"
cin >> num;
Waits on-screen for keyboard entry
Value entered at keyboard is "assigned" to num
ARITHMETIC 19
PROMPTING FOR INPUT:
When using console output and keyboard input always "prompt"
user for input
cout << "Enter number of dragons: ";
cin >> numOfDragons;
No endl in cout means that the prompt "waits" on same line for
keyboard input as follows:
Enter number of dragons:
ARITHMETIC 20
A FIRST PROGRAM - GREETING.CPP
ARITHMETIC 21