CS 111 - Week 8 Lecture 1 - 2016-10-11
Intro to C++
* C++ simple expressions
* and a literal is a simple expression,
as is an identifier that has been given
a value;
* BUT C++ has some different types,
and some of their syntax is a little
different;
* C++ has SEVERAL numeric types,
and because it is "strongly typed" --
you have to SPECIFY types in advance,
as we will see --
we can't get away with a single
number type;
Here are the TWO C++ numeric types
we will be using in CS 111:
int - integer type
double - double-precision floating point
(there is also:
float: single-precision floating point,
short: lesser-range integer
long: more-range integer
...and there may be more...)
* so -- how do you write an
int literal?
put digits together!
optionally preceded by + or -
* how do you write a double literal?
...put a decimal point in there
somewhere,
...OR use scientific notation;
3.0
-15.5
13e5 <-- 13 times 10 to the 5th power
* C++ actually has 2 string types,
and I can't ignore that;
there is an "old" style string type
called:
char*
AND there is a "new" style string class
called:
string
When you write something in double
quotes in C++,
that is a literal of type char*
I can't type a C++ literal of type string,
(BUT we'll see we CAN assign a
char* literal to be string identifier's
value,
AND in the vast majority of cases,
DECLARE identifiers and function parameters
and function return types to be
string
...rather than:
char*
...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* There's also a C++ type
char
...and a char literal is a single character
surrounded by single quotes
(a few special characters are typed
with a backslash and a character within
single quotes --
'\n'
...is a char, the newline character
* C++'s boolean type is called:
bool -> the C++ boolean type
and the bool literals are:
true
false
* compound expressions
...there are SEVERAL kinds of compound
expressions;
* any expression can be surrounded by
parentheses (and that expression is now a compound
expression)
* INFIX operators go IN BETWEEN two
expressions;
* PREFIX operators go BEFORE an expression;
* POSTFIX operators fo AFTER an expression;
* and a call to a function has yet-another syntax,
which we'll demo/discuss on Thursday;
...and all of these are C++ compound expressions!
* MORE on INFIX operators -
* first, here are some infix arithmetic operators:
+ - * /
^ these arithmetic operators
expect expressions of a numeric
type;
* IF both operand expressions
are of type int,
you get the integer version of
that operation,
and the result will be an int
* IF either operand expression
is of type double,
you get the floating point version of
that operation,
and the result will be a double
(so, YES,
3 / 5
is
0
in C++, beware!!!! integer division, because 3 and 5 are
of type int!
3.0 / 5
is
0.6
...because 3.0 is of type double, so floating-point division,
and so result *is* of type double
* there is also an infix modulo operator
in C++
%
..and it returns the remainder from
integer division
* ORDER of OPERATIONS!
* once you have infix operators, you have to worry about
the precedence of operations in a compound expression;
(shades of PEMDAS! BUT has to be defined as part of C++
syntax, because computers do NOT tolerate ambiguity
well...!)
* you can find precedence charts for C++ operators --
see, for example:
http://en.cppreference.com/w/cpp/language/operator_precedence
...16 levels of operators (and associativity...!)
* (but you can use PARENTHESES to specify desired order
of operations, also!) <-- and this makes it clearer
to someone READING an expression what is intended,
also;
* CS 111 CLASS DEAL:
...I won't require you to memorize operator precedence
if you use PARENTHESES to make it CLEAR what operations,
in what order, are intended;