ch4 csci 1933
What are the three logical operators?
-&& (AND) - | | (OR) - !(not)
Which value can be entered to cause the following code segment to display the message: "That number is acceptable." int number; cin >> number; if (number > 10 && number < 100) cout << "That number is acceptable.\n"; else cout << "That number is not acceptable.\n";
-99
Assuming x is 5, y is 6, and z is 8, which of the following is false? 1. x == 5; 2. 7 <= (x + 2); 3. z < = 4; 4. (1 + x) != y; 5. z >= 8; 6. x >= 0; 7. x <= (y * 2)
3 and 4 are false.
How does an if statement work?
Allow statements to be conditionally executed or skipped over
Why isn't there an expression/condition for the F grade?
Because it is a trailing clause. It will be used assign a grade to any test score including negatives.
True/False: As a rule of style, when writing an if statement you should indent the conditionally-executed statements.
TRUE
True/False: If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.
TRUE
True/False: If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.
TRUE
True/False: You should be careful when using the equality operator to compare floating point values because of potential round-off errors.
TRUE
Why is indentation important?
Tell the computer how to group the code into blocks to be run in a specific order
When an if statement is placed within the conditionally-executed code of another if statement, this is known as:
nesting
How does the logical NOT work?
reverses the value of an expression, true ones will become false & false ones will be true
What is a break statement?
used to exit a switch statement
Out of the three logical operators, which has the lowest precedence?
| |, has the lowest precedence
After execution of the following code, what will be the value of input_valueif the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15;
15
This operator is used in C++ to represent equality.
==
What is a Boolean expression?
A boolean expression is an expression that can be evaluated as true or false. These expressions are also called relational expressions.
If you intend to place a block of statements within an if statement, you must place these around the block.
Curly braces { }
How does the logical AND work?
Expression can only be true if both statements are true if one is false the whole relation is false.
How is the if/else statement different from the if statement?
The if else statement is able to provide two paths of execution. it performs one statement if the expression is true, if it is false it will perform another statement.
What kind of structure is the if statement?
The if statement is a decision structure.
What is the purpose of relational operators?
The purpose of relational operators is to form the expression that appears inside the parenthesis of if statements.
How does an if-else statement work?
The purpose of the braces after the if statements is needed if you want to execute multiple statements.
What is the purpose of a nested if statement?
They can be used to test more than one condition
How can you use logical operators to validate user input?
You can use it to test range, reasonableness, valid menu choice, and divide by 0. Inspecting input data to determine whether it is acceptable.
True/False: The following code correctly determines whether x contains a value in the range of 0 through 100. if (x >= 0 && <= 100)
false
What is the value of the following expression? true && false
false
What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl;
false
How does the logical OR work?
if one relation is true then both relations are true.
When a program lets the user know that an invalid choice has been made, this is known as:
input validation
These operators connect two or more relational expressions into one, or reverse the logic of an expression.
logical
What are relational operators?
used to compare numbers to determine relative order <(greater than), >(less than), >= (greater than or equal), <=(less than or equal), == (equal), != (not equal)
How are characters compared?
using their ASCII values
What should you do if you want more than one statement executed within an if statement?
you should enclose them in {}. that symbol creates a block of code
When a relational expression is false, it has the value ________.
zero
How does a switch statement work?
-used to select among statements from several alternatives -First the expression is evaluated, second the value of expression is compared against exp1 thru expN, then if expression matches EXPi the program branches to the statement following EXPi, and continues to the end of the switch, lastly if no matching value is founds the program will then branch to the statement after default.
After execution of the following code, what will be the value of input_valueif the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15;
15
What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2;
20