Loop Review Questions and Exercises

From CompSciWiki
Revision as of 20:57, 19 March 2007 by Umwooley (Talk | contribs)

Jump to: navigation, search

Review Questions

While Loops (Vic)

1. When do you use a while loop?

2. Under what situation will a while loop continue to iterate?

For Loops

1. When do you use a for loop?

2. If your for loop looks like this: "for(int i = 0; i < 50; i = i+5)", how many times will it iterate?

3. Are you allowed to read the control variable in the body of the while loop?

Additional Information

1. Describe what a sentinel value is and how it is used. (David)

2. Describe what a running total is and how it is used. (David)

3. Loops can have multiple conditions or control statements. Explain why this is helpful and how it can be used to create better code. (David)

Exercises

1. The Rock Pile

(David)

The point of this game is to force the other player to throw the last rock in the lake. At the beginning of the game, the user should set the number of rocks to start between 50 and 99. The game is played in the following manner: There are two players you and the computer. The user will always go first. On each of the users turn you prompt the user to pick between 1 to 3 rocks from the pile to throw in the lake. On the computer's turn, the computer will always make the number rocks thrown in equal to four. So that if the user throws in 2 rocks, the computer on the next turn will throw in 2 rocks. If the user throws in 1 rock, then the computer will throw in 3 rocks and so on.

Bonus: Can you make it so that the Computer will me smarter when there are 10 or less stones left? // Note: solution for bonus not finished

The Rock Pile Solution

2. Ascii Art X

(Roger)

Create a program that will print out ascii art in the shape of an X. The program will prompt the user for input in the form of a number of at least size 3. For example if the user typed the number 5 your result would look like:

Answers

While Loops (Vic)

1. You use a while loop when you don't know beforehand how many times you need to iterate.

2. A while loop will continue to iterate for as long as the test condition evaluates to true.

For Loops

1. You use a for loop if you know beforehand how many times you need to iterate.

2. It will iterate 10 times, when 'i' = 0,5,10,15,20,25,30,35,40,45

3. You are allowed to read the control variable in the body of the loop, but don't try to write it.

Additional Information (David)

1. A sentinel value is used as an abnormal end case to a loop. It is used by giving the sentinel value a value outside the operating parameters of the loop. Such as -1 if we are counting from 1 to 10. The counter is used and set to -1 so that the loop will exit before completing all of it's iterations.

2. A running total is a count of either iterations or items. This can be used to count the sum or the number of even numbers between 1 and 20. It is used by creating a variable that will increment each time a condition is met inside the loop.

3. Having multiple control statements or conditions in a loop is beneficial as it can increase efficiency. This is done by exiting using these extra conditions to set conditions where the loop will exit before completing all of its iterations. This can be done using a sentinel value to cause an end case that will set the result of the condition statements to be false.

Back to Loops