Tag Archives: iteration

Break and continue in PHP

In this tutorial you will learn how to break a loop (stop a loop from running when a certain condition is met) and continue (skip an iteration of a loop). A for loop will be used for the example shown in this tutorial.

There may be situations where you may wish to use the break statement if you want a loop to stop running if a condition from an if statement inside the loop evaluates to true. Similarly, you may also want to skip an iteration of a loop using the continue statement.

Watch the video below and then scroll down to see the examples of break and continue being used in PHP code.

Sample PHP code for breaking a loop:

1
2
3
4
5
6
7
8
<?php
 for($i=0; $i<10; $i++){
 if($i==8){
 break;
 }
 echo $i , '</br>';
 }
?>

Sample code for using continue to skip an iteration of a loop:

1
2
3
4
5
6
7
8
<?php
 for($i=0;$i<10;$i++){
 if($i==5){
 continue;
 }
 echo $i , '</br>';
 }
?>

Sample code for using continue to skip any even numbers in a for loop:

1
2
3
4
5
6
7
8
<?php
 for($i=0;$i<10;$i++){
 if($i%2==0){
 continue;
 }
 echo $i , '</br>';
 }
?>

Sample code for using continue to skip any odd numbers in a for loop:

1
2
3
4
5
6
7
8
<?php
 for($i=0;$i<10;$i++){
 if($i%2!=0){
 continue;
 }
 echo $i , '</br>';
 }
?>

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:

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
}