Middler
VARIABLE DECLARATIONS
A variable is a named storage location in memory. The line of code where a variable is defined is the variable declaration.
int item;
CONSTANT DECLARATIONS
A variable whose value cannot be altered anywhere in the program.
const double pi = 3.14;
DATA TYPES
INT
‘CHAR’
“STRING”
FLOAT: Float(4bytes), Double (8bytes), Long Double
BOOL: True (1) or False (0)
VARIABLE INITIALIZATION AND ASSIGNMENT
An assignment statement uses the = operator to store a value in a variable
int length = 12;
COMPOUND ASSIGNMENT
num += 3;
ORDER OF OPERATIONS (+)
()
- (unary negation)
*, /, %
+, -
USING MATHEMATICAL OPERATORS
Mathematical expressions can be created using multiple mathematical operators.
Parentheses can be used to override the order of operations.
USING RELATIONAL OPERATORS
Relational Expressions are made with Relational Operators to make a true/false determination (boolean expressions)
<, >, >=, <=, ==, !=
USING LOGICAL OPERATORS
NOT !: reverses the value of a bool expression
AND &&: true if both bool expressions are true
OR ||: true if either expression is true
*ordered in highest precedence
TRACING IF ELSE STATEMENTS
😀
NESTED IF STATEMENTS
if (expression) {
if (expression) {
statement;
}
}
SWITCH STATEMENTS
Used to select among statements from several alternatives.
switch (exp [integer]) {
case exp1: statement1;
case exp2: statement2;
default: (optional);
}
*include break
TYPE CONVERSIONS (IMPLICIT)
(Type coercion) Automatic conversion of an operand to another data type. Promotion (convert to a higher data type) or Demotion. Lower is promoted to higher.
float val1 = 3.6;
int val2 = 2;
float result = val1*val2;
*val2 remains int
TYPE CONVERSIONS (EXPLICIT)
Manual type conversion. Useful for floating point division with ints.
static_cast<type>(expression)
CURLY BRACKET INCLUSION/EXCLUSION
Use curly brackets when executing more than one statement.
if (score > 90)
grade = ‘A’;
if (score > 90) {
grade = ‘A’;
cout << “Good job!”;
}
FORMATTING OUTPUT
setw(x): prints in a field at least x spaces wide
fixed: displays floating point numbers in fixed point notation
showpoint: causes a decimal point and trailing zeros to be displayed, even if there is no fractional part
setprecision(x): when used with fixed, print floating point value using x-digits after the decimal
<< left: output left justified
<< right: output right justified
\ escape character
cout << right << setw(6) << setprecision(2) << fixed << num;
WHILE LOOP
A loop allows us to write code that repeats code until a condition is met. Can be infinite if nothing makes the expression false.
while (expression) {
statement;
}
DO WHILE LOOP
A posttest loop, executes the loop first then tests the expression.
do {
statement;
} while (expression);
FOR LOOP
A more compact way of writing a while loop. Good when you know in advance how many times to repeat.
for (initialization; test; update) {
statement;
}
for (count = 10; count < 20; count++ {
cout << count;
}
HIERARCHY OF TYPES (+)
Long Double
Double
Float
Unsigned long
Long
Unsigned int
Int
Unsigned Char
Char
*unsigned means no negative value
TERNARY CONDITIONAL OPERATOR
Most frequently used to perform an assignment
expr (tested) ? expr (done if true) : expre (done if false);
WHAT IS OUTPUT WHEN ‘F’ IS ENTERED?
Report to the student union.
Report to the student attendance office.
Report to the science building.
WHAT IS OUTPUT WHEN ‘M’ IS ENTERED?
Go home
WHAT IS OUTPUT WHEN ‘D’ IS ENTERED?
Report to the small gym.
WHAT IS OUTPUT WHEN ‘I’ IS ENTERED?
Report to the student attendance office.
Go home
GIVEN THE FOLLOWING CODE, HOW MANY TIMES IF “GOT ONE!” PRINTED?
2
GIVEN THE FOLLOWING CODE, FOR WHAT VALUES OF i WILL “GOT ONE!” BE PRINTED?
6 and 9
CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 15?
AD
CONSIDER THE FOLLOWING CODE SEGMENT. UNDER WHAT CONDITIONS WILL ‘C’ BE PRINTED?
No conditions allow it
CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 1?
F