Goals

The goal of this lab is to learn the following:

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

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!".

Activity: Write a phrase 10 times.

You can choose the phrase that you want for this particular activity.


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.

Use repeat while to check how many cheeseburgers you can buy.

Activity: Use $1.19 as the price for the cheeseburgers, and write a phrase telling how many cheeseburgers you 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).

Activity: Repeat the previous activity using repeat until instead of repeat while.

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.

Activity: Write a program that repeats until the random number is greater than 50

You can choose if you want to use the repeat while or the repeat until statements and the number should be randomly generated by the computer.

Next Tutorial

In the next tutorial, we will discuss Lab 3.3, which describes More Conditionals, Loops, and Debugging in Quorum..