Exam 1 Flashcards
(9 cards)
data types/variables
bool
char
int
double
string
etc.
integer division
any fractional remainder of the answer is rounded down to the nearest integer
ex: 5/4 = 1 vs. 5.0/4.0 = 1.25
use casting to prevent this
int x = 5;
double y = 4;
double z = (double)x / y; // z = 1.25 since we cast x as a double
variable name/identifier
must
- be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9)
- start with a letter or underscore
ex:
userName
pickleColor
pickle_color
function
list of statements that can be executed by referring to the function’s name
function(argument)
constants
const
- declare the variable as constant when you have values that are unlikely to change
- when you declare a constant variable, it must be assigned with a value
ex:
const int minutesPerHour = 60;
boolean types
declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
character types
declares a character (any kind: 3, &, a, A, -, etc) enclosed in single quotes (‘’)
ex:
char myLetter = ‘c’;
string type
A string literal may contain spaces, periods, and other non-letter characters.
string literals are enclosed in “” (double quotes)
casting
A type cast explicitly converts a value of one type to another type.
The static_cast operator –>
(static_cast<type>(expression)) converts the expression's value to the indicated type.</type>
Ex: If myIntVar is 7, then static_cast<double>(myIntVar) converts int 7 to double 7.0.</double>