Tag Archives: repetition

Do while loops in C#

A do while loop is a type of loop which repeats code while a certain condition evaluates to true. However, unlike a while loop which tests the condition first, a do while loop tests the condition after running instructions inside the loop. This means that the code inside the loop will always run at least once even if the condition evaluates to false. This is an example of post-test repetition.

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

Sample code

Take a look at the sample code below. The counter is set to 99 and the condition being tested is counter < 10. However, the code inside the loop runs the first time even though the condition evaluates to false because a do while loop runs the code inside the loop before testing the condition (meaning that the instructions inside the loop will always run at least once).

using System;

namespace MyCSharpProject
{
  class Program
  {
    static void Main(string[] args)
    {
      int counter = 99;
      do
      {
        Console.WriteLine("Counter is: " + counter);
        counter++;
      } while(counter < 10);
      Console.ReadLine();
    }
  }

}

Next tutorial: Arrays in C#

For loops in C#

In this tutorial you will learn how to use the for loop to repeat sections of code in the C# language. For loops are useful for when you want to repeat code a certain number of times. They are also very useful for efficiently working through all the elements in an array or going through each character in a string. For loops have a built in counter, condition, and increment. Watch the video below and then scroll down for the sample code.

Sample code

Example 1 – using for loop as simple simple counted loop

using System;

namespace MyCSharpProject
{
  class Program
  {
    static void Main(string[] args)
    {
      for (int i = 0; i < 10; i++)
      {
        Console.WriteLine("Counter is: " + i);
      }
      Console.ReadLine();
    }
  }

}

Example 2 – using for loop to go through each character in string

using System;

namespace MyCSharpProject
{
  class Program
  {
    static void Main(string[] args)
    {
      string word = "dog";
      for (int i = 0; i < word.Length; i++)
      {
        if (word[i] == 'a')
        {
          Console.WriteLine("This word contains the letter a");
        }
      }
      Console.ReadLine();
    }
  }

}

Next tutorial: Do while loops in C#

While loops in C#

This tutorial explains how to write a while loop in the C# language. A while loop is used to repeat a section of code while a condition evaluates to true. For example, keep asking for a password while the password being entered is wrong, or keep displaying a message a number is less than a certain number, or keep moving a character in a game to the right side of the screen while the right arrow key is being pressed on the keyboard.

A while loop is a pre-test loop meaning it tests a condition before running code inside the loop. If the condition being tested never evaluates to true then the loop will not run. After each iteration of a loop, if the condition being tested evaluates to false then the loop will exit and the rest of the program will run. It is important that you allow loops to eventually end by specifying a condition that will eventually evaluate to false, otherwise you will end up having an ‘infinite loop’ causing your program to crash.

Watch the video below and then scroll down for the sample code.

Sample code

using System;

namespace MyCSharpProject
{
  class Program
  {
    static void Main(string[] args)
    {
      int counter = 0;
      while (counter < 10)
      {
        Console.WriteLine("The counter is " + counter);
        counter--;
      }
      Console.ReadLine();
    }
  }

}

Next tutorial: For loops in C#

For loops in C#

For loops are used to repeat code a certain number of times. For example, a for loop could be used to repeat an instruction 10 times. For loops are much more useful than that though. For example, they can be used to process items in a list one-by-one (we will learn how to do that later).

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

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.

Sample code

This for loop will start with an integer value of 0 and then keep increasing the number by 1 and displaying its value until the value is no longer less than 10.

using System;

namespace MyApp
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      for (int i = 0; i < 10; i++) {
        Console.WriteLine (i);
      }
    }
  }
}

And another example shown below will repeat the word “Hello” 10 times.

using System;

namespace MyApp
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      for (int i = 0; i < 10; i++) {
        Console.WriteLine ("Hello");
      }
    }
  }
}

Foreach loops in PHP

This tutorial explains how to use the foreach loop to loop through each item (element) in an array. The foreach loop can only be used on arrays and objects in PHP. On each iteration of a foreach loop, the value of the current element being accessed is assigned to $value. Foreach loops can be used on both regular arrays and associative arrays.

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

The sample PHP code below shows how to use a foreach loop for both regular arrays and associative arrays:

<?php
  $normal_array = array("Mary","Bob","Joe","Tim");
  $assoc_array = array("Sarah"=>25,"Kim"=>18,"Alice"=>32,"Alan"=>17);

  // using a foreach loop on a regular array:
  foreach($normal_array as $value){
    echo "Name: $value</br>";
  }

  // using a foreach loop on an associative array:
  foreach($assoc_array as $key => $value){
    echo "$key's age is $value.</br>";
  }
?>

While loops in PHP

In this tutorial you will learn how to use a while loop in PHP. A while loop‘s syntax is slightly different to a for loop. A while loop will test a condition and will repeat a section of code inside the loop while that test condition evaluates to true. A while loop always tests the condition before running the code inside the loop (if the condition evaluates to true).

Unlike for loops, while loops do not have an inbuilt counter but you can include your own counter variable if you want to use one. While loops and for loops can often be used for the same purpose or to achieve the same goal. However, in different situations, one type of loop may be better than the other. For example, a for loop may be more efficient for going through each element in an array and when written may also express the statement in a clearer way.

Watch the video below and then scroll down to see the sample code for a PHP while loop.

Sample PHP code:

1
2
3
4
5
6
7
<?php
 $counter = 0;
 while($counter < 10){
 echo $counter , '</br>';
 $counter++;
 }
?>

PHP Manual references:

For loops in PHP

This video tutorial explains how to use for loops to repeat code using a counter. For loops have many uses such as processing arrays and strings but in this tutorial we will just look at how to use a for loop as a counted loop.

A for loop is made up of three parts:

  • counter – which is initialised
  • condition – a condition that must evaluate to true for the loop to run its code, and is tested in each iteration of the loop
  • increment – how much the counter increases by in each iteration of the loop

For example, a for loop may contain a counter which starts at 0, and the condition in the loop is for the loop to repeat for as long as the counter is less than 50, and the increment is 1. For loops are very useful for repeating code a specified number of times, going through all the characters in a string or elements in an array and using the loop counter to check the characters in a string or elements in an array, one by one – this can be achieved by using the counter in each iteration of the loop to represent the index of the character in a string or element in an array being accessed in each iteration of the loop.

This example would look like:

for($i = 0; $i < 50; $i++){
  // code inside the loop goes here
}

In the example above a counter called $i is used. It is quite common for these loops to use a counter called i but you can call the counter whatever you like (following the same rules as any other variable). You can place for loops within for loops. If you do that, make sure you give each loop a different name for its counter. The example above also increments the counter by 1 ($i++) in each iteration of the loop.

The video below shows the steps involved in creating and using a for loop. Watch the video below and then scroll down for the sample code.

Sample PHP code:

<?php
 for($i=10;$i > 5;$i--){
 echo("This is iteration #" . $i . "</br>");
 }
?>

PHP Manual references: