Looping

Introduction

When writing programs there will be instances where a task needs to be repeated a number of times until a certain condition occurs. There is a number of ways of doing this. If the task needs to be repeated x amount of times then the for function is sufficient. If a task needs to be repeated but the terminating condition is dependent on a piece of data need to equal to some value or an external source i.e. user enters a certain character then the while or do while functions needs to be used. Looping functions are also used to access data like arrays that have numerical instances. The most commonly used function for this is the for statement.

As all those used comparison operators, it might be advisable to read more on it. The link is here.

For Function

The for function starts at a specified value and then repeated the code associated with it until a value is met. Most of the time this value is increased or decreased by 1 but not necessarily so.The structure of the for statement is as follows:

for (variable = starting value ; variable meets end value; variable increases/decreases by value)

  • execute code

A simple for statement is as follows:

for (var i=0; i< 9; i++) 
   print (i);

What happens is that variable i is set to 0, checks to see if variables i is less than 9. If it is less than 9 then increases the value of i by 1 and executes code, is this instance, prints the value of i. If it is not less than 9 then the for loop terminates.

There may be instances that you want the loop to go in reverse. The code for this is as follows:

for (var i =9; i >= 0; i--) 
   print (i);

In this case variable i is set to 9, checks to see if variables i is less than more than or equal to 0. If it is more than or equal to 0 then decreases the value of i by 1 and executes code, is this instance, prints the value of i. If it is not more than or equal to 0 then the for loop terminates.

The incremental/decremental value does not need to by steps of one as seen in the following example which the step is in increments of 3:

for (var i =0; i <=30; i+=3) print (i);

Simple examples have been used but in real life situations, multiple lines of code would be executed in the for loop. In these instances, the code needs to enclosed in parentheses. The following example shows this in action:

   1 #!/usr/bin/env seed
   2 
   3 for ( var i = 0; i  < 9; i++) {
   4   print ("this is the value of x ");
   5   print (i);
   6 };

For loops are used on data structure that use numerical indices as a method of storing them. This will explained later in the tutorial.

While function

The for statement is used when it is known for the loop to terminate. If the condition is not known for the loop to terminate the while statement is used.

Although the while function it is the same as the do while function that loops until a condition is met, there may be times where in the first instance of the loop, the code should not be run. If this is the case the while function is used.

The structure of the while statement is that a variable which already exists is checked to see if it meets a certain condition. If it meets that condition then execute code. If it does not meet that condition then terminate loop. Inside the code that is executed, there needs to be a statement in it that changes the conditions of the variable. If there is not then the while loop will continue indefinitely which is not what you want!

The structure of the while loop is as follows:

while variable does not meet condition

  • execute code which changes contents of variable as well

Unlike the for statement which can execute one line of code, all code to be executed needs to be enclosed in parentheses.

An example of this as follows:

   1 #!/usr/bin/env seed
   2 
   3 var x = 0;
   4 while (x <5){
   5    print(x);
   6    x++;
   7 }; 

An explanation of the code is as follows:

while (x <5){

If variable x is less than 5 execute code. If x is equal to 5 or more then terminate loop.

   print(i);
   x++;

The value of x is printed and increased by 1. The program goes back to the start of the while loop.

Do while function

If it does not matter if code is to be executed on first run of loop then the do while function is used. The structure of this is as follows:

do

  • execute code which changes contents of variable as well

while variable does not meet condition

An example of this is as follows:

   1 #!/usr/bin/env seed
   2  
   3 var foo = 5;
   4 do {
   5     print(foo);
   6     foo--;
   7 } while (foo >= 0);

An explanation of this is as follows:

do {

Start loop.

    print(foo);
    foo--;

Execute loop which prints value of foo and decrease it by 1

} while (foo<5);

If foo is more than or equal to 0, go to start of loop and execute code again. If foo is less than 0 then terminate loop.

Projects/Seed/Tutorial/basic/loop (last edited 2013-11-22 19:19:58 by WilliamJonMcCann)