EZ Math Movie, Tutorials, Loops, The For Loop


Do something more than once.

Often we want to do something many times. Say we want to draw 10 line segments or print a list of 5 consecutive even numbers. Computer languages have statements that make this type of work easy. These statements are called loops. Loops contain code that repeats again and again. In EZ Math Movie we usually work with a type of loop named a for loop.

A for loop has many parts:

For loop syntax

For loop syntax

The first line

This loop will do a job 5 times. Its job is to print 'Hello', so it will print 'Hello' 5 times. But why 5 times? Let's understand that. Let's just say that the loop cycles 5 times and forget, for the moment, what job it does each time it cycles.

The first line of the for loop is where you state how the loop will run. The first line starts with the word for. After that comes what we will call the control section. The control section is within a set of parentheses.

The control section has three parts. They are separated from one another by semicolons, as shown in the previous section's diagram.

The first part

The first part of the control section happens before the loop cycles even once. In our example it looks like this:

i = 0;

We will call it the setup part. Usually, here a variable is set to an initial value, often 0. This variable is a counter. We use it to keep track of how many cycles the loop has run. Note the closing semi-colon that separates this first part from the second part.

The variable i is often used as a counter variable in a for loop. Here we set i to its starting value. As this loop runs, i will increase by 1 after every loop cycle. As long as its value is below 5, the loop will keep running. That is how this for loop runs, and most of the for loops we will use will be a variation on this one.

So in the first part of the control section a counter variable, i, is set to its initial value. How is the check made to be sure it is less than 5? That answer is in the second part. How does i increase by 1 after each cycle? That is explained in the third part.

The second part

The second part of the control section, like the first, happens before the loop cycles even once. In our example it looks like this:

i < 5;

We will call this the check part. This part is a Boolean statement that is evaluated at the start of each loop cycle, even the first. If it is true, then the loop runs one more cycle. It will be checked again at the start of the next and every following cycle. If it ever is false, then the loop quits cycling. Note the final semi-colon separating this check part from the third part.

Now, i starts out at zero, and it increases by 1 after each loop cycle. (Why i increases will be shown in a bit.) To see how this Boolean statement controls the loop to run through 5 cycles and then quit, read the following table from left to right, top to bottom.

The second part details

Value of i at start of cycle Boolean check at start of cycle Cycle or quit Increment i after cycle
i = 0 i < 5 is 0 < 5 is true loop runs first cycle i = 0 + 1 = 1
i = 1 i < 5 is 1 < 5 is true loop runs second cycle i = 1 + 1 = 2
i = 2 i < 5 is 2 < 5 is true loop runs third cycle i = 2 + 1 = 3
i = 3 i < 5 is 3 < 5 is true loop runs fourth cycle i = 3 + 1 = 4
i = 4 i < 5 is 4 < 5 is true loop runs fifth cycle i = 4 + 1 = 5
i = 5 i < 5 is 5 < 5 is false loop quits cycling  

The third part

The third part of the control section happens at the end of each cycle. Here it looks like this:

x++

We will call this the after cycle part. This code, as explained in the mathematics tutorials regarding increment and decrement, causes the value of x to be increased by 1. There are other ways to do this, but using ++ lets the computer execute the increment the fastest. Note that there is no semi-colon at the end of this code. It will be followed by the close parenthesis of the control section.

Other equivalent control sections

It may seem a bit odd to be counting loop cycles starting from 0, but this is often how it is done in computer programing. You don't have to do it that way, though. You could start counting from 1 and still cycle 5 times. Note the i <= 5 code:

for(i = 1; i <= 5; i++)

Here is another way to count from 1 and cycle 5 times. Note the i < 6 code:

for(i = 1; i < 6; i++)

You could count by 2's. The following would cycle 3 times. Note the i += 2 code:

for(i = 0; i < 5; i += 2)

The loop body

The loop body is everything between the open and close curly brackets that follow the control section. Every time the loop goes through a cycle, the code in the loop body executes. The loop body is the repeated job that the loop performs.

Our simple example only has one line of code in its body. However, the body can hold many lines of code.

The available counter variable

The counter variable is available for use within the body of the loop. For example, this code:

for(i = 0; i < 5; i++)
{
  printNumber(i);
}

Would print this:

01234

You can also use the counter variable in any calculation. Be sure to understand, though, if you change the value of the counter variable within the body of the loop, you will most likely change the way that the loop cycles.

Running the example program

The example program clears the printer, and then prints a short message five times using a for loop.


The example program

0ptions
Angle measurement: Degrees Radians

Go on input

Bounds:
Sorry, apparently this browser does not support HTML 5 canvas tag graphics.
Printer

Edit: Runtime          View: Starter Suggestion

To runtime:

Storage:

Editor width: Normal Wide


		

	


		

	


		

	

And....

Next tutorial: Nested for loops


All contents of EZ Math Movie are copyrighted: Copyright 2011, Edward A. Zobel. All rights reserved.



Other sites you may find interesting

Zona Land Education Like Math and Physics?

Zona Land Education offers explanations, diagrams, and animations on these topics.
And it is all presented in a clear and casual manner.

Here is the link: zonalandeducation.com




EZ Programing Demos Want to learn about programing computers?

EZ Programing Demos has an animated, slow motion, interactive programing language demonstrator.
The site presents basic ideas at a beginner's level.

Here is the link: ezprogramingdemos.com



Custom Search

Name:
Domain:

Name
0


Name:
Name
0