COP 3014 Exam 1
CPU
Central Processing Unit: The "brain" of the computer (ISA - Instruction Set Architecture: the specific set of low-level instructions available to a CPU)
operand
an input to an operator
R-value
any expression that evaluates to a single value
test expression
best to make the expression a Boolean expression, which is an operation that evaluates to true or false
/*
block comments; can span multiple lines
floating point types
float, double, long double
comments
for documenting programs; they are ignored by the compiler
operators
functions that use a more familiar notation
disk storage
hard disk, floppy disk, CD, DVD
unary operator
has one operand
ternary operator
has three operands
binary operator
has two operands
bool
has two possible values, true or false
arity
how many operands an operator takes
if/else basic syntax
if (expression) statement; else statement; -else clause is optional: if (expression) statement;
flow of control
implemented with three basic types of control structures
post-increment
incrementing is done after the value of x is used in the rest of the expression int x = 5, count = 7; result = x * count++; //result = 35, count = 7;
pre-increment
incrementing is done before the value of x is used in the rest of the expression int x = 5, count = 7; result = x * ++count; //result = 40, count = 8;
extraction operator >>
inputStreamSource >> locationToStoreData [double weight; cin >> weight;]
initialize built-in variable types with values
int numStudents = 10; double weight = 160.35; char letter = 'A';
cascading operators
int x, y; double a; cin >> x >> y >> a;
software development
involves -analysis and problem definition -design: includes design of program or system structure, algorithms, user-interfaces, and more -implementation (coding) -testing: can be done during design, during implementation, and after implementation -maintenance: usually the major cost of a software system. Not part of "development", but definitely part of the software life cycle
//
line comments
cascading
linking of multiple operators, especially of related categories, together in a single statement x= a + b + c - d + e; x = y = z = 3;
input devices
mouse, keyboard, scanner, network card
switch statement
often convenient for occasions in which there are multiple cases to choose from -only works with integer and character expressions switch (expression) { case constant: statements case constant: statements default: statements break; }
indentation
only for people; it improves readability, but means nothing to the compiler
insertion operator <<
outputStreamDestination << itemToBePrinted [cout << "Hello World";]
x--;
post-decrement shortcuts for x = x - 1
x++;
post-increment (returns value of old x) shortcuts for x = x + 1
--x;
pre-decrement
++x;
pre-increment (returns reference to new x)
escape sequences
represent single characters that cannot be represented with a single character from the keyboard in your code -the backslash \ is the indicator of an escape sequence; \ and the next character are considered one item together
associativity
rules specifying which operators are evaluated first when they have the same level of precedence (mostly left to right)
precedence
rules specifying which operators come first in a statement containing multiple operators
output devices
screen/console, printer, network card
statement
smallest complete executable unit of a program -declaration statement -execution statement -compound statement: enclosed in {}, called a block -simple C++ statements end with a semi-colon
interpreted languages
source code is directly run on an interpreter, a program that runs code statements
cin
standard input stream -of class type istream -usually defaults to the keyboard [int numStudents; cin >> numStudents; //read an integer]
cout
standard output stream -of class type ostream -usually defaults to the monitor [cout << numStudents; //prints variable contents]
explicit type conversion (casting)
static_cast c1 = static_cast<char>(i1); i1 = static_cast<int>(d2);
conditional operator
test_expression ? true_expression : false_expression -test_expression is evaluated for true/false -if true, returns true_expression -if false, returns false-expression cout << (x > y ? "x is greater than y" : "x is less than or equal to y");
syntax
the grammar of a language
semantics
the meaning of language constructs
initializing variables
to load a value into the variable for the first time
declaring variables
to tell the compiler exists, and to reserve memory for it
library <iostream>
to use these streams (cin, cout), we need to include this library into our programs #include <iostream> using namespace std;
selection
used for decisions, branching -- choosing between 2 or more alternative paths -if -if/else -switch
repetition
used for looping -- repeating a piece of code multiple times in a row -while -do/while -for
long
usually 4 or more bytes
short
usually at least 2 bytes
library
usually pre-compiled code available to the programmer to perform common tasks -interface: header file, which contains names and declarations of items available for use -implementation: pre-compiled definitions, or implementation code. In a separate file, location known to compiler
v %= e;
v = v % e;
v *= e;
v = v * e;
v += e;
v = v + e;
v -= e;
v = v - e;
v /= e;
v = v / e;
assignment operator
value on the right side (R-value) is stored in the location (variable) on the left side (L-value) variable_name = expression x = 5; z = x + y;
constant variables
variable cannot change once it's declared and initialized -must use keyword const -must declare and initialize on the same line -it's common to name constants with all caps but not required [const int SIZE = 10;]
while loop format
while (expression) statement;
arithmetic operators
+ add (x + y) - subtract (x - y) * multiply (x * y) / divide (x / y) % modulus (x % y) - minus (-x)
compiled languages
-a compiler program translates source code (what the programmer writes) to machine language (object code) -a linker program puts various object code files together into an executable program (or other target type, like a DLL) -[ex: C and C++]
High-level procedural languages
-abstraction of concepts into more human-readable terms -closer to "natural language" -easy to write and design, but must be translated for computer -[ex: C, Pascal, Fortran]
Object-oriented languages
-abstraction taken farther than procedural languages -objects model real-world objects, not only storing data (attributes), but having internet behaviors (operations, functions) -easier to design and write good, portable, maintainable code [ex: Smalltalk, C++, Java]
automatic type conversions
-atomic data types can go from "smaller" to "larger" types when loading a value into a storage location -allowed if no chance for partial data loss char -> short -> int -> long -> float -> double -> long double
Machine Language
-based on machine's core instruction set -needed by computer, hard for humans to read (1's and 0's) -[ex: 1110110101011]
Atomic Data Types
-integer types -floating point types -bool
assignment examples
-one common way to initialize variables int numStudents; double weight; char letter; numStudents = 10; weight = 160.35; letter = 'A';
General structure of a C++ program
-sequence of statements, typically grouped into functions -can consist of multiple code files and libraries -a program starts with main() -statements -library
Assembly Language
-translation of machine instructions to symbols -slightly easier for humans to read -[ex: ADD $R1, $R2, $R3]
char
1 byte on most systems -typically used for representing characters -stored with an integer code underneath
bit
a binary digit -stores the value 0 or 1 -smallest unit of storage in a computer
character literal
a character in single quotes ('F', 'a')
L-value
a storage location; a variable or a reference to a location
string literal
a string in double quotes ("Hello", "Bye", "Wow!\n")
floating point literal
an actual decimal number written in code (4.5, -12.9, 5.0) -interpreted as type double -can be written in exponential notation (3.12e5)
integer literal
an actual integer number written in code (4, -10, 18) -if an integer literal is written with a leading 0, it's interpreted as an octal value (base 8) -if an integer literal is written with a leading 0x, it's interpreted as a hexadecimal value (base 16) [int x = 26; //26 int y = 032; //octal 32 = decimal 26 int z = 0x1A; //hex 1A = decimal 26]
\'
escape sequence meaning single quote
\t
escape sequence meaning tab
while, do/while test expression
expression is a test condition evaluated to decide whether the loop should repeat or not; true: run loop body again, false: quit
boolean operators
&& (and) || (or) ! (not)
creation & execution of a C++ program
1. create a source code, a plain text file, usually given a filename extension to identify the programming language, with a text editor, to store disk [.c for C & .cpp for C++] 2. preprocessor: part of compiler process, performs any pre-processing tasks on source code 3. compilation: syntax checking, creation of object code, the machine code translation of the source code 4. linking: final stage of the creation of an executable program. Linking of object code files together with any necessary libraries (also already compiled) 5. execution of program -program loaded into memory, usually RAM -CPU executes code instructions
int
4 bytes on most systems
byte
8 bits -smallest addressable unit of storage in a computer -storage units (variables) in a program are 1 or more bytes -each byte in memory has an address (a number that identifies the location)
logical operators
== (equal to) != (not equal to) < (less than) <= (less than or equal to) > (greater than) >= (greater than or equal to)
RAM
Random Access Memory -main memory -storage close to CPU -faster to access than hard disk -stores executing programs and data being currently worked on
compound statement
can enclose multiple code statements -enclosed in set braces {}
integer types
char, short, int, long
fixed format
cout << fixed; cout << showpoint; cout << setprecision(3);
scientific format
cout.setf(ios::scientific);
floating-point numbers
decimals will print out as far as needed
sequential
default mode; sequential execution of code statements
#include
directive used to make a library part of a program (satisfies declare-before-use rule)
do/while loop format
do { statement1; statement2; } while (expression);
break statement
end the case with this if you want to execute code only in the case that you jump to
\a
escape sequence meaning alert sound
\\
escape sequence meaning backslash
\r
escape sequence meaning carriage return
\"
escape sequence meaning double quote
\n
escape sequence meaning newline