Tag Archives: looping

Create a looping walk cycle animation in Adobe Flash / Animate

This tutorial explains how to create a looping walk cycle animation in Adobe Flash (Adobe Animate) like the one shown below.

WalkCycle

You can use the same technique for other types of internal animated loops. Watch the video below to find out how to do this using either the Adobe Flash or Adobe Animate software.

In this video I used the sprite sheet by soldiern at http://soldiern.deviantart.com/art/Sub-Zero-Sprite-Sheet-V2-210769129 (using Creative Commons CC3.0 license – https://creativecommons.org/licenses/by-sa/3.0/ ).

Looping through arrays with PHP

This tutorial explains how to loop through the elements in a PHP array. This works by using a for loop that will access elements in the array by their index. For each iteration of the loop, a different index will be accessed. You will also see how count() is used to count the number of elements in an array. The process for looping through each character in a string variable is basically the same as looping through each element in an array, as a string is basically an array of characters.

Watch the video below and then scroll down to see the sample code.

Sample PHP code to loop through each string element in an array and echo each element’s value on a new line:

<?php
  // create a new array
  $myArray = array("a","b","c");

  // loop through each element in the array
  // count will count all elements in an array
  for($i=0;$i<count($myArray);$i++){
    echo $myArray[$i] , "</br>";
  }
?>

Sample code to loop through an array and modify the value of each element in the array:

<?php
  // create a new array
  $myArray = array(1,2,3,4,5);

  // loop through each element in the array
  // count will count all elements in an array
  for($i=0;$i<count($myArray);$i++){
    // this will multiply each integer value by 2
    $myArray[$i] = $myArray[$i] * 2;
    echo $myArray[$i] , "</br>";
  }
?>

String length in PHP

This tutorial covers a few things about strings in PHP:

  • the index of strings (each character has an index number)
  • how to find the length of a string (how many characters there are in a string)
  • how to ‘loop’ through each character of a string

Although this may not seem all that useful yet, being able to find the length of a string and process each character of a string is something that will become very useful later on.

Indexing and strings

In every string, each character has what is known as an index or index number. In PHP, indexing starts from the number 0. That means that the letter B in the word “Batman” has an index of 0, the second letter “a” will have an index of 1, the third letter “t” will have an index of 2 and so on… The image below shows an example of this.

strings
A string’s index.

You might, as an example, want to find out what the third character is in a string. You could do this by using its index number eg. $mystring[2]; would return the third character in the string.

Finding the length of a string

The length of a string is how many characters are in the string. The word “Batman” has 6 letters in it so its length is 6. Letters, numbers, spaces and other characters are all counted in a string. So the string “Hello world” will have a string length of 11 because there are 10 letters and 1 space in it.

As an example, finding the length of a string might be useful if you want to check how much text was entered into a form (eg. checking that a minimum of 10 characters was used for a password, or a maximum of 160 characters were used for a tweet). The strlen() function is used to find the length of a string. Eg.  strlen(“my string”); will return the length of a string. We will look at other string functions later on.

Looping through strings

A for loop can be used to go through every single character in a string and process it. In the video example, we do this to display every letter of a string on a new line. This example might not be all that useful, but there are many other ways that a for loop can be used to process characters in a string.

Watch the video below and then scroll down to see the sample PHP code.

Sample PHP code for displaying the length of a string:

1
2
3
4
5
6
<?php
 $name = "Batman";
 $nameLength = strlen($name);
 echo $nameLength;
 }
?>

Sample PHP code for displaying the third character in a string:

1
2
3
4
5
<?php
 $name = "Batman";
 echo $name[2];
 }
?>

Sample PHP code for looping through a string:

1
2
3
4
5
6
7
<?php
 $name = "Batman";
 $nameLength = strlen($name);
 for($i=0;$i<$nameLength;$i++){
 echo $name[$i] , '</br>';
 }
?>

PHP Manual references:

Loops (iteration) in C#

There are two main types of loops you can use in C# to repeat sections of code. These are called the while loop and for loop.

while loop

The while loop is the easiest type of loop to use for repetition of code. The basic syntax is as follows:

while(<condition>)
{
// do something
}

It looks very similar to an if statement. However, an if statement only runs the code it contains once. A while loop will run the code that it contains over and over again until the specified condition evaluates to false. Any code inside the { and } brackets will run inside the loop.

Here is an example of a while loop in the C# language:

int count = 0;
 
while(count <10)
{
 count++;
}
print(count);

In the example above, the count variable is initially set to 0. The loop will check if the count variable is less than 10. If it is not less than 10, it will add 1 to the count variable. This will keep repeating until the condition evaluates to false when the count variable’s value is no longer less than 10. When this occurs, the loop will end and the value of the count variable will be displayed (on the last line of the code which is outside of the loop).

It is important that a condition be specified that will allow the loop to end, otherwise the loop will never end! This is known as an infinite loop.

for loop

The for loop is a little more complex than the while loop but at its simplest level it is very easy to set up. The syntax looks like this:

for(<initialise counter>;<condition>;<increment the counter>)
{
// do something
}

Semi-colons separate three important components of the for loop inside the ( and ) brackets. The first part is where a counter is initialised, for example int i=0. The second part is where the condition is specified, for example i<10. The third part is how much to increment the counter by each time the loop runs (each iteration), for example i++ would increment the counter by 1.

for loops are great for using as counters to repeat a section of code a certain amount of times. They are also great for repeating operations on each item in an array (looping through an array) or each character in a string. Below is an example of a for loop.

for(int i=0; i <10; i++)
{
 print(i);
 // the value of i will be displayed for each iteration of the loop
}