Loops

In this problem set you will learn to build a program that repeats a set of instructions. The programming structures that repeat are called loops.

Repeat a Number of Times

This program repeats a set of instructions five times. Run the program and observe the results. Then, modify the repeat times loop so that it repeats three times.

Intro to Counters

Creating a loop often includes adding a counter to track which iteration of the loop you are on. In this problem, add a counter from the tray to the repeat times loop. The program will then output the following information to the output window.

On loop run number 1
On loop run number 2
On loop run number 3
All done!

Intro to Scope

Explore scope by moving the count variable so it is the first block of code inside the repeat loop. Next, run the program. Why is the value of the count variable staying at 1?

Outside of a Scope

The repeat scope in this program is the code between the start of the loop and the end block. It is important to realize that a variable defined inside a scope only exists within that scope. Run this program that tries to use a variable defined inside a different scope. Read the error and remove the block that is causing the problem.

Boolean Expressions and While Loops

In the next exercise, you will look into making a while loop. Before this next step, remind yourself how boolean expressions work.

Read through this program and add the correct output statement for each of the four results.

Repeat While

Following the same pattern as the previous problem. Add the two blocks to the while loop in the correct order so that the program outputs the following text.

0
1
2
The final value of count is 3

Boolean Expressions and Until Loops

In the next exercise, you will look into making an until loop. Before this next step, look through an unrolled version of an until loop using a counter and boolean expressions.

Read through this program and add the correct output statement for each of the four results.

Repeat Until

Look through this program and then run it. Next, identify the problem with the until loop. Keep in mind, the following output is the correct program output.

0
1
2
The final value of count is 3

Problem Solving with Loops

In this problem, the program should calculate the number of digits in a given positive integer, in this case, 1240791. You can count the number of digits, 7, but your job is to make a program that can count the number of digits of any given positive integer.

Most of the program has been written for you, but it still needs a repeat while or repeat until block with a boolean expression. Add a repeat block and add the boolean expression that will produce the following output.

124079
12407
1240
124
12
1
0
The number of digits in the original integer is 7

Practice with Loops

Now that you have practiced editing and adding loops with boolean expressions. Use the blocks in the tray to build a program that counts by 5s to a max of 30. This will require that you edit each of the blocks in the tray.

Use one loop to produce the following output.

5
10
15
20
25
30