Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
52 Cards in this Set
- Front
- Back
What do relational operators allow you to do? |
compare numeric and char values and determine whether one is greater than, less than, or equal to the other |
|
What does each of these relational operators mean? Are they unary, binary, or ternary? What is their associativity? > < >= <= == != |
binary exactly what I think they mean Left-to-right associativity == means equal to != means not equal to |
|
What does this expression mean? x>y |
Is x greater than y? Relational operators ask a question |
|
Relational expressions are ___________ expression |
Boolean value of the expression is true or false |
|
What is the difference between the equality operator and the assignment operator?
|
assignment: = equality: == |
|
How many relationships does >= test for? >? |
>= tests for two relationships (> and ==) > tests for one |
|
What number represents true? What number represents false?
|
True: 1 (but one is not the only number regarded as true) False: 0 |
|
What does the if statement do?
What is its general format? |
causes other statements to execute only under certain conditions. If the value in the parentheses is true, it executes the statement. If it is false, it skips the statement if (expression) statement; |
|
Compare sequence structure to decision structure |
Sequence structure: statements are executed in sequence without branching off in another direction
Decision structure: programs only execute certain algorithms under certain conditions. Actions are conditionally executed |
|
What is a null statement? |
An empty statement that does nothing if (expression); |
|
For stylistic purposes, how is the if statement formatted?
|
Even though it is really one single statement, it should be: if (expression) statement; |
|
Why should you avoid using the equality operator to compare floating-point numbers? What should you do instead? |
some fractional numbers cannot be exactly represented using binary and lead to round-off errors use < and > to compare floating-point numbers |
|
For if statements, what is considered true and what is considered false?
For relational expressions, what is true and what is false? What is the bool value false equivalent to? |
if -- true: anything other than 0 if -- false: 0 (or bool false) relational -- true: 1 relational -- false: 0 bool false = 0 |
|
Relational expressions are not the only thing that can be tested by if statements. Give an example of something else that the if statement can test
|
if (value) cout << "It is true!"; if the value is anything other than 0, the statement will execute |
|
How can you conditionally execute a block of code using an if statement?
|
if (expression) { statement; statement; statement; } |
|
Without braces, what does does if conditionally execute? |
the very next statement |
|
What does an if/else statement do? What is the format?
|
if (expression) statement or block; else statement or block; else is at the same indentation level as if |
|
How can if/else be used to prevent division by zero?
|
If (division by zero) statement; else statement; <--division by zero does not occur |
|
What is the purpose of nesting if statements? What is the format?
|
Each if statement is indented |
|
What is a simpler alternative to nested if/else statements? What is the format?
|
if (expression_1) { statement; } else if (expression_2) <-- evaluated if above are false { statement; } else if (expression_3) <-- evaluated if above are false { statement; } else { statement; <--- executed if none of the conditions above are true } |
|
When are braces required for if, if/else, if/else if statements?
|
Only when two or more statements are conditionally executed |
|
Is the trailing else mandatory? What are the benefits?
|
Useful for catching errors when none of the other conditions are true and displaying an error message |
|
What are two disadvantages of if/else statements compared to if/else if? |
- if/else statements will eventually wrap around after a certain number of indentations |
|
What is a flag?
|
false: condition does not exist |
|
How do you create/use integer flags? |
1 = true --> flag. condition exists 0 = false |
|
What do logical operators do?
|
connect two or more relational expressions into one, or they reverse the logic of the expression |
|
What are the three logical operators? What do they do? What is the format?
|
|| OR connects two expressions into one. One or both must be true for overall condition to be true. Does not matter which. ! NOT reverses the "truth" of an expression (!(x<100)) <-- inside evaluated, then truth is reversed |
|
What is short circuit evaluation? When and why does it occur? |
&&: first one is false, second won't be checked ||: first one is true, second won't be checked Saves processing power/time |
|
What is the order of precedence of the logical operators? What is their associativity? |
&& || left-to-right |
|
The ! operator has a higher precedence than many C++ operators. What should you do to avoid an error?
|
(!(expression)) |
|
What is the best way to determine if a number is in range?
Out of range? |
In range: && x >= #1 && x<=#2
Out of range: || x < #1 || x > #2 |
|
Is 5 < x < 20 valid in C++? |
NO connect the two separate expressions with && |
|
What is a menu driven program do? How do you create one? |
Allows a user to choose from a list of options nested if/else if statements |
|
What is input validation? |
process of inspecting data given to the program by the user and determining if it is valid Don't assume the user followed the input instructions. Check the data. else statements are great for this when none of the other conditions are met |
|
Can relational operators be used to compare characters and string objects? |
Yes. They all have ASCII values |
|
Which has higher-value ASCII codes, uppercase or lowercase? |
lowercase |
|
How can you determine whether one string object is alphabetically before another string object?
|
< relational operators compares the first character of each. If they match, it moves on to the second. If they match, it moves on to the third. Stops comparing once there is a mismatch and determines which is first alphabetically. The one that is first alphabetically will have a lower ASCII score |
|
True or false: '1' == 1 |
false the character 1 has an ASCII value different than the integer 1 |
|
What does the conditional operator do? How many operands does it require? What is the format? |
? --> conditional operator. ternary operator (3 operands) create short conditional expressions that work life if/else statements expression1? expression2 : expression3;
expression 1 is tested expression 2 executes if true expression 3 executes if false parentheses are helpful but not required (x<20)? (a=2) : (a=3) If x<20, a=2, else a=c |
|
In C++, all expressions have a ____________, and this includes conditional expressions Explain in the context of j = k < 90? 25 : 32; |
value sets j equal to the value of the conditional statement (25 if less than 90, 32 if greater than or equal to 90) |
|
cout << "Your grade is: " << (score < 60? "Fail." : "Pass.");
What are the () around the conditional operator necessary?
What is the if/else equivalent of this statement? |
<< has a higher precedence than the conditional operator. () ensure that the correct string value of the grade is outputted after the condition is evaluated
if (score < 60) cout << "Your grade is: Fail."; else cout << "Your grade is: Pass.";
|
|
When does a branch occur? What does a switch statement do?
|
A branch occurs when one part of the program causes another part to execute The switch statement lets the value of a variable or expression determine where the program will branch |
|
What is the general format of a switch statement? |
switch (IntegerExpression) { case ConstantExpression: statement; statement; break; case Constant Expression: statement; statement; break; default: statement; } |
|
Explain the parts of a switch expression:
- switch (integerexpression) - case constantexpression - break - default |
- integer expression can be a variable of an integer data including char, or an expression whose value is an integer data type - constant expression of the case statement: constant expression must be an integer literal or an integer named constant. Program branches to a group of statements after the case expression matches - break: case statements show the program where to start executing, and break statements show it where to stop - default: break isn't necessary after it but it's nice for consistency |
|
What is one of the best features of switch statements? How do you take advantage of it? |
Fall through If break statements are carefully, consciously omitted, then all statements after the first matching case statement until the first break statement will be executed e.g., three levels of a model. level 3 has all 3 feature --> fall through all 3. Level 2 has 2 features --> fall through 2. Level 1 has 1 --> last feature only also useful if you want a bunch of cases to have the same expression case 'a': case 'A': statement; break; |
|
Is the default section of a switch statement required? |
no |
|
Can the expression following the word case be a variable, relational expression, or logical expression? |
No must be an integer literal or an integer named constant |
|
What is the scope of a variable limited to? |
the block in which it is defined |
|
When is it a good idea not to define variables at the top of a program? Where should they be defined instead? |
Useful in longer programs Define near the part of the program where they're used so the purpose of the variable is more evident |
|
Variables defined inside a set of braces have ____ _____ or _____ ______ |
local scope or block scope |
|
T or F: When a block is nested inside another block, a variable defined in the inner block may have the same name as a variable defined in the outer block. As long as the variable in the inner block is visible, the variable in the outer block will be hidden |
true, but it's still bad practice to reuse variable names |
|
How do you arrange for console insertions and keyboard extractions of Boolean values to print/read as true/false rather than 1/0? |
cout << boolalpha; cin >> boolalpha; persists throughout program |