coding - Practical 7 Flashcards
Why might a programme never exit on its own?
By explicitly specifying the condition of the while loop as true.
How can the programme be controlled at run-time?
As we learned with if statements, it is possible to insert a conditional statement in place of true.
Describe std::string name = “”;
We refer to variables that hold text as strings, or more specifically we use the variable type std::string.
How can you compare the value of a string?
By using the == operator:
What does the logical AND operator (&&) allow?
It allows you to connect multiple conditional statements such that they can be tested as one big conditional expression.
Describe how the logical AND (&&) operator works.
- If both the conditional statements either side of the logical AND (&&) operator are true, the outcome of the entire conditional expression is true and the oscillator is turned on.
- If either, or both of the statements are false, then the outcome of the entire expression is false and the call to aserveOscillator() is skipped.
What can the logical OR (||) operator be used to test?
Whether a series of the conditions are true. (e.g. “yes” or “YES”)
Describe how the logical OR (||) operator works.
If one (or any) of the conditional statements on either side of the logical OR operator (||) are true, the outcome of the entire conditional expression is true and therefore the loop will continue to run.
What does the logical NOT operator (!=) do?
Inverts the outcome of a conditional expression. For example, printing ‘funds available’ when the variable cash is not 0.
Why can’t you be sure on the order in which the AND and OR will be executed?
They have the same order of precedence. When chaining together multiple logical operations it is best practice to place them in brackets.
What can a switch/case structure often be used in place of?
A multi-way selection if…else if structure - creates more readable and efficient flow control.
When is a switch/case structure the preferred method?
If an if…else if structure tests an integer variable against a number of constant values
What does ‘break;’ do in a switch/case?
The switch/case structure terminates. If the variable does not match a specified case, this default case is executed.