C++ Programming
C++ Programming
ARITHMETIC 1
SOME PARTS OF THE PROGRAM
Comments - a type of program documentation
/* 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.
ARITHMETIC 4
SOME PARTS OF THE PROGRAM
; statement terminator
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
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
ARITHMETIC 8
ORDER OF OPERATIONS
1. ( )
2. * / %
3. + -
4. left to right
6 1 2 4 3 5
ARITHMETIC 9
ASSIGNMENT
We will usually see an assignment operation in this format:
<variable> = <expression>
c = c + 3;
or
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
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
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“
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
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;
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
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