My name is Jerome and I work in the Windows devices group at Microsoft.
It's basically a team where we build cool stuff, tablets, phones, things like
Hololens, that hopefully will excite and delight customers.
Computers are not infinitely fast, but they are much faster than human beings
at many tasks. In 2014 the fastest computer in the world
was clocked in at an astounding 33 quadrillion calculations per second.
Now for some tasks like composing a song generally human beings do a better job.
For highly repetitive tasks that can be automated, computers are much better.
Programming languages usually have a few different ways to define and control
behavior that should repeat. These are usually called "Loops".
They go by different names in different languages, like For-Loop, While-Loop
Do-While-Loop, For-Each-Loop, Repeat-Loop, and so on and so forth.
These types of loops have small differences, but all of them are
just different ways to get the computer to execute some lines of code repeatedly.
The generic term for looping is Iteration.
Iteration is just a fancy word that means "repeat". It usually is reference to the
repetition of a process or a procedure.
This is why iteration is a good generic term.
Often in programming, we have an algorithm that just needs to repeat a specific
number of times. Or repeated while some condition holds true. We are going to show
you how to use a looping structure called a "For-Loop" in Javascript.
You can find For-Loops in the control section of the toolbox.
It looks like this when you drag it out.
A For-Loop in javascript like this one is just a type of counter loop.
It counts up from zero to the number you type in.
Why does this simple For-Loop look so complicated?
Javascript, unlike some languages, doesn't provide a simple repeat loop structure.
The For-Loop is a very powerful and versatile looping structure for both
advanced and simple tasks. We're just keeping it simple for now.
Let's do a little experimentation. You can drag any commands you like inside the
For-Loop block. These commands will be executed over and over for however many
times you specify in the loop. When you run the loop, notice that Applab
highlights each line as it executes.
Now why does the For-Loop block highlight after each iteration of the loop?
That's because the For-Loop needs to check its counter, every time. To see if it
should stop, or run the commands inside the loop one more time.
Well, that's not terribly exciting. But, it's because we're not really using
the computing power that looping gives us.
Instead of looping this code 4 times lets loop it 200 times and see what happens.
Now that's more interesting.
One thing to note about loops is that your code will still execute from top to bottom
you can put lines of code before the loop if you need to get something set up
before the loop starts. Likewise, after the loop is done executing, it will pick
up on the line right after the loop.
For example, if you want to move the turtle before drawing this figure, I could
use a moveTo command before the loop. And if I want to draw a red dot to see
where the turtle ends up, I could add lines after the loop.
You will use the same top-down design in many of your projects.
These problem solving techniques will help you identify where loops belong in your
code. You still might want to write a function to do a small part of your
program, and then write a loop to call that function.
Take for example, the problem of trying to draw this.
You might recognize that this is just a bunch of squares. Each one rotated
slightly before being drawn.
Previously we would have had to make a function to draw a square. To get the ten
rotating squares, we would have had to call this function ten times, turning a
little each time after we called the function.
This would have involved a lot of copying and pasting.
This code works, but is very repetitive.
Lets see if we can simplify our code with a loop. We already have a working
function that does a piece of the problem once. We can easily stick the function
call in a loop to do it multiple times.
Clarity and readability are important considerations when programming.
As we can see, this code is much more readable. It's obvious just looking at the
loop that the intent is to draw a square a bunch of times.
Loops are a powerful programming construct that uses the computer's primary
advantage over humans. That being their amazing speed.
By repeating commands as many times as we wish, loops allow us to solve new
problems at a scale that would be impossible without computers.
There is still much to learn about the loop, but the concept is simple.
Loops repeat commands as many times as we need.
Now you know how to use a simple loop you can use them in your programs
and take advantage of your computer's power and speed.