Chapter 4 Flashcards
relational operators
allow comparisons of numeric and char values and determine whether one is greater than, less than, equal to, or not equal to another.
Ex: num1 > num2; num1 != num2;
flag
a boolean or integer variable that signals when a condition exists
if (flag ) // same as if (flag == true)
run code;
if (integerFlag) // if 0 = false
if (integerFlag) // if nonzero = true
Ex: salesQuotaMet = 0; // this is an integer flag = 0 = false = sales quota NOT met…
salesQuotaMet = 1 = nonzero = true = sales quota met
conditional operator
create short expressions that work like if/else statements
expression1 ? expression2 : expression3;
same as
if (expression1) expression2;
else expression3;
switch statement
lets the value of a variable or an expression determine where the program will branch.
switch (variable or expression) {
case constant1: code; // if variable or expression equals constant 1 run code. No break; so program will include the next line of code
case constant2: code; break; // if variable of expression equals constant 2 run code. contains break which will exit the switch.
default: code; // if variable or expression does not equal any case run this code. no need for break
}
Logical Operators
connect two or more relational expressions into one or reverse the logic of an expression.
&& AND || OR ! NOT
&&
The logical AND operator. Takes two expressions as operands and creates an expression that is true only when both subexpressions are true.
||
The logical OR operator. Takes two expressions as operands and creates an expression that is true when either of the subexpressions are true.
!
The logical NOT operator. It takes an operand and reverses its truth or falsehood.
Precedence and Associativity of Logical Operators
High to Low
!
&&
||
Menu-driven program
allows the user to determine the course of action by selecting it from a list of actions.
Validating User Input
the process of inspecting data given to a program by the user and determining if it is valid.
What is the difference between if/else if statement vs a series of if statements
The if/else if statement tests a series of conditions. It is often simpler to test a series of conditions with the if/else if statement than with a set of nested if/else statements. The if/else if statement is more human readable.
switch statement with initialization
switch (Initialization; IntegerExpression) {
case ConstantExpression: code; break;
}
switch (int value = abs(number); value) {
case 1: code; break;
}
nested if statement
To test more than one condition, an if statement can be nested inside another if statement.
Blocks and Variable Scope
the scope of a variable is limited to the block in which it is defined.