Programming test 2 - C++ (part 3)
Explicit Conversion
Can be used to remove warnings - you are effectively saying, "I know what I'm doing"
Characters
Can perform comparisons (lexicographical) Can perform arithmetic Can convert between cases
Valid Names
Cannot be a reserved word
Valid Names
Cannot begin with a number
Scope
Cannot have two variables in the same scope with the same name
Equal: == Not equal: != Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >=
Comparison Operators
Valid Names
Contain only letters, numbers, and underscores (_)
Streams
Data "flowing" from one place to another
Number Text Boolean
Data Types in OVERFLOW
assign or math
Data conversion occurs when you assign or math
Input Stream
Data flowing into the program By default comes from the keyboard The cin object
Input extraction
Only reads characters that matches the type of input we're looking for (e.g. digits for a number)
Boolean Operators
Operands must be boolean Answer is boolean && and || use short-circuit evaluation
Arithmetic/math Operators
Operands must have the same type Answer is always the same type as the operands Beware integer division Beware division by zero
Comparison Operators
Operands must have the same type Answer is boolean Beware equal / not equal with floats
associativity
Operators with the same precedence have an ______: either left associative (left-to-right) or right associative (right-to-left)
How do we decide which operation to perform first?
Parentheses Precedence & Associativity
Initialization
When you declare a variable, it's initial value is undefined; whatever happened to be in that memory previously; garbage
Const
can be initialized but not assigned to
Input
cin - key board
Input
cin >> variable;
Expression
consists of literals variables and operators
Declaration ****
consists of the data type, followed by the variable name (ends in semicolon)
Output
cout - screen
Output
cout << variable << data << endl;
Post-test loop
do block while (test); Must have a semi colon at the end
backslash
we use ___ for special characters
Pre-test loop
while (test) block
Literals
write data into program, literally; writing data directly into your program
higher
x / have _____ precedence than +-
{block} (Sequence)
{ instruction; instruction; ... }
Implicit conversion when performing a mathematical operation
— the two operands must be the same data type Smaller ⇒ larger Signed ⇒ unsigned Integer ⇒ floating point
I/O Manipulators
set width , set precision, set fill
Inputting a character
simply gives you the next character (its ASCII value)
Output strings
simply prints each character in sequence
using namespace std;
so you don't have to call by last name
void main()
sub routine named main
Input Strings
take multiple characters up until the next whitespace (single word)
Input numbers
takes as many number characters as it can find, then gives you the number with that value
Conversion
tries to copy the data to a different format
Assignment
variable = data;
Scope
From the point of declaration to the end of the block
Implicit Conversion
When assigning data of one type to a variable of a different type
short/long
(potentially) use less or more memory
Floating point rounding errors
-All numbers are represented in scientific notation -Decimal numbers do not make pretty binary fractions .1 = .00110011001100110011... -Fractions have to be rounded -Small changes may be lost 1e10 + 500 = 1e10
short int
2 bytes
long int
4 bytes
long double **
8 bytes
Whitespace Case sensitive #include <iostream> using namespace std; void main() Comments Text file
Basic forms of a C++ program
And: && Or: || Not: unary !
Boolean Operators
Namespaces
Acts as a "last name" to avoid accidental name conflicts
Input / Output Operators
Also has a side effect: input or output Left hand operand must be input/output stream Result is the stream
Explicit Conversion
Applies to the next piece of data
Add: + Subtract: - Multiply: * Divide: / Modulus (remainder): % Negative: unary -
Arithmetic/math Operators
Conversion
Assigning to a variable of a different type
Assign: = Add to: += Subtract from: -= Multipy by: *= Divide by: /= Increment: ++ Decrement: --
Assignment Operators
side effect
Assignment operators with a ____: Changes the variable
Error Stream
Data resulting from errors flowing out of the program By default goes to the screen The cerr object
Output Stream
Data resulting from normal execution flowing out of the program By default goes to the screen The cout object
Scope
Defines when a variable name is valid
precedence level
Each operator has a ________—operators with higher precedence go first
-input -ouput -cerror
Every program has three streams
Input
Extraction
Initialization
Formally, an initialization is when we give a variable an initial value as part of the declaration
"breaks"
If the next character does not match the type of input we're looking for, the input stream
Input extraction
If the stream is empty, the program pauses and allows the user to type in a new line
Conversion
In an operation that expects two values of the same type - e.g. math and comparison
Initialization
Informally, initializing a variable means giving it a value before using it
Extraction: >> Insertion: <<
Input / Output Operators
Output
Insertion
Characters
Just a number that is intended to be an ASCII value
Assignment Operators
Left hand operand must be a variable (lvalue) Operands must be the same data type Still has a result: the lvalue One time change, not an algebraic rule Beware = when you mean ==
Initialization
Looks like an assignment, but it's different
Const unsigned short/long
Modifiers
Declare ****
Must ____ all variables used in your program
Input extraction
Skips over any whitespace in the stream (throws it away)
input output assignment control flow?
Statements in C++
Redirecting Streams
Streams are like hoses; they can be connected to different sources and destinations
tokens
The basic idea is that input is a stream of ___, not individual characters
Math comparison boolean input/output assignment
Types of operations
Valid Names Declaration Scope Namespaces Initialization
Variables in C++
Scope
Variables in different scopes can have the same name, but it's often a bad idea
text streams
We will be using ______ — data is all ASCII characters (there are also binary streams)
semicolon
each instruction ends with a ____
Comments C style *****
everything btwn /* and */
Comments C++ style ******
from // to the end of the line
broken stream (input)
getting input from a ____ has no effect
Float
has better range, Int has better precision
Range
how big (or small) a number it can hold
Precision
how many significant digits
if (1-way branch)
if (test) block
Two way branch
if (test) block else block
Comments ****
ignored by compiler
#include <iostream>
input/output stream; goes and gets code in iostream so you don't have to keep doing it.
I/O Manipulators—
inserting them into the stream changes the stream
White Space
insignificant; but can't run two words together
String
is in double quotes; represents multiple characters - not the same thing!
Single Character ***
is in single quotes; represents the number for that character
operation on characters
just numbers you can add
unsigned
no negative numbers (integral only); minus
Operators
operate on data to form a result
signed
plus
Output numbers
prints character representation of that number
Output Boolean
prints either "1" or "0" (throwback to C)
Output Characters
prints the character with the given ASCII value
Output insertion
prints whatever you put in stream
Explicit Conversion or TYPE CAST
put the name of the type you want in parenthesis
cerror
screen