Flow Control Flashcards

1
Q

What is a program’s path?

A

The sequence of statements the CPU executes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does “sequential flow” refer to?

A

A program that has the same path each time it is run (i.e. no logic)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a halt control flow statement?

A

The most basic control flow statement; tells the program to stop running immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax for a function which causes a halt, and what library is it defined in?

A

exit(int exitCode);

This is similar to the way ‘main’ returns an integer.

Defined in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a jump control flow statement?

A

Causes the CPU to unconditionally jump to another statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some examples of jump keywords?

A

goto, break, continue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe what happens when a function cannot handle an error.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the best syntax practice when an if statement executes a single statement, as opposed to multiple statements?

A

Use a block for multiple statements {}, and no block for single statements:

if (nX > 10)
cout &laquo_space;nX;

if(nx > 10)
     {
           cout << nX;
           cout << nY;
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What must the condition of an if statement be enclosed in?

A

(parentheses)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a “dangling else”, and how can they be avoided?

A

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 &laquo_space;nX &laquo_space;“is between 10 and 20” &laquo_space;endl; }
else // attached to outer if statement
cout &laquo_space;nX &laquo_space;“is less than 10” &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an early return?

A

Using a return statement to end a function and return a value before all of its statements have been executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When is a good time to use a switch statement?

A

When many else if statements are used; switch has better readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the flow of a switch statement.

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F: The expression of a switch statement must evaluate to an integral type (char, short, int, long, enum).

A

T; no floats or non-integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a constant expression?

A

An expression that evaluates to a literal, an enum, or a variable assigned a constant integral value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is the constant expression given with a case executed?

A

If the case evaluation is equal to the switch evaluation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What must be done if we do not want multiple cases to be executed?

A

A return, break, or goto statement must end the case.

18
Q

When is the ‘default’ keyword in a switch statement executed?

A

Only executed if no other case evaluates to true.

19
Q

What is “fall-through”?

A

When multiple cases are erroneously executed b/c they all evaluate to true and no return, break, or goto is present to prevent multiple case statements from executing.

20
Q

When a break statement is used, where does the program resume?

A

At the first statement following the end of the block.

21
Q

What is the proper syntax of a switch statement?

A
switch(evaluated_statement)
{
  case (value_possibility1):
     executed statements;
     break;
  case (value_possibility2):
      executed statements;
      break;
   .
   .
   .
   default:
        executed statements;
        break; OR exit(int code);
}
22
Q

What does a goto statement require, and what is its syntax?

A

A statement label, with the syntax:

myIdentifier:

23
Q

Given a statement label, what does a goto statement do?

A

Jumps to the statement label.

24
Q

What is one good use of a goto statement?

A

To check for correct input and keep prompting otherwise.

25
Q

T/F: The goto statement can jump anywhere, as long as a statement label is present.

A

F; The statement label must appear in the same function as the goto statement (function scope), and must not skip over any important declarations.

26
Q

What is the best practice regarding goto statements?

A

Avoid them unless necessary.

27
Q

What is the syntax of a while loop?

A

while (condition)
{
some code/iteration;
}

28
Q

What are the ways to break out of an infinite loop?

A

A return statement, a break statement, an exception being thrown, or the user killing the program.

29
Q

T/F: Iterator variables (aka loop variables) should be only 1 or two characters.

A

F; They should be specific to avoid ambiguities across programs.

30
Q

What is the syntax of a do loop?

A

do
{
mycode;
} while (condition);

31
Q

What is the syntax of a for loop?

A

for(Init statement; evaluation statement; iterator statement)
{
Iteration code;
}

32
Q

What is the scope of loop variables declared in a for loop?

A

Loop scope.

33
Q

T/F: Expressions cannot be omitted when writing a for loop.

A

F; Only the semicolons are required:

int iii=0; 
for ( ; iii < 10; )
 { 
    cout << iii << " ";    
    iii++; 
} 

This for loop produces the result:

0 1 2 3 4 5 6 7 8 9

34
Q

T/F: A for loop can have multiple declarations.

A

T;

for (int iii=0, jjj=9; iii < 10; iii++, jjj–)
cout &laquo_space;iii &laquo_space;” “ &laquo_space;jjj &laquo_space;endl;

35
Q

What is the difference between break and continue?

A

Break exits the loop entirely, whereas continue ends the current iteration (goes back to the top of the loop).

36
Q

What is a seed?

A

A starting number for a pseudo-random number generator (PRNG) which is transformed by the PRNG algorithm.

37
Q

What is the difference between srand() and rand(), and what is the name of the library in which they are included?

A

srand() should only be called once to set the initial seed value, and rand() generates the next random number in the sequence starting from the seed value set by srand()

Both are included in the cstdlib library.

38
Q

What is one way to convert the result of rand() to a random number in a given range?

A

rand() % (nHigh - nLow + 1)) + nLow;

39
Q

What is an example of a situation where you would WANT to set the seed and get reproducible results?

A

When testing a simulation; eliminate random element to fix other problems (i.e. debugging a random dungeon generator)

40
Q

How can time() be used to make an effective PRNG, and what library is it in?

A

Pass the value of the system clock, time(0), to srand():

using namespace std;

int main()
{
srand(time(0)); // set initial seed value to system clock
for (int nCount=0; nCount < 100; ++nCount)
{
    cout << rand() << "t";

    if ((nCount+1) % 5 == 0)
        cout << endl; } }

time() is included in the ‘ctime’ library.

41
Q

What are four qualities of a good PRNG, and does rand() exhibit these?

A

1) Distribution uniformity, or the quality of the PRNG generating any two numbers with equal probability.
2) Unpredictable/non-obvious sequence (the numbers generated after the initial seed value should not have an easily discernible pattern)
3) Dimensional distribution, or the equal probability of returning a low, mid, or high number
4) A long period (a PRNG should not repeat a value already returned for a very long time, because once it does, it will begin to repeat itself.

rand() is NOT a good PRNG, as it does not exhibit all of these properties.