Repeating 🔗

repetition, loop

There are times when you may want to repeat some stuff for the user over and over again. You can save a bunch of typing if you use the *repeat keyword. Here is how it works:

*repeat: 6
	All work and no play makes jack a dull boy

▶ Run

When run, this will display to the user six repetitions of the text, which will look like this:

All work and no play makes jack a dull boy
All work and no play makes jack a dull boy
All work and no play makes jack a dull boy
All work and no play makes jack a dull boy
All work and no play makes jack a dull boy
All work and no play makes jack a dull boy

Saves a lot of typing, doesn't it? But text isn't the only thing you can repeat. For instance:

*repeat: 6
	All work and no play makes jack a dull boy.
	*question: What do you think?

▶ Run

This will display the text "All work and no play makes jack a dull boy" then ask the question "What do you think?", wait for the user to respond, then display the text "All work and no play makes jack a dull boy" again, ask "What do you think?" again, wait for response, and repeat the whole process a total of 6 times. This is a great way to drive your user insane, especially if they are trapped with their family in a creepy hotel for the duration of the winter.

Here's a more helpful example:

I'd like you to take a minute now to think of three things that you're grateful for. This provides an immediate mood boost for many people. I'll walk you through the process, step by step.

*repeat: 3
	*question: What's one thing you're grateful for?
	For the next few seconds, visualize what life would be like if you didn't have this thing.
	*wait: 10.seconds
	Now remind yourself now how your life is improved by this thing that you're grateful for.
	*wait: 10.seconds

▶ Run

This program, when run, will ask "What's one thing you're grateful for?" When you answer, it will say "For the next few seconds, visualize what life would be like if you didn't have this thing." Then, it will pause for 10 seconds (showing nothing else to the user during this time). Next it will say "Remind yourself now how your life is improved by this thing that you're grateful for." It will then pause for another 10 seconds. This whole process will repeat a total of 3 times.

Not only does the *repeat keyword help prevent your hands from getting tired, but it makes it easier to make changes to your GT program later on. If you decide to write out repetitious stuff rather than using the *repeat keyword, for instance using:

All work and no play makes jack a dull boy
All work and no play makes jack a dull boy
All work and no play makes jack a dull boy

and then you decide later to make a change, like:

All socks and no stares makes jack a sad clown
All socks and no stares makes jack a sad clown
All socks and no stares makes jack a sad clown

then you'll have to make the change on all three lines (how annoying!). Instead, by using the *repeat keyword, you'd just have to change one line. A generally good principle in programming (whether it be in GT, or another programming language) is that you should reduce code redundancy as much as possible. That is, you should avoid having the same stuff written out multiple times, and instead find a way to just write it once while still getting the desired outcome (e.g., by using the *repeat keyword). One reason this is a good idea is that it prevents mistakes that arise from changing one copy of something and forgetting to modify it in all the other locations it appears. Programmers call this the DRY principle: D on't R epeat Y ourself.

The *repeat keyword can also be used with variables. Let's take the following example:

*question: How many times do you want to do this activity?
	*type: number
	*save: times

*repeat: times
	Take a deep breath in
	*button: Next

	And take a deep breath out
	*button: Next

The end

In the example above, the user would be allowed to specify how many times they'd like to do the "activity" and consequently see the two pages of the activity: the first containing the text "Take a deep breath in" and a "Next" button and the second containing "And take a deep breath out" and a "Next" button. So, if users entered "2" in the question box, they would be instructed to take a deep breath in and out twice.

You can write any number expression after a *repeat keyword to state how many times the indented content should be repeated (e.g., 3, a variable such as times , points + 1 , etc.).

Repeating While a Given Condition is Being Met 🔗

while loop, repeat

The *while keyword is the same as *repeat , except that it allows the author to determine how many times to execute the code dynamically. So, instead of repeating something a specified number of times, it repeats it eternally *while a given condition is being met. Let's illustrate that with an example:

If you can guess my name you'll win $10,000,000.

>> answer = "wrong"

*while: answer = "wrong"
	*question: What's my name?
		*type: text
		Rumpelstiltskin
			>> answer = "correct"
		*other
			That's not my name. Try again.

You did it! You won the money!

In the above program, the user will see the text enticing them to guess the narrator's name. They will also see the question "What's my name?" If they guess the narrator might be named "Harold" they will see the text "That's not my name. Try again." Then, they'll once more be presented with the question "What's my name?" They can keep trying new names (Sally, Gilbert, Veronica), but so long as their answer is anything *other than Rumpelstiltskin, they will repeatedly see the text telling them to try again and will repeatedly be asked "What's my name?" in a debilitating, never-ending cycle.

As soon as the user guesses "Rumpelstiltskin" (with appropriate caps and spelling), they will finally break free from this cycle and will at last be told they won the money.

So how does this work?

The program will execute the code indented beneath *while so long as the variable answer is still being defined as the string "wrong". As an editor, you must create some kind of condition in which the value of that variable could potentially change so the user can continue on with the program.

Remember that with *type: text questions, the user is presented with a box in which to write an answer, but as an editor you can also create customized content for specific answers they could potentially write. We created customized content for the answer "Rumpelstiltskin" as well as anything *other than "Rumpelstiltskin".

Only if the user writes "Rumpelstiltskin" would the content >> answer = "correct" be activated by the user. Once that happens, this would change the variable answer from "wrong" to "correct". Only while answer is "wrong" would the question repeat.

(Note: instead of re-defining answer as "correct", we could have also written "you got it", "bingo", or "183847438". So long as answer was anything other than "wrong", the cycle of questions would end).

Here's one more example, using the *clear keyword.

>> seconds = 10

*while: seconds > 0
	Countdown
	{seconds}
	*wait: 1.second
	*clear
	>> seconds = seconds - 1

Blast off!

The above program is a countdown. The user will see the text "Countdown" with a number beneath it. To start, the number shown will be 10. Then there will be a wait of 1 second, and the number shown will appear to change to 9; then there will be another second wait and the number will change to 8, and so on. The next-to-last thing users will see is the text "Countdown" and the number 1. Then, the screen will *clear and they will see the words "Blast off!"

Here, *while the variable seconds is greater than the number 0, the indented content will repeat indefinitely.

We know the content won't repeat indefinitely though, because the seconds variable is slowly decreasing by 1 each time the program passes this line: >> seconds = seconds - 1 . And it passes this line exactly 10 times, until seconds = 0 and the *while keyword no longer kicks in. That's when users will see "Blast off!".

Here's how the *clear keyword comes into play. Without it, the user's screen would begin to look something like this:

Repeating While a Given Condition is Being Met (1)

However, because we've added the *clear keyword, the user's screen will clear each time they get to the *clear keyword section of the *while content. That means that the words "Countdown" as well as the number on display will disappear and will be quickly be replaced by a new "Countdown" and a new number. Usually, these clears happen pretty quickly, though; so to the user, it will seem as though "Countdown" stays on the screen the entire time and only the numbers change.


Next: