## Counter Loops, Nested Loops, and Sentinel Loops

Published:  June 28, 2007
By Richard G. Baldwin

File: Alice0175PracticeTest.htm

## Questions

1.  Given the following conditions:

• temp contains a numeric value.
• temp should be incremented by one if its current value is greater than zero.

Listing 175-113.
 ```public void main ( ) { // A if ( 0 > temp ) { temp ++ } else { Do Nothing } // B if ( temp > 0 ) { temp ++ } else { Do Nothing } // C if ( temp < 0 ) { temp ++ } else { Do Nothing } }```

Which block of code in Listing 175-113 satisfies the given conditions: A, B, or C?

2.  After the execution of the code in Listing 175-114:

Listing 175-114.
 ```public void main ( ) { Number sum = 0 ; Number count = 0 ; Number num = 0 ; Number average = 0 ; while ( ( count <= 3 ) ) { num.set( value , (NumberDialog(question = numb ) ) ); sum.set( value , ( ( sum + num ) ) ); count.set( value , ( ( count + 1 ) ) ); } average.set( value , ( ( sum / count ) ) ); print( sum ); print( average ); print( count ); }```

• A. sum will contain the total of all of the values of num that are entered.
• B. average will contain the average of all numbers entered.
• C. count will contain the number of values entered.
• D. All of the above

3.  Which type of loop is shown in Listing 175-115?

Listing 175-115.
 ```public void main ( ) { for (int index=1; index< 10 times ; index++) { print( index ); } }```

• A. Sentinel Controlled
• B. Counter Controlled
• C. Event Controlled
• D. None of the above

4.  True or False:  The name counter loop results from the fact that a loop of this type runs an incremental counter and continues looping until the value of the counter reaches or exceeds a pre-specified limit.  The counter may increment by a value of one or by a value of more than one, but it increments by the same amount each time it increments.

5.  True or False:  The name nested loop results from the fact that it consists of two or more loop structures following one another.

6.  True or False:  A sentinel loop is a loop that continues looping until some value (the sentinel) attains a value of zero.

7.  True or False:  The sentinel value used for a sentinel loop must be chosen carefully to ensure that it won't be confused with a valid data value.

8.  True or False:  A while loop is the ideal syntax for a counting loop.

9.  True or False:  The template for a for loop contains four clauses surrounded by parentheses and separated by semicolons:

• A counter declaration and initialization clause.
• A conditional clause.
• A counter update clause.
• A length clause.

10.  True or False:  The counter declaration and initialization clause in an Alice for loop consists of everything between the left parentheses following the word for and the first semicolon.

11.  True or False:  Type int is normally available to the Alice programmer

12.  True or False:  It is not necessary to declare and initialize a counter variable for a for loop in Alice.

13.  True or False:  The conditional clause in an Alice for loop is essentially the same as the conditional clause in a while loop or in an if-else construct.

14.  True or False:  The counter update clause in an Alice for loop appears after the third semicolon and before the closing parentheses.

15.  True or False:  Even though all three clauses are physically located in the header of the for loop, that isn't necessarily when and where they are executed.

16True or False:  The counter declaration and initialization clause is executed once during each iteration of a for loop.

17True or False:  The conditional clause is executed once and only once when control first enters the for loop structure.

18True or False:  Like the while loop, the for loop is an exit-condition loop.

19True or False:  If the expression in the conditional clause of a while loop or a for loop evaluates to false, the body of the loop is skipped and control passes to the code immediately following the loop structure.

20True or False:  The counter update clause in a for loop is executed once during each execution of the body of the loop.  However, regardless of the fact that the update clause is physically located in the loop header at the top of the loop, it is not executed until after the last statement in the body of the loop has been executed.  In other words, it is executed immediately before control transfers back up to the conditional clause to make the next test.

21True or False:  Testing two values of type Number for absolute equality is a safe and recommended way to write Alice code.

22True or False:  The starting index value in an Alice for loop must always be zero.

23True or False:  The additive assignment operator can be used in the counter update clause in an Alice for loop to let you specify the amount by which the counter variable will be increased each time the counter update clause is executed.

24True or False:  It is possible to nest while loops and if-else constructs in Alice.  However, it is not possible to nest for loops.

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

False

False

True

True

### Explanation 19

Back to Question 19

False

False

False

### Explanation 16

Back to Question 16

True

False

True

True

False

True

False

False

True

False

False

True

### Explanation 4

B. Counter Controlled

### Explanation 3

D. All of the above