Flow Control Flashcards
What is a program’s path?
The sequence of statements the CPU executes.
What does “sequential flow” refer to?
A program that has the same path each time it is run (i.e. no logic)
What is a halt control flow statement?
The most basic control flow statement; tells the program to stop running immediately.
What is the syntax for a function which causes a halt, and what library is it defined in?
exit(int exitCode);
This is similar to the way ‘main’ returns an integer.
Defined in
What is a jump control flow statement?
Causes the CPU to unconditionally jump to another statement.
What are some examples of jump keywords?
goto, break, continue
Describe what happens when a function cannot handle an error.
If an error occurs that the function can not handle, it can raise an exception, and control jumps to the nearest block of code that has declared it is willing to catch exceptions of that type.
What is the best syntax practice when an if statement executes a single statement, as opposed to multiple statements?
Use a block for multiple statements {}, and no block for single statements:
if (nX > 10)
cout «_space;nX;
if(nx > 10) { cout << nX; cout << nY; }
What must the condition of an if statement be enclosed in?
(parentheses)
What is a “dangling else”, and how can they be avoided?
When nesting if/else statements, it can become unclear which if belongs to which else; these can be made less ambiguous by always using blocks when nesting statements. Without a block, the else simply would attach to the nearest if:
if (nX > 10)
{
if (nX < 20)
cout «_space;nX «_space;“is between 10 and 20” «_space;endl; }
else // attached to outer if statement
cout «_space;nX «_space;“is less than 10” «_space;endl;
What is an early return?
Using a return statement to end a function and return a value before all of its statements have been executed.
When is a good time to use a switch statement?
When many else if statements are used; switch has better readability.
Explain the flow of a switch statement.
The switch expression is evaluated to produce a value, and each case label is tested against this value for equality. If a case label matches, the statements after the case label are executed. If no case label matches the switch expression, the code under the default label is executed (if it exists).
T/F: The expression of a switch statement must evaluate to an integral type (char, short, int, long, enum).
T; no floats or non-integers
What is a constant expression?
An expression that evaluates to a literal, an enum, or a variable assigned a constant integral value.
How is the constant expression given with a case executed?
If the case evaluation is equal to the switch evaluation