Lab 3.2: Loops
Goals
The goal of this lab is to learn the following:
- Understanding the types of repeat statement
- Using loops
Computer Science Principles Curriculum
- Big Idea: Algorithms: EK 4.1.1A, EK 4.1.1B, EK 4.1.1C, EK 4.1.1D, EK 4.1.1E, EK 4.1.1F, EK 4.1.1G, EK 4.1.1H, EK 4.2.1A, EK 4.2.1B
- Big Idea: Programming: EK 5.1.2A, EK 5.1.2B, EK 5.2.1A, EK 5.2.1B, EK 5.2.1C, EK 5.2.1D, EK 5.2.1E, EK 5.5.1A, EK 5.5.1D
Common Core Standards
- English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.10, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.11-12.4, CCSS.ELA-Literacy.RST.11-12.5, CCSS.ELA-Literacy.RST.11-12.10
- Mathematics Content: High School Functions » Building Functions: CCSS.Math.Content.HSF.BF.1A
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP4, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8
Vocabulary
- Loops
- Repeat times
- Repeat while
- Repeat until
- Random
Overview
In this lab, we will learn about using loops in Quorum.
Goal 1: Understanding the types of repeat statement
The concept of repetition, where we do the same thing over and over again, is common in computer programming. Robots in the real world, for example, might do the same thing repeatedly in order to allow humans not to. Programs as well, might use repetition to do things like long searches, like looking for a phone number in a list. In this first goal, we will use the 'repeat' statement to do this.
In order to use loops you must use the repeat
statement. There are 3 types of the repeat statement: repeat times
, repeat while
and repeat until
.
The repeat times
statement you will use to perform a task a certain number of times.
Example: Write "Hello!" 5 times.
repeat 5 times
output "Hello!"
end
You should see in your output window exactly 5 "Hello!".
The repeat while
loop works differently than the repeat times
loop. The repeat while
loop works like an if statement, but at the end
keyword, Quorum evaluates and determines if the loop should go again.
Example: Use repeat while
to check how many apps for your phone you can buy.
text balanceInput = input("How much money do you have in your account?")
number balance = cast(number, balanceInput)
integer counter = 0
repeat while balance > 1
number price = 1.0
counter = counter + 1
balance = balance - price
output "You have enough money to buy another app!"
end
output "You don't have money enough to buy another app! You have bought " + counter + " apps!"
For testing, let's use 6 as the input. Our output should be 5 times the phrase that we have enough money to buy another app. The last line is the phrase saying we do not have enough money and how many apps you have bought.
The final type of loop in Quorum is the repeat until
loop. The repeat until
functions much in the same way as repeat while
, but the interpretation of your condition is slightly different.
Instead of saying repeat while balance > 1 you can say repeat until balance < 1.
The difference between these two conditions is that in the repeat while the number 1 is not included while in the repeat until the number 1 is included, for them to match exactly you should use <=(less or equal).
Your output should look exactly the same for the same input with both statements.
Goal 2: Using Loops
You will work with all three kinds of loops to get a grasp of each loop.
Also, for this task, you will be using the Random
class. Add a use statement for the Random
class, found in the Compute library, at the top of the file.
To help you become familiar with the Random
class, sample code using the Random
class is provided below.
Example: How to use the Random Class.
use Libraries.Compute.Random
Random choice
integer counter1 = 0
integer randomValue = 0
repeat 6 times
randomValue = choice:RandomInteger(100)
counter1 = counter1 + 1
output "counter = " + counter1
output "random value = " + randomValue
end
The loop repeats the block of code 6 times as you can see and each time the computer choose a randomly generated integer
value between 0 and 100.
Next Tutorial
In the next tutorial, we will discuss Assignment 3.2, which describes how work Number Guessing Game in Quorum..