## Sequence, Selection and Loop Structures

Published:  June 19, 2007
By Richard G. Baldwin

File: Alice0165PracticeTest.htm

## Questions

1.  What is the value of x after the code in Listing 165-101 is executed?  (Hint:  ignore the word times.  It is misleading.)

Listing 165-101.
 ```public void main ( ) { Number x = 0 ; for (int index=2; index< 7 times ; index++) { x .set( value , ( ( x + index ) ) ); } print( x ); }```

• A. 0
• B. 14
• C. 20
• D. 27

2.  The loop shown in Listing 165-102:

Listing 165-102.
 ```public void main ( ) { Number count = 1 ; Number sum = 0 ; while ( ( count < 10 ) ) { sum .set( value , ( ( sum + count ) ) ); count .set( value , ( ( count + 1 ) ) ); } }```

• A. will exit (terminate) when count > 10.
• B. will exit (terminate) when sum > 10.
• C. will exit (terminate) when count = 10.
• D. will result in an error.

3.  You have designed a program and have a number of steps which must be executed multiple times.  What is the program control structure most likely to be used to do this?

• A. sequence
• B. selection
• C. repetition or loop
• D. procedure

4.  True or False:  Structured programming is not important in modern computer programming.

5.  True or False:  Any programming logic problem can be solved using an appropriate combination of only three programming structures.

6.  True or False:  The three structures required by structured programming are known generally as:

• The sequence structure.
• The selection or decision structure.
• The loop, repetition, or iteration structure.

7.  True or False:  In structured programming, a section of program code must have only one entry point, but there may be multiple exit points.

8.  True or False:  In structured programming, structures may be nested inside of other structures provided that every structure meets the basic rules for a structure.

9.  True or False:  The general requirement for the sequence structure is that one or more actions may be performed in sequence after entry and before exit.

10.  True or False:  In a sequence structure, one or more of the action elements may themselves be sequence, selection, or loop structures.

11.  True or False:  The selection or decision structure can be described as shown in the pseudocode in Figure 165-11.

Figure 165-11. The selection structure in pseudocode.
 ```Enter Test a condition for large or small On large Take one or more actions in sequence On small Take none, one, or more actions in sequence Exit ```

12.  True or False:  For a selection structure, when all of the actions for a chosen branch have been completed in sequence, control is transferred to the other branch of the selection structure.

13.  True or False:  For a selection structure, it is often the case that no action is required when the test returns false.  In that case, control simply exits the structure without performing any actions.

14.  True or False:  The loop or iteration structure can be described as shown in the pseudocode in Figure 165-14.

Figure 165-14. The loop structure in pseudocode.
 ```Enter Test a condition for true or false Exit on false On true Perform one or more actions in sequence. Go back and test the condition again ```

There is only one entry point and one exit point for a loop structure.

15.  True or False:  For a loop structure, unless something is done in one of the actions in the body of the loop to cause the test to eventually return false, control will never exit the loop.

In this case, the program will be caught in what is commonly referred to as a finite loop.

16True or False:  In some programming languages, there are a couple of structures other than sequence, selection, and loop that structured-programming experts are willing to accept for convenience:

• The switch-case structure.
• The do-until structure.

These structures are not required for the solution of programming logic problems.  Also, they are not support by Alice 2.0.

17True or False:  To create a selection structure in Alice, begin by dragging the if tile from the object tree and dropping it at the appropriate location in the edit pane.

18True or False:  When constructing a selection structure in Alice 2.0, the expression that you place in the conditional clause is the expression that will be evaluated to determine which part of the selection structure to execute.  If the expression in the conditional clause evaluates to true, the code between the curly braces before the else clause will be executed.  If the expression in the conditional clause evaluates to false, the code in the body of the else clause will be executed.

19True or False:  To create a while loop structure in Alice, begin by dragging the loop tile from the bottom of the Alice development screen and dropping it at the appropriate location in the edit pane.

20True or False:  As is the case with a selection structure, a loop structure has a conditional clause.

21True or False:  Once the skeleton for a loop structure appears in your Alice code, you can replace the placeholder value with any expression that will evaluate to a value of type Number.

Answer and Explanation

22True or False:  The expression that you place in the conditional clause of a loop structure is the expression that will be evaluated to determine if the code in the body of the loop structure (the code between the curly braces) will be executed one time, or if the loop structure will terminate.

Answer and Explanation

23True or False:  If the expression in the conditional clause of a while loop structure evaluates to false, the code in the body of the loop will be executed once.  If the expression in the conditional clause evaluates to true, the loop will terminate immediately causing the code in the body of the loop to be skipped and causing the next statement following the body of the loop to be executed.

Answer and Explanation

24True or False:  The purpose of a priming read is to get the first input value before entering the loop so that the value can be used to determine the behavior of the loop during the first iteration.

Answer and Explanation

25True or False:  A while loop is  known as a pre-test or entry-condition loop.  This is because the conditional test is performed before the code in the body of the loop is executed.  If the test returns false the first time that it is performed, the code in the body of the loop won't be executed.

Other programming languages such as Java, C++, and C# also have an exit-condition loop, known as a do-while loop.  The major significance of the exit-condition loop is that the code in the body of the loop will be executed at least once, even if the test returns false the first time that it is performed.  This is because the test is performed after the body of the loop has already executed once.

Alice does not have an exit-condition loop.

Answer and Explanation

26True or False:  If you drag the loop tile (instead of the while tile) from the bottom of the Alice development screen and drop it into the edit pane, a for loop will be created.  A for loop is an exit-condition loop.  The code in the body of the loop will continue to execute for as long as the conditional clause evaluates to true.

Answer and Explanation

Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

The following image is the splash screen from Alice 2.0, and is the property of the developers of Alice at Carnegie Mellon.

## Answers and Explanations

False

True

True

False

True

False

True

False

### Explanation 19

Back to Question 19

True

False

True

### Explanation 16

Back to Question 16

### Answer 15

False (infinite, not finite)

True

True

False

False

True

True

True

False

True

True

False

### Answer 3

C. repetition or loop

### Answer 2

C. will exit (terminate) when count = 10.

C. 20

### Explanation 1

Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

The following image is the splash screen from Alice 2.0, and is the property of the developers of Alice at Carnegie Mellon.

-end-